Auto Resize Image in CSS FlexBox Layout and keeping Aspect Ratio?

后端 未结 8 839
长情又很酷
长情又很酷 2020-12-12 12:34

I\'ve used the CSS flex box layout which appears as shown below:

\"enter

If t

相关标签:
8条回答
  • 2020-12-12 13:15

    I am using jquery or vw to keep the ratio

    jquery

    function setSize() {
        var $h = $('.cell').width();
        $('.your-img-class').height($h);
    }
    $(setSize);
    $( window ).resize(setSize); 
    

    vw

     .cell{
        width:30vw;
        height:30vw;
    }
    .cell img{
        width:100%;
        height:100%;
    }
    
    0 讨论(0)
  • 2020-12-12 13:23

    HTML:

    <div class="container">
        <div class="box">
            <img src="http://lorempixel.com/1600/1200/" alt="">
        </div>
    </div>
    

    CSS:

    .container {
      display: flex;
      flex-direction: row;
      justify-content: center;
      align-items: stretch;
    
      width: 100%;
      height: 100%;
    
      border-radius: 4px;
      background-color: hsl(0, 0%, 96%);
    }
    
    .box {
      border-radius: 4px;
      display: flex;
    }
    
    .box img {
      width: 100%;
      object-fit: contain;
      border-radius: 4px;
    }
    
    0 讨论(0)
提交回复
热议问题