Is it possible to have a child element behind his parent element with z-index

前端 未结 5 1176
逝去的感伤
逝去的感伤 2020-12-10 03:54

I would like to know if it possible to have a child element behind his parent element with z-index.

I would like to use the parent div as transparent color layer on

5条回答
  •  猫巷女王i
    2020-12-10 04:21

    Why not? Sure you can, and it's easy:

    1. give a non-static position to your desired elements;
    2. set z-index of child to -1;
    3. create a stacking context on the main container (by setting on it a z-index, opacity, transforms or whatelse generates a composite layer).

    .container {
        position: absolute;
        z-index: 0; /* or eg. opacity: 0.99;*/
      
        background-color: blue;
        color: lightblue;
        width: 100%;
        height: 100%;
        text-align: center;
    }
    
    .parent {
        position: relative;
      
        background-color: rgba(100, 255, 150, 0.75);
        color: green;
        width: 50%;
        height: 30%;
        top: 30%;
        left: 20%;
    }
    
    .child {
        position: absolute;
        z-index: -1;
      
        background-color: orange;
        color: yellow;
        width: 100%;
        height: 100%;
        top: -50%;
        left: 20%;
    }
    container
    parent
    child

    (if the parent is used as a transparent layer, be sure to use a background-image or rgba background-color: children inherit the opacity of the parent)

提交回复
热议问题