How to Set z-index Value in React JS ?
Last Updated :
24 Apr, 2025
This article explores setting the z-index attribute in React, a CSS property influencing the stacking order of HTML elements for visibility control. In React, adjusting the z-index attribute enables effective management of stacking contexts within the application.
We will discuss the following two approaches to set z-index values in React.
Prerequisites:
Syntax:
element {
z-index: value;
};
Property Value:
- element: The HTML element to which you want to apply the z-index.
- value: An integer value that determines the stacking order. Higher values bring elements to the front.
Steps to Create the React Application And Installing Module:
Step 1: Create a react application by using this command
npx create-react-app <<My-Project>>
Step 2: After creating your project folder, i.e. reactProject-app, use the following command to navigate to it:
cd <<My-Project>>
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}
Approach 1: Inline Style
In this approach, we employ inline styles in React to directly configure the z-index property within a component. The process entails creating a JavaScript object that holds the desired z-index value and subsequently applying it as a style attribute to the relevant component.
Example: In this example we are using the above-explained approach.
JavaScript
import React from 'react';
function App() {
const containerStyle = {
position: 'relative',
textAlign: 'center',
fontFamily: 'Arial, sans-serif',
left: '30%',
top: "30%",
position: "absolute",
};
const imgStyle = {
position: 'absolute',
left: '0',
top: '0',
zIndex: -1,
// Lower z-index value
boxShadow: '0px 0px 10px 0px black',
borderRadius: "10px",
width: 400,
height: 400,
};
const headingStyle = {
zIndex: 1,
// Higher z-index value
padding: '10px',
borderRadius: '5px',
margin: '0',
textShadow: '2px 2px 4px white',
};
const textStyle = {
margin: '10px',
textShadow: '2px 2px 4px white',
};
return (
<div style={containerStyle}>
<h1 style={headingStyle}>
The z-index Property
</h1>
<img
src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230816223829/geeksgforgeeks-logo-1.png"
width="100"
height="140"
style={imgStyle}
alt="Image"
/>
<p style={textStyle}>
The image is going to be positioned
behind the heading because it has
a z-index of -1.
</p>
</div>
);
}
export default App;
Step to run the application:Open the terminal and type the following command.
npm start
Output:

Approach 2: CSS Modules
This method involves using CSS Modules to separate component styles from logic. Styles are defined in dedicated CSS files, and React components import and apply them using dynamically generated class names, promoting modularity and preventing conflicts with global CSS for improved maintainability.
Example: In this example we are using the above-explained apporach.
JavaScript
import React from 'react';
import styles from './MyComponent.module.css';
function App() {
return (
<div className={styles.container}>
<h1 className={styles.heading}>
The z-index Property
</h1>
<img
src=
"https://media.geeksforgeeks.org/wp-content/uploads/20230816223732/geeksgforgeeks-logo.jpg"
className={styles.image}
alt="Image"
/>
<p className={styles.text}>
The image is positioned behind
the heading because it has a
z-index of -1.
</p>
</div>
);
}
export default App;
CSS
/* MyComponent.module.css */
.container {
position: absolute;
text-align: center;
font-family: 'Arial, sans-serif';
left: 30%;
top: 30%;
}
.image {
position: absolute;
left: 0;
top: 0;
z-index: -1;
width: 410px;
height: 400px;
}
.heading {
z-index: 1;
padding: 20px;
border-radius: 5px;
margin: 0;
text-shadow: 2px 2px 4px white;
color: white;
}
.text {
margin: 10px;
text-shadow: 2px 2px 4px white;
color: red;
padding: 20px;
font-size: 20px;
}
Output:

Similar Reads
How to Use Z-Index In React Native ?
The CSS property called z-Index allows us to control the stacking order of elements on a web page. This concept works similarly in React Native, where the z-Index attribute enables the specifÂication of element display order on the screen. By assigning a higher value to an element's z-index, Element
4 min read
How to add Input Slider in React JS ?
We will learn how to add an input slider in React JS. React is a free and open-source front-end JavaScript library for building user interfaces or UI components. It was introduced by Facebook and a community of individual developers and companies. Prerequisites:Node JS or NPMReact JSReact useStateAp
2 min read
How to use styles in ReactJS ?
React is a popular JavaScript library for building single-page applications (SPAs) with dynamic user interfaces. Styling in React can be done in various ways, each with its advantages. In this article, we will explore different approaches to applying styles in React, including inline styles, CSS fil
4 min read
How to use react-window Module in React JS?
React window works by only rendering part of a large data set. This module is very easy to integrate and reduces the amount of time which generally takes a while to render. This module avoids the over-allocation of DOM nodes. We can use the react-window module in React JS using the following approac
2 min read
How to Set Box Shadow in React JS ?
The CSS property box-shadow is used to set box shadow in React. This can apply to any React.js component. The CSS box-shadow property creates shadow effects around an element's frame possible to set several effects at once by separating them with commas. A box shadow is defined by the element's X an
3 min read
How to Zoom in/Zoom out using React-PDF in React JS ?
Zooming in or out in React-PDF within a React JS application is achieved by using the 'scale' prop inside the `<Page />` component. The 'scale' prop accepts decimal values; values greater than 1 zoom in, while values less than 1 zoom out the page. Prerequisites:React JSReact Functional Compone
2 min read
How to use z-index in svg elements ?
The z-index only works on the complete content. This is because the HTML rendered controls the positioning before handing off to the SVG rendered to place the internal SVG contents. So, basically there is no z-index for SVG, it uses the painters model. Painter's model: According to this model, paint
3 min read
How to lift state two levels up in ReactJS ?
We want to lift the state two levels up such that when an event occurs at the lower level component then it should affect the state two levels up. For example, there are three components X, Y, and Z. X is the parent of Component Y, and Component Y is the parent of Component Z. Prerequisites:NodeJS o
2 min read
How to use react-countup module in ReactJS ?
While displaying some count count-up animation enhances the appearance with an increasing count value on the web page. To display count up the animation of numbers we need to use the react-countup module in ReactJSPrerequisites:NPM & Node.jsReact JSApproach:Using react-countup module in React JS
2 min read
How to use Bootstrap in React JS ?
Explore the seamless integration of Bootstrap into React JS for enhanced styling and responsive web development. This comprehensive guide provides step-by-step instructions, covering the installation process, utilization of Bootstrap components, and best practices to harness the power of both techno
3 min read