Search code examples
htmlcssz-index

Why is a nested z-index not working


I have a overlay, which i want to display with some information. However, there is a button below the overlay which I want to display over the overlay. I have set the overlay with a z-index of 9998 and the button below it with a z-index of 9999, but it doesn't work. Is this possible at all?

.overlay-message {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    display: table;
    background-color: rgb(0, 0, 0);
    opacity: .9;
    z-index: 9998;
}
h4 { display: table-cell;
color: #fff;text-align: center;
vertical-align: middle; }
.container {
    align-items: center;
    width: 100%;
    max-width: 100%;
    height: 100%;
}
.row {
    display: table !important;
    align-items: center;
    justify-content: center;
}
    .one-fifth-flex {
        width: 50%;
        padding-bottom: 20px;
        float: left;
    }

    .ico-btn {
        background-color:transparent;
        color:rgb(26, 188, 156);
        border-color:rgb(26, 188, 156);
        border-style: solid;
        border-width: 10px;
        width:100px;
        z-index: 9999;
}
<div class="overlay-message">
      <h4>Hey! Tap on the button above!</h4>
</div>
<div class="container">
    <div class="row">
        <div class="one-fifth-flex">
            <div role="button"  class="ico-btn">
                Me
            </div>
        </div>
    </div>
</div>


Solution

  • You need to set the position property to relative, absolute or fixed on the button for z-index to be considered:

    .overlay-message {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        display: table;
        background-color: rgb(0, 0, 0);
        opacity: .9;
        z-index: 9998;
    }
    h4 { display: table-cell;
    color: #fff;text-align: center;
    vertical-align: middle; }
    .container {
        align-items: center;
        width: 100%;
        max-width: 100%;
        height: 100%;
    }
    .row {
        display: table !important;
        align-items: center;
        justify-content: center;
    }
        .one-fifth-flex {
            width: 50%;
            padding-bottom: 20px;
            float: left;
        }
    
        .ico-btn {
            position: relative;
            background-color:transparent;
            color:rgb(26, 188, 156);
            border-color:rgb(26, 188, 156);
            border-style: solid;
            border-width: 10px;
            width:100px;
            z-index: 9999;
    }
    <div class="overlay-message">
          <h4>Hey! Tap on the button above!</h4>
    </div>
    <div class="container">
        <div class="row">
            <div class="one-fifth-flex">
                <div role="button"  class="ico-btn">
                    Me
                </div>
            </div>
        </div>
    </div>