Member-only story

How to add background image in tailwind css?

Ayyaz Zafar
2 min readOct 26, 2023

Setting a background image is a common task in web development. Tailwind CSS, a utility-first CSS framework, provides various methods to add background images. In this guide, we’ll explore three different ways to set a background image using Tailwind CSS.

Prerequisites

Before we delve into the methods, ensure that Tailwind CSS is correctly set up in your development environment. If you’re unfamiliar with setting up Tailwind CSS, you can refer to a tutorial titled “How to install Tailwind CSS in VS Code” available in the Tailwind CSS playlist on my YouTube channel.

1. Using Inline CSS

The most straightforward approach involves utilizing inline CSS to set the background image:

  1. First, download the desired image to your project directory.
  2. Add a div element in your HTML, then apply the background image using the style attribute:
<div
style="
background-image: url('path_to_image.jpeg');
width: [desired_width];
height: [desired_height];
background-size: cover;
background-position: center;
"
></div>

Replace path_to_image.jpeg with the path to your image and [desired_width] and [desired_height] with your preferred dimensions.

--

--

Ayyaz Zafar
Ayyaz Zafar

Written by Ayyaz Zafar

Enrol my Angular 18 course | Build FullStack Blog with CMS in Angular, Node, MySQL, Angular Material, Tailwind CSS https://bit.ly/angular-18-course

No responses yet