简体   繁体   English

动态网格,使流体箱高度相同

[英]Dynamic grid, make fluid boxes same height

I am trying to create a dynamic grid which contains tiles - there is one row and the grid columns have a % width. 我正在尝试创建一个包含图块的动态网格-有一行,网格列的宽度为%。

At larger resolutions, the width might be 30% but at lower resolutions it might be 50% or even 100% - they are all floated left and this works fine for widths. 在较大的分辨率下,宽度可能为30%,但在较低的分辨率下,宽度可能为50%甚至100%-它们都向左浮动,因此对于宽度来说效果很好。

The issue is when one of the columns has more content than the others hence is taller and it messes up the grid system and user could end up with row of 3 then row of 1 then row of 2. 问题在于,当其中一列的内容比其他列的内容更多时,则该列更高,并且弄乱了网格系统,用户最终可能会遇到3行,然后是1行,然后是2行。

I can't use min/max height in CSS as, depending on the user, the div might have 10 words or 1000 words. 我不能在CSS中使用最小/最大高度,因为div取决于用户,div可能有10个单词或1000个单词。 The "row" is actually a wrapper div as it incorporates multiple "rows" so setting a height on that doesn't work either. “行”实际上是包装器div,因为它合并了多个“行”,因此在其上设置高度也不起作用。

How can I create a dynamic responsive fluid grid layout using something like the below where all widths are the same and all heights are the same as the highest box - I don't mind using JS/jQuery if it is needed which I suspect it will be. 如何使用如下所示的内容创建动态响应式流体网格布局,其中所有宽度均相同,所有高度均与最高框相同-我不介意使用JS / jQuery(如果需要),我怀疑它将是。

<div id="row">
    <div class="col">COL1</div>
    <div class="col">COL2 Text added to this column so height is different</div>
    <div class="col">COL3</div>
    <div class="col">COL4</div>
    <div class="col">COL5</div>
    <div class="col">COL6</div>
</div>

#row {
    float:left;
    border:1px solid red;
}

.col { float:left;
width:30%; border:1px solid black; margin:1%;}

@media screen and (max-width: 400px) {
    .col {width:45%;}
}

http://jsfiddle.net/8bnqbpnr/ http://jsfiddle.net/8bnqbpnr/

You could use the :nth-child pseudo-selector to force clear:left on specific columns and media queries for responsiveness: http://jsfiddle.net/mpartarrieu/109gjy37/ 您可以使用:nth-​​child伪选择器在特定列和媒体查询上强制clear:left以提高响应速度: http : //jsfiddle.net/mpartarrieu/109gjy37/

<div id="row">
    <div class="col">COL1</div>
    <div class="col">COL2 Text added to this column so height is different</div>
    <div class="col">COL3</div>
    <div class="col">COL4</div>
    <div class="col">COL5</div>
    <div class="col">COL6</div>
    <div class="col">COL7</div>
    <div class="col">COL8</div>
    <div class="col">COL9</div>
    <div class="col">COL10</div>
    <div class="col">COL11</div>
    <div class="col">COL12</div>
    <div class="col">COL13</div>
</div>

#row {
    float:left;
    border:1px solid red;
}

.col { float:left;
width:30%; border:1px solid black; margin:1%;}

@media screen and (min-width: 401px) {
    #row .col:nth-child(3n+1) {  
        clear: left;
    }
}

@media screen and (max-width: 400px) {
    .col {width:45%;}
    #row .col:nth-child(2n+1) {  
        clear: left;
    }

}

You can fix this using flexbox . 您可以使用flexbox修复此flexbox I'm quite fond of flexbox myself :) Just remember, if your target audience will use IE8 or older, that you can't use this. 我本人非常喜欢flexbox :)请记住,如果您的目标受众将使用IE8或更早版本,则不能使用它。

 .container { width: 400px; margin: 32px auto; } .row { border: 1px solid red; display: flex; width: 400px; flex-wrap: wrap; justify-content: space-between; } .col { box-sizing: border-box; padding: 10px; border: 1px solid black; margin: 5px; width: 30%; } 
 <div class="container"> <div class="row"> <div class="col">COL1</div> <div class="col">COL2 Text added to this column so height is different</div> <div class="col">COL3</div> <div class="col">COL4</div> <div class="col">COL5</div> <div class="col">COL6</div> </div> </div> 

There is a simple solution for this which even works for IE 6. All you need to do is to replace float:left to display:inline-block . 有一个简单的解决方案,甚至适用于IE6。您所需要做的就是将float:left替换为display:inline-block Here is a jFiddle . 这是一个jFiddle However, there are 2 things to mention: 但是,有两件事要提及:

  1. Make it work for IE6 The display:inline-block property only works for inline elements. 使它适用于IE6 display:inline-block属性仅适用于内联元素。 Thus instead of div you may want to use a span element if you want to support IE 6. 因此,如果要支持IE 6,则可能要使用span元素而不是div

  2. Delete Whitespaces Since your elements are now inline, the whitespace between your span's are displayed. 删除空格由于您的元素现在已内联,因此将显示跨度之间的空格。 To remove them and keep a clean code you could write it like that: 要删除它们并保留干净的代码,可以这样编写:

    <span class="col">COL1</spanasd><!-- remove whitespace --><span class="col">...</span>

Maybe interesting to read: Advantages of using display:inline-block vs float:left in CSS . 也许读起来很有趣:在CSS中使用display:inline-block与float:left的优点

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

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