简体   繁体   English

浮动div不能正确包装

[英]Floated divs are not wrapping correctly

I have a product table with various categories. 我有一个具有各种类别的产品表。 In a teaser page, I show each category and between 2 to 5 product images belonging to that category. 在预告页面中,我显示了每个类别以及属于该类别的2至5个产品图片。

I want them displayed such that the category divs are floated to the left and the wrap around if a category div cannot fit into the same row because of width constraint. 我希望它们显示为使类别div浮动到左侧,如果类别div由于宽度限制而无法放入同一行,则将其环绕。 I have following CSS that I use for the same: 我有以下用于相同的CSS:

.prod-table
{
    display: table;
    float:left;
    border: 1px solid red;
    margin:5px;
    padding:5px;
}

.prod-cell
{
    display: table-cell;
    float:left;
    padding:5px;
}

.spread-image
{
    display: block;
    width: 100%;
    height: auto;   
    max-height: 200px;

}

The HTML is as follows: HTML如下:

<div class='spread-table'> <-- this loops for categories
    <div class='spread-cell'> <-- this loops for image in categories
        <div style='position:relative;'>
        <img src="{{image.url}}" class='spread-image'>
        </div>
    </div>
    <div class='clearfix'></div>
    <div style='text-align:center'><strong>View More</strong></div>
</div>

This seems to float the images correctly, however from time to time I get into a situation, where category div wraps to the next row, but does not start at the left end, but rather somewhere in the middle of the row. 这似乎使图像正确浮动,但是有时我会遇到这样的情况,类别div环绕到下一行,但不是从左端开始,而是在行的中间。 Like so: 像这样:

[---category 1---] [-category 2-] [----category 3----] [--category 4--]
                   [---category 5---] [-category 6-] [---category 7---]
[-----category 8----] [---category 9---] [-category A-] [---category B---]
                                         [-category C-] [-category D-] etc 

I have added the dashes to indicate that different categories have different widths. 我添加了破折号以指示不同的类别具有不同的宽度。 In above layout, you can see that the first and third rows are wrapping correctly, while the second and fourth are not. 在上面的布局中,您可以看到第一行和第三行正确包装,而第二行和第四行没有正确包装。

When category 5 & category C wraps incorrectly you can see that they align themselves to one of the category divs from their above row. category 5category C换行错误时,您会看到它们与上一行中的div类别之一对齐。 In this example category 5 aligns with second div in above row and category C with third div. 在此示例中, category 5与上一行的第二个div对齐, category C与第三个div对齐。

I guess this might be a small fix, something weird related to CSS, but I am not able to figure it out. 我想这可能是一个小的解决方案,与CSS相关,但我无法弄清楚。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

Take a look at this example: 看一下这个例子:

http://jsfiddle.net/x6fkm411/ http://jsfiddle.net/x6fkm411/

I demonstrates the behavior of float: left , if the elements don't have same heights. 我演示了float: left的行为,如果元素的高度不同。 It's the same behavior as you illustrate. 这与您说明的行为相同。 So check the actual heights of your elements 因此,请检查元素的实际高度

Also you combine float:left and display: table-cell in one CSS rule. 您还可以在一个CSS规则中将float:leftdisplay: table-cell组合在一起。 The display won't apply, but will be implicitly set to display: block . 该显示将不适用,但会隐式设置为display: block See here: http://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo 参见此处: http : //www.w3.org/TR/CSS21/visuren.html#dis-pos-flo

edit 编辑

To achieve what I think you want to achieve, try display: inline-block with a hack: http://jsfiddle.net/12vj3y5g/1/ or flex layout https://css-tricks.com/snippets/css/a-guide-to-flexbox/ 要实现我认为您想要实现的目标,请尝试display: inline-block有hack的代码: http : //jsfiddle.net/12vj3y5g/1/或弹性布局https://css-tricks.com/snippets/css/a -指南到flexbox /

edit2 编辑2

The float issue even applies, when there is only 1px in height difference 当高度差只有1像素时,甚至会出现浮动问题
See here http://jsfiddle.net/gtkp82pf/ 看到这里http://jsfiddle.net/gtkp82pf/

This is how I would do it (comments in code to help explain what I have done): 这就是我的做法(代码中的注释有助于解释我所做的事情):

 .table-wrap { display: inline-block; vertical-align:top; } .spread-table { display: table; border: 1px solid red; margin: 5px; padding: 5px; } .spread-row { display: table-row; } .spread-cell { display: table-cell; /* remove float:left - you cannot have both float:left and display:table-cell*/ padding: 5px; } .spread-image { display: block; height: auto; max-height: 200px; float: left; margin:10px; } strong.spread-cell { text-align: center } 
 <div class='table-wrap'> <!-- instead of floating the table, make it inline-block so the wrap is natural and if the heights are out you won't get the stacking issue --> <div class='spread-table'> <!-- direct children of display-table should only either all be display:table-row or all be display:table-cell, if you have a mixture of block and table-cell it will ruin the layout (think of mixing tds with divs) --> <div class='spread-row'> <!-- this needs to be a row to get the view more below --> <div class='spread-cell'> <!-- you can't loop this cell as css has no such thing as colspan and as you have a row below with only one cell in, this row can only have one cell --> <img src="http://lorempixel.com/800/800/city/1/" class='spread-image'> <img src="http://lorempixel.com/800/800/city/2/" class='spread-image'> <img src="http://lorempixel.com/800/800/city/3/" class='spread-image'> <img src="http://lorempixel.com/800/800/city/4/" class='spread-image'> <!-- loop the image instead --> </div> </div> <div class='spread-row'><strong class='spread-cell'>View More</strong></div> </div> </div> 

EDIT 编辑

As per HerrSerker's comments, remove table structure all together: 根据HerrSerker的评论,一起删除表结构:

 .category-wrap { display: inline-block; vertical-align:top; } .image-holder img { max-height: 200px; } .more { text-align: center; } 
 <div class='category-wrap'> <div class='image-holder'> <img src="http://lorempixel.com/800/800/city/1/" class='spread-image'> </div> <div class='more'><strong>View More</strong></div> </div> 

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

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