How to overlay one div over another div

前端 未结 9 1866
旧时难觅i
旧时难觅i 2020-11-21 17:39

I need assistance with overlaying one individual div over another individual div.

My code looks like this:

相关标签:
9条回答
  • 2020-11-21 18:03

    By using a div with style z-index:1; and position: absolute; you can overlay your div on any other div.

    z-index determines the order in which divs 'stack'. A div with a higher z-index will appear in front of a div with a lower z-index. Note that this property only works with positioned elements.

    0 讨论(0)
  • 2020-11-21 18:03

    The new Grid CSS specification provides a far more elegant solution. Using position: absolute may lead to overlaps or scaling issues while Grid will save you from dirty CSS hacks.

    Most minimal Grid Overlay example:

    HTML

    <div class="container">
      <div class="content">This is the content</div>
      <div class="overlay">Overlay - must be placed under content in the HTML</div>
    </div>
    

    CSS

    .container {
      display: grid;
    }
    
    .content, .overlay {
      grid-area: 1 / 1;
    }
    

    That's it. If you don't build for Internet Explorer, your code will most probably work.

    0 讨论(0)
  • 2020-11-21 18:15

    #container {
      width: 100px;
      height: 100px;
      position: relative;
    }
    #navi,
    #infoi {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
    }
    #infoi {
      z-index: 10;
    }
    <div id="container">
      <div id="navi">a</div>
      <div id="infoi">
        <img src="https://appharbor.com/assets/images/stackoverflow-logo.png" height="20" width="32" />b
      </div>
    </div>

    I would suggest learning about position: relative and child elements with position: absolute.

    0 讨论(0)
提交回复
热议问题