Overlay Divs Without Absolute Position

前端 未结 7 1418
旧时难觅i
旧时难觅i 2020-12-24 01:36

What follows is a long explanation, but it\'s the only way to effectively communicate the issue I\'m trying to resolve...

I am (somewhat desperately, and entirely un

相关标签:
7条回答
  • 2020-12-24 02:18

    Figured out an elegant solution for your problem without absolute positioning OR floats using flexbox — main advantage is that both div heights are respected when calculating parent height. Very useful for overlaying text on an image:

    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    }
    
    .container {
      max-width: 500px;
      /*border: 1px solid lime;*/
    }
    
    .card {
      display: flex;
      /*border: 1px solid red;*/
      overflow: hidden;
    }
    
    .box {
      position: relative;
      min-width: 100%;
      width: 100%;
      flex-basis: 100%;
      flex-grow: 1;
    }
    
    .box--image {
      z-index: 1;
      overflow: hidden;
    }
    
    .box--image.box--heightrestricted {
      max-height: 300px;
    }
    
    .box--text {
      z-index: 2;
      margin-left: -100%;
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      color: red;
    }
    
    .box--text h3 {
      margin: 0;
      padding: 1rem;
    }
    <div class="container">
    
      <button onclick="document.getElementById('imagebox').classList.toggle('box--heightrestricted')">toggle image max-height</button>
      <br>
      <br>
    
      <div class="card">
    
        <div id="imagebox" class="box box--image box--heightrestricted">
          <img src="https://placeimg.com/640/640/4" alt="" />
        </div>
    
        <div class="box box--text">
          <h3>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris in urna porta, maximus velit vitae, suscipit eros. Praesent eleifend est at nisi laoreet auctor. Nunc molestie fringilla magna et dictum. Nam ac massa nec erat consequat lacinia in in leo. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas sollicitudin nibh nisl, sed molestie lorem lobortis in. Nulla eget purus a risus scelerisque cursus. Praesent nisl dolor, varius eget faucibus sit amet, rutrum non lorem. Ut elementum sapien sed facilisis tempus. Morbi nec ipsum sed lacus vulputate sodales quis ut velit. Quisque id ante quis leo pharetra efficitur nec at mauris. Praesent dignissim hendrerit posuere. Vestibulum venenatis urna faucibus facilisis sodales. Suspendisse potenti. Proin a magna elit. Aenean vitae aliquam est, quis fringilla lectus.</h3>
        </div>
    
      </div>
    
    </div>

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