The Wayback Machine - https://web.archive.org/web/20241130211611/https://www.geeksforgeeks.org/css-z-index-property/
Open In App

CSS z-index Property

Last Updated : 27 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS z-index property controls which elements appear in front or behind others when they overlap. An element with a higher z-index value will appear on top, while one with a lower value will be behind.

Note:

The z-index property in CSS controls the stacking order of elements. It only applies to positioned elements (using absolute, relative, fixed, or sticky) and flex items within a display: flex container.

Syntax

z-index: auto | number | initial | inherit;

Property values

These are the values of the z-index property and their descriptions:

ValueDescription
autoThe stack order is equal to that of the parent (default).
numberThe stack order depends on the number.
initialSets the property to its default value.
inheritInherits the property from the parent element.

Examples of CSS z-index Property

Example 1: Layering Elements with z-index

In this example, we demonstrate the z-index property. An image with z-index: -1 is positioned behind the <h1> and <p>, which have a green background, creating layered content.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        z-index Property
    </title>
    <style>
        img {
            position: absolute;
            left: 0px;
            top: 0px;
            z-index: -1;
        }

        h1,
        p {
            background-color: green;
        }
    </style>
</head>

<body>

    <h1>GeeksforGeeks</h1>
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/geek.png" 
         width="400" 
         height="150">
    <p>This example shows the use of z-index property.</p>
</body>

</html>

Output: 

Example 2: Controlling Stacking Order with z-index

In this example, we demonstrate z-index. The image is positioned absolutely with z-index: 1, while <h1> and <p> are positioned relatively with z-index: 0, ensuring the text is behind the image.

html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>z-index Property Example</title>
    <style>
        img {
            position: absolute;
            left: 0;
            top: 0;
            z-index: 1;
            /* z-index value set to 1 */
        }

        h1,
        p {
            position: relative;
            /* Ensure z-index works */
            background-color: green;
            z-index: 0;
            /* Ensure text is behind the image */
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/geek.png" 
        width="400" 
        height="150"
        alt="GeeksforGeeks Logo">
    <p>This example shows the use of z-index property.</p>
</body>

</html>

Output: 

z-index

CSS Z-index property example output

The CSS z-index property is essential for controlling the stacking order of elements on a webpage. By understanding and using z-index, you can create complex, layered layouts that enhance the visual appeal and functionality of your web design.

Supported Browsers

The browser supported by z-index property are listed below:

Note: The z-index property is widely supported, ensuring consistent behavior across modern browsers. Even older versions of these browsers support this property, making it reliable for most web designs.

CSS z-index Property – FAQs

What does the z-index property control in CSS?

The z-index property controls the stacking order of elements along the z-axis (depth), determining which elements appear in front or behind others.

What is the highest z-index?

Technically, there is no defined highest z-index value in CSS, but browsers may have their limits based on system memory.

When should I use the z-index?

Use the z-index property when elements overlap and you want to control which one appears in front or behind another.

Does z-index work without position being set?

No, z-index only works on elements with a position value of relative, absolute, fixed, or sticky.

What is the default stacking order of elements without z-index?

Without z-index, elements are stacked in the order they appear in the HTML, with later elements overlapping earlier ones.



Previous Article
Next Article

Similar Reads

three90RightbarBannerImg