简体   繁体   English

如何用不同尺寸的图像创建网格?

[英]How create grid out of images of different sizes?

I'm trying to create a grid of images where all images of a row share the same height and where each row uses the same width. 我正在尝试创建一个图像网格,其中一行的所有图像共享相同的高度,并且每行使用相同的宽度。

How can I do this and what libraries can help me? 我该怎么做以及哪些图书馆可以帮助我?

图像网格

This type of grid are difficult to make by yourself so its better to not reinvent the wheel and use awesome libraries created by awesome people on the internet. 这种类型的网格很难自己制作,因此最好不要重新发明轮子并使用互联网上令人敬畏的人创建的真棒库。

Checkout this links which are best for what you are looking for -- > 查看此链接最适合您所需的内容 - >

  1. http://masonry.desandro.com/ http://masonry.desandro.com/
  2. http://css-tricks.com/seamless-responsive-photo-grid/ http://css-tricks.com/seamless-responsive-photo-grid/

Also this link http://www.jqueryscript.net/tags.php?/grid%20layout/ has a variety of those image grid view libraries which may be useful .. 另外这个链接http://www.jqueryscript.net/tags.php?/grid%20layout/有各种各样的图像网格视图库,可能有用..


The CSS trick link is, in fact, a library free easy implementation. 事实上,CSS技巧链接是一个免费实现。 The idea is to set images width to 100% and divide your space into columns. 我们的想法是将图像宽度设置为100%并将空间划分为列。

Here is a snippet extracted from the previous link: 以下是从上一个链接中提取的代码段:

 function getRandomSize(min, max) { return Math.round(Math.random() * (max - min) + min); } var allImages = ""; for (var i = 0; i < 25; i++) { var width = getRandomSize(200, 400); var height = getRandomSize(200, 400); allImages += '<img src="https://placekitten.com/' + width + '/' + height + '" alt="pretty kitty">'; } photos.innerHTML = allImages 
 .snippet-result-code { height: 500px } #photos { /* Prevent vertical gaps */ line-height: 0; -webkit-column-count: 5; -webkit-column-gap: 0px; -moz-column-count: 5; -moz-column-gap: 0px; column-count: 5; column-gap: 0px; } #photos img { /* Just in case there are inline attributes */ width: 100% !important; height: auto !important; } @media (max-width: 1200px) { #photos { -moz-column-count: 4; -webkit-column-count: 4; column-count: 4; } } @media (max-width: 1000px) { #photos { -moz-column-count: 3; -webkit-column-count: 3; column-count: 3; } } @media (max-width: 800px) { #photos { -moz-column-count: 2; -webkit-column-count: 2; column-count: 2; } } @media (max-width: 400px) { #photos { -moz-column-count: 1; -webkit-column-count: 1; column-count: 1; } } body { margin: 0; padding: 0; } 
 <section id="photos"></section> 

CSS Grid frameworks work well. CSS Grid框架运行良好。 You can find a more detailed explanation on the website CSS Tricks . 您可以在网站CSS Tricks上找到更详细的解释。 This is an example that could work for the images above. 这是一个可以用于上述图像的示例。 (And here is my JSFiddle result). (这是我的JSFiddle结果)。

/*grid container*/

 .container {

          display: grid;
          padding:60pt;
          grid-template-columns: 25% 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 25%;  
          /*adjust outer values if you want less padding on the sides, 
           in jsfiddle I used 5% */
          grid-template-rows: 10% 300pt 250pt 10%;
          grid-column-gap: 10px;
          grid-row-gap: 5px;

        }

    .container div img { width: 100%;
                         height: 100%;
                         }

/*image div classes*/

    .main_1 {
      grid-area: main_1;
      grid-column:2/5;
      grid-row: 2/3; 
    }

    .main_2 {

      grid-area: main_2;
      grid-column:5/8;
      grid-row: 2/3;
    }

    .main_3 {
      grid-area: main_3;
      grid-column:8/11;                                          
      grid-row: 2/3;
    }

    .main_4 {
      grid-area: main_1;
      grid-column:2/4;
      grid-row: 3/4;
    }

    .main_5 {
      grid-area: main_2;
      grid-column:4/7;
      grid-row: 3/4;
    }

    .main_6 {
      grid-area: main_3;
      grid-column:7/11;                                           
      grid-row: 3/4; 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM