CSS 如何动态创建图像的拼贴(没有间隙的网格),其中图像具有不同的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6541078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 00:54:40  来源:igfitidea点击:

How do I dynamically create a collage (grid with no gaps) of images, where images have different heights?

cssimagegridcss-floatgaps-in-visuals

提问by Homan

I want to display a page of thumbnails. The thumbnails are user uploaded artwork images. Each image has a constrained width, but not a constrained height, this is to show the full image without any cropping.

我想显示一页缩略图。缩略图是用户上传的艺术作品图像。每个图像都有一个受约束的宽度,但没有一个受约束的高度,这是为了显示没有任何裁剪的完整图像。

I want the images to butt up against each other with no gaps in between. The number of columns is not fixed: If I expand or resize the page the images should flow into correct number of columns.

我希望图像相互对接,中间没有间隙。列数不固定:如果我扩展或调整页面大小,图像应该流入正确的列数。

float:left

is ALMOST, what I want. Except when there are tall images in the page I get this gapped look:

几乎是我想要的。除非页面中有高大的图像,否则我会看到这种间隙:

enter image description here

在此处输入图片说明

How do I eliminate the gaps?

如何消除间隙?

采纳答案by Litek

If you want to do this in css only you have to create divs for every column, and float those columns (but it messes up order of images).

如果您只想在 css 中执行此操作,则必须为每一列创建 div,并浮动这些列(但它会打乱图像的顺序)。

Alternatively, you can use jquery plugin like this one.

或者,您可以像这样使用 jquery 插件。

回答by onefastsnail

Not sure how graceful you need your CSS, but a good option none the less. You could use masonry.js to help that.

不确定您需要多么优雅的 CSS,但仍然是一个不错的选择。你可以使用 masonry.js 来帮助它。

http://css-tricks.com/seamless-responsive-photo-grid/

http://css-tricks.com/seamless-responsive-photo-grid/