How to create overlapping div in react js?

Hi Friends πŸ‘‹,

Welcome To aHoisting!

To create overlapping div in react js, you can set position: absolute; in .overlap and set z-index: 2; in .overlap-2. It will create overlapping div in react js.

Today, I am going to show you, how to create overlapping div in react js.

Table of contents

  • Install and create a new React app.
  • Import react component.
  • Create a Component.

Let’s start with the first step.

Step 1: Install and create a new React app.

First you have to install the React project. You should use create-react-app command to create a new React project.

npx create-react-app my-app
cd my-app
npm start

Step 2: Import react component.

After installing, you have to import your React component.

import React from "react";
import "./App.css";

Step 3: Create a Component.

You can use className="container" to create overlapping div in react js.

<div className="container">
  <div className="overlap overlap-1">Div 1</div>
  <div className="overlap overlap-2">Div 2</div>
  <div className="overlap overlap-3">Div 3</div>
</div>

Create overlapping div example.

The below code is an example of a React. You have to import react and set set position: absolute; in .overlap and set z-index: 2; in .overlap-2 to create overlapping div in react js.

App.js

import React from "react";
import "./App.css";

const App = () => {
  return (
    <div className="container">
      <div className="overlap overlap-1">Div 1</div>
      <div className="overlap overlap-2">Div 2</div>
      <div className="overlap overlap-3">Div 3</div>
    </div>
  );
};

export default App;

App.css


/* Container to position elements */
.container {
  position: relative; /* Establish a positioning context */
  width: 400px;
  height: 400px;
  margin: 20px auto; /* Center the container */
  background-color: #f0f0f0; /* Light background for contrast */
  border: 2px solid #ccc;
}

/* Base styles for overlapping divs */
.overlap {
  position: absolute; /* Allows positioning relative to the container */
  width: 150px;
  height: 150px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-weight: bold;
  font-size: 16px;
  border-radius: 10px; /* Optional: Adds rounded corners */
}

/* Position and color each div */
.overlap-1 {
  background-color: #ff6b6b; /* Red */
  top: 50px;
  left: 50px;
  z-index: 3; /* On top */
}

.overlap-2 {
  background-color: #4dabf7; /* Blue */
  top: 100px;
  left: 100px;
  z-index: 2; /* Middle layer */
}

.overlap-3 {
  background-color: #51cf66; /* Green */
  top: 150px;
  left: 150px;
  z-index: 1; /* Bottom layer */
}

/* Optional: Add hover effect */
.overlap:hover {
  transform: scale(1.1); /* Enlarge on hover */
  z-index: 5; /* Bring to front */
  transition: transform 0.3s ease, z-index 0.3s ease;
}

In the above code example, I have used the use position: absolute; to create overlapping div in react js.

Check the output of the above code.

React, create, overlapping, div

All the best πŸ‘