Have image within flexbox fill all vertical space and remain fully visible

前端 未结 1 2025
无人共我
无人共我 2021-01-24 20:43

I need to have an image to the left of a div, and have the image:

  • Be the same height as the div (which itself has non-fixed, content-dependent height);
  • Be
相关标签:
1条回答
  • 2021-01-24 21:02

    This is somehow impossible as the browser need at least two iterations to correctly calulate the final width and this may create a cycle. Basically the broswer will first ignore the percentage height of the image to set the width/height of the container then will resolve the percentage height and after will calculate the width of the image to keep the ratio but it won't go back to adjust the width of the container again because it may lead to an infinite loop in some cases, that's why you will simply have an overflow.

    Here is a hack that works only with Chrome where you can force the calculation of the width again by applying an animation.

    .container {
      display: inline-flex;
      background-color: green;
    }
    
    .image {
      background-color: purple;
    }
    
    .image img {
      display: block;
      height: 100%;
      animation:hack 1s infinite linear;
    }
    
    .right {
      width: 100px;
      height: 100px;
      background-color: yellow;
    }
    
    @keyframes hack {
      to {
        height:99.9%;
      }
    }
    <div class="container">
      <div class="image">
        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABOUlEQVRYhc2XPW6DQBCFP6bJEeLOMhKdS3dukOhT5FAcIAdJYfkKruzSnQvjCvsGSTUpdpEilMAiA7NPetXCvMfPzr5JlGBkwBbYAGtgBbz6tQdwBc7ACTgAl6Cq2s9c4UOhUtBAVv6evK9+12KqUCrUA4TbrH2NdKiBQmH/hHCbe18zyMCbwnFE8YZHX7vTQDGR+G8TxX8G0pFfe9fnSP8yUM4g3rBsG8j1ub99KGuvifh28A4sghrHOFh4TVDIdFiTGYuVQia49rqc8ekbLIGt4Hq7FTaCO1issBbcqWaFVaLwBbwYGfiW/mumheDChBUegksyVrgKLkZZ4Sy4DGeFk+AC5M1A/AYcBJdedwYGdsCl2YafwH1G8bvXjCeQRBHJzENpFLE8isEkitFstuE00fC9O8l4/gNz0YYudsoRxwAAAABJRU5ErkJggg==" >
      </div>
      <div class="right">right div</div>
    </div>

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