CSS I want a div to be on top of everything

前端 未结 6 1419
孤街浪徒
孤街浪徒 2020-12-04 16:21

How do I make an html div tag to be on top of everything?

6条回答
  •  囚心锁ツ
    2020-12-04 16:35

    Yes, in order for z-index to work, you'll need to give the element a position:absolute or a position:relative property.

    But... pay attention to parents!

    You have to go up the nodes of the elements to check if at the level of the common parent the first descendants have a defined z-index.

    All other descendants can never be in the foreground if at the base there is a lower definite z-index.

    In this snippet example, div1-2-1 has a z-index of 1000 but is nevertheless under the div1-1-1 which has a z-index of 3.

    This is because div1-1 has a z-index greater than div1-2.

    .div {
      
    }
    
    #div1 {
      z-index: 1;
      position: absolute;
      width: 500px;
      height: 300px;
      border: 1px solid black;
    }
    
    #div1-1 {
      z-index: 2;
      position: absolute;
      left: 230px;
      width: 200px;
      height: 200px;
      top: 31px;
      background-color: indianred;
    }
    
    #div1-1-1 {
      z-index: 3;
      position: absolute;
      top: 50px;
      width: 100px;
      height: 100px;
      background-color: burlywood;
    }
    
    #div1-2 {
      z-index: 1;
      position: absolute;
      width: 200px;
      height: 200px;
      left: 80px;
      top: 5px;
      background-color: red;
    }
    
    #div1-2-1 {
      z-index: 1000;
      position: absolute;
      left: 70px;
      width: 120px;
      height: 100px;
      top: 10px;
      color: red;
      background-color: lightyellow;
    }
    
    .blink {
      animation: blinker 1s linear infinite;
    }
    
    @keyframes blinker {
      50% {
        opacity: 0;
      }
    }
    
    .rotate {
      writing-mode: vertical-rl;
      padding-left: 50px;
      font-weight: bold;
      font-size: 20px;
    }
    div1
    z-index: 1
    div1-1
    z-index: 2
    div1-1-1
    z-index: 3
    div1-2
    z-index: 1
    <=
    z-index: 1000!!
    div1-2-1
    because =>
    (same
    parent)

提交回复
热议问题