How to Add Background Image Overlay in Tailwind CSS?
Last Updated :
11 Oct, 2024
Adding a background overlay to your Tailwind CSS project can help you create visually appealing layouts that layer content over the background image. In this article, we'll demonstrate how to add a background overlay using the Tailwind CSS utility class.
Approach
We will use Tailwind CSS to create a section with a background image. We will implement an overlay using:: before the pseudo-element or by adding a div to the overlay within the section. Tailwind's utility classes can handle styles, and this solution guarantees a responsive design. We will customize the color and opacity of the overlay to suit different designs.
Steps to Create & Configure the Project
Step 1: Create a New React Project
First, we must run the given command to create a new React project.
npx create-react-app bg-image-overlay-gfg
cd bg-image-overlay-gfg
Step 2: Install Tailwind CSS
When we create a new project We will need to install Tailwind CSS and create a configuration file named tailwind.config.js, we can do this by running the following command.
npm install tailwindcss postcss autoprefixer
npx tailwindcss init
Project Structure:
Project structureUpdated Dependencies:
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
Step 3: Configure Template Paths
Edit tailwind.config.js file to add content paths for your HTML and JavaScript files.
/* tailwind.config.js */
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
Step 4: Add Tailwind Directives
In public/tailwind.css file, add base, components and utilities layers of Tailwind:
@tailwind base;
@tailwind components;
@tailwind utilities;
Step 5: Create the Component with Background Image and Overlay
Now let's create a component with a background image and overlay, add a new file called BackgroundSection.js in src/components folder.
JavaScript
// src/components/BackgroundSection.js
import React from "react";
const BackgroundSection = () => {
return (
<div className="relative h-screen bg-cover bg-center bg-no-repeat"
style={{ backgroundImage:
"url('https://media.geeksforgeeks.org/wp-content/uploads/20241009154536548672/0_ilw552fVUGbwIzbE.jpg') ", }}>
{/* Overlay */}
< div className="absolute inset-0 bg-black opacity-50" ></div>
{/* Content on top of overlay */}
< div className="relative z-10 flex items-center justify-center h-full">
<h1 className="text-white text-5xl font-bold text-center px-4">Welcome to GeeksForGeeks</h1>
</div>
</div >
);
};
export default BackgroundSection;
Step 6: Add the Component to App.js
Next import and use BackgroundSection component in your src/App.js file.
JavaScript
// src/App.js
import React from 'react';
import BackgroundSection from './components/BackgroundSection';
import './index.css';
function App() {
return (
<div className="min-h-screen">
<BackgroundSection />
</div>
);
}
export default App;
Step 8: Run the Application
Now you can start your development server.
npm start
Output: Open your browser and navigate to http://localhost:3000
.
Output
Similar Reads
How to set Background Image with opacity in Tailwind CSS?
Tailwind CSS is a utility-first approach to stylesheets that utilizes customized utility classes for styling. Although Tailwind officially supports setting the background image, there is no built-in class to directly set the background imageâs opacity while keeping the opacity of the overlay content
3 min read
How to Add Background Image in CSS?
The CSS background-image property is used to add an image as a background to an element. Syntax background-image: url()1. Setting background Image of an ElementA background image is added to an h1 tag using the URL. [GFGTABS] HTML <h1 style="background-image: url( 'https://media.geeksfor
1 min read
How to Set Background Image in CSS?
CSS (Cascading Style Sheets) can allow developers to set the background image for the web pages or specific HTML elements. This feature can help enhance the visual aesthetics of the website. The background-image property in CSS can be used to specific one or multiple background images to be applied
3 min read
How to Add Rounded Corners to an Image in Tailwind CSS ?
Adding rounded corners to an image in Tailwind CSS is an easy way to make your pictures look nicer. Instead of having sharp corners, you can give them smooth, rounded edges. Tailwind CSS has simple classes that let you choose how rounded the corners are, whether just a bit or completely round. Appro
3 min read
How to Add a Clip Path to Image in Tailwind CSS ?
Web application designers can create creative and eye-catching designs with Tailwind CSS's flexibility in applying clip paths to images. Developers can create complex shapes by using clip paths, which define an element's visible region. Custom Clip Paths using TailwindTailwind CSS's custom clip path
2 min read
How to Add a Background Image in Next.js?
Next.js is a powerful React framework that allows for server-side rendering and the generation of static websites. Adding a background image to your Next.js application can enhance the visual appeal of your web pages. This article will guide you through the process of adding a background image to a
3 min read
CSS - How To Set Opacity Of Background Image?
Here are the different ways to set the opacity of the background image 1. Using Opacity PropertyThe opacity property in CSS is a simple way to adjust the transparency of an element. You can set its value between 0 (completely transparent) and 1 (fully opaque). Syntax div { opacity: 0.5; /* Adjusts t
1 min read
How to Remove Background from image in CSS?
In web design, removing or hiding the image's background is a common task for better visuals or layering content. With CSS, developers can achieve this using various techniques depending on the requirement, such as making the background transparent or clipping out parts of the image. we will discuss
5 min read
Tailwind CSS Background Image
This class accepts more than one value in tailwind CSS. All the properties are covered in class form. It is the alternative to the CSS background-image property. This class is used to set one or more background images to an element. By default, it places the image on the top left corner. To specify
2 min read
How to apply background image with linear gradient using Tailwind CSS ?
In this article, we will see how to apply background images with a linear gradient using Tailwind CSS. Tailwind CSS is a highly customizable, utility-first CSS framework from which we can use utility classes to build any design. To apply background images with linear gradients we use the background
2 min read