Member-only story
How to add background image in tailwind css?
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:
- First, download the desired image to your project directory.
- Add a
div
element in your HTML, then apply the background image using thestyle
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.