简体   繁体   English

如何在占用空白空间的CSS中制作网格画廊?

[英]How to make grid gallery in css that occupies empty spaces?

I want to make a kind of grid gallery. 我想做一个网格画廊。 I want it to be responsive so i thought to use float with percentage. 我希望它具有响应性,所以我认为使用带百分比的浮点数。

Float in css states it'll occupy empty spaces. 在CSS中浮动表示它将占用空白空间。 (I may be wrong, it might applicable for horizontal only not vertical). (我可能是错的,它可能仅适用于水平,不适用于垂直)。

I tried with this code: 我尝试使用以下代码:

css CSS

#wrapper {
    width:100%;
}
.blocks{
    width:25%;
    float:left;
    border:2px solid green;
    margin:5px;
}

Html HTML

<div id="wrapper">
    <div class="blocks">
        <img src="http://i.stack.imgur.com/DOPHF.jpg"/>
    </div>
     <div class="blocks">
        <img src="http://i.stack.imgur.com/ppuwO.jpg"/>
    </div>
     <div class="blocks">
        <img src="http://i.stack.imgur.com/t2QF9.jpg"/>
    </div>
    <div class="blocks">
        <img src="http://i.stack.imgur.com/MAVHu.jpg"/>
    </div>
    <div class="blocks">
        <img src="http://i.stack.imgur.com/XpHOP.jpg"/>
    </div>
</div>

JSfiddle 的jsfiddle

In the Jsfiddle there is a gap between image 1 and image 5. 在Jsfiddle中,图片1和图片5之间存在间隙。

Why it is happening how to occupy the empty space. 为什么发生这种情况如何占用空白空间。

Note: image sizes are changeable so it should dynamically adjust.No need for hardcode. 注意:图片大小是可变的,因此应该动态调整。无需硬编码。

Any other way to make grid gallery? 还有其他方法可以制作网格画廊吗? What is the problem here? 这里有什么问题?

If you want it to work responsively, you'll want 如果您希望它能以响应方式工作,那么您会想要

display:inline;

for this, not 为此,不是

float:left;

Although that solution will need some additional work, so I have another suggestion. 尽管该解决方案需要其他工作,但是我还有另一个建议。

I think you might be interested in Twitter Bootstrap since it is oriented around responsive design and has dedicated grid layouts just for things like this. 我认为您可能对Twitter Bootstrap感兴趣,因为它是围绕响应式设计而设计的,并且具有专门用于此类事情的网格布局。

If you have variable height/width blocks, getting a CSS solution which can accommodate any combination will prove frustratingly elusive. 如果您有可变的高度/宽度块,那么获得可容纳任何组合的CSS解决方案将令人望而却步。 You'll likely be best going down a JS route with something like jQuery Masonry 您可能最好沿着jQuery Masonry之类的JS路线走

Alternatively you can always crop/resize the content of your tiles, and use something like this 另外,您随时可以裁剪/调整图块的内容大小,并使用类似的方法

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

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