z-index doesn’t work because?

Asked

Viewed 1,601 times

0

I have this very simple code, my div#d4 is coming out of your container and falling into the div#d2 this because the div daughters do not fit in the div father. And by defining a position: absolute for her. She appears again, but because in this case the z-index does not work ?

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        #d1, #d2, #d3, #d0, #d4 {
            width: 200px;
            height: 200px;
            color: white;
        }

        #d0 {
            background-color: aqua;
        }

        #d1 {
            background-color: tomato;
        }

        #d2 {
            background-color: green;
        }

        #d3 {
            background-color: blue;
        }

        #d4 {
            background-color: yellow;
            z-index: 1;
        }

    </style>
</head>
<body>

<div id="d1">
    content1
    <div id="d0">
        content0
    </div>
    <div id="d4">
        content4
    </div>
</div>

<div id="d2">
    content2
</div>

<div id="d3">
    content2
</div>

</body>
</html>

3 answers

2

The property z-index only works on elements where the property position is present and at one of the following values::

  • position: Absolute
  • position: relative
  • position: Fixed
  • position: Sticky

In your code it should be:

#d1, #d2, #d3, #d0, #d4 {
    width: 200px;
    height: 200px;
    color: white;
    position: relative; /* Propriedade position aqui */
}

2

In fact things are connected to the type of container or scope of the element. If the element is inside a container flex, or container grid, you don’t need to declare the position to the z-index work. But that might depend on what you want with the final layout... and maybe you don’t even need the position

Code in a Container GRID

#d1, #d2, #d3, #d0, #d4 {
    width: 200px;
    height: 200px;
    color: white;
}

#d0 {
    background-color: aqua;
}

#d1 {
    background-color: tomato;

    /* container GRID */
    display: grid;
}

#d2 {
    background-color: green;
}

#d3 {
    background-color: blue;
}

#d4 {
    background-color: yellow;
    z-index: 1;
}
<div id="d1">
    content1
    <div id="d0">
        content0
    </div>
    <div id="d4">
        content4
    </div>
</div>

<div id="d2">
    content2
</div>

<div id="d3">
    content2
</div>


Now in a Container Flex

#d1, #d2, #d3, #d0, #d4 {
    width: 200px;
    height: 200px;
    color: white;
}

#d0 {
    background-color: aqua;
}

#d1 {
    background-color: tomato;

    display: flex;
    flex-wrap: wrap;
}

#d2 {
    background-color: green;
}

#d3 {
    background-color: blue;
}

#d4 {
    background-color: yellow;
    z-index: 1;
}
<div id="d1">
    content1
    <div id="d0">
        content0
    </div>
    <div id="d4">
        content4
    </div>
</div>

<div id="d2">
    content2
</div>

<div id="d3">
    content2
</div>

-1

For any over position should be placed the type of position or for z-index to work correctly put a position: as relative, Fixed or nesmo Absolute.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.