Mosaic of images HTML/CSS

后端 未结 10 1260
情书的邮戳
情书的邮戳 2020-11-29 03:53

I want to make an image layout with portrait images inside a div with a fixed aspect ratio of 3:2. The size of images is 327

相关标签:
10条回答
  • 2020-11-29 04:16

    First of all, to remove space between images, just remove set to '0' padding and margin.

    Then to achive what you want, you can use or combine those strategies:

    1) Assign a fixed size in pixel to one of the sizes: other one will scale automatically.

    2) You can calculate the size you need via javascript and assign the value dinamically. For example with jQuery framework:

    $(img).each(function(){
     var h = this.height();
     var w = this.width();
    
    
     if (w => h){
      this.attr('width', w*0.66);
    }
    else {
      this.attr('height',h*.066);
    }
    });
    

    3) You can use css background-image for divs and background-size: cover or background-size: contain as you need, statically or dinamically (w3c docs

    0 讨论(0)
  • 2020-11-29 04:17

    In my experience: if you only set the dimension of either height or the width (not both) the image will scale accordingly.

    0 讨论(0)
  • 2020-11-29 04:19
    img{
     height: auto;
     width: 50%
    }
    
    0 讨论(0)
  • 2020-11-29 04:25

    I would like to give simple solution.

    You can simply wrap img tag with DIV. And you should apply CSS to this wrapped DIV.

    Example Code

    Instead of this
    <img src='some_image.jpg'>
    
    Use below kind of structure
    <div class="img_wrapper">
         <img src='some_image.jpg'>
    </div>
    


    CSS

    //  suggestion: target IMG with parent class / ID
    .img_wrapper img{
        width: 100%; 
        height: auto;
    } 
    

    All images inside class .img_wrapper would have proper aspect ratio.

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