垂直对齐具有不同高度的浮动 DIV?
请参阅我的JSfiddle 以了解当前发生的情况
我有一些内容高度不同的 div 。宽度始终相同。
每行应该包含两列,但我希望一行中每个 div 的开头都处于相同的高度。与 jQuery Masonry 插件相比,所有“砖块”都挤在一起以删除空格。
有什么好的跨浏览器方法来实现这一目标?我的想法是清除每第二个 n 个孩子的浮动,但我相信 IE 不支持这个?
我相信我也可以用 jQuery 做一些事情,但是它整洁吗?我是否能学到一些东西以便下次问题发生时解决它?
HTML:
<div id="a" class="box"> Some content<br />Div A</div>
<div id="b" class="box"> Some content<br />Div B</div>
<div id="c" class="box"> Some content<br />Div C</div>
<div id="d" class="box"> Some content<br />Div D</div>
<div id="e" class="box"> Some content<br />Div E</div>
<div id="f" class="box"> Some content<br />Div F</div>
CSS:
.wrapper { width: 444px; }
.box {
width: 200px;
border: 1px solid #333;
float: left;
margin: 0 10px;
}
#a { height: 200px; }
#b { height: 180px; }
#c { height: 100px; }
#d { height: 80px; }
#e { height: 50px; }
#f { height: 50px; }
See my JSfiddle to see what currently happens
I have some divs with content of varying height. The width is always the same.
Each row should contain two columns, but I would like the start of each div in a row to be at the same height. Contrast this to say the jQuery Masonry plugin, where all the "bricks" are squeezed together to remove spaces.
What's a good, cross-browser way to achieve this? My idea was to clear the float for every 2nd nth child, but I believe IE doesn't support this?
I believe I could also do something with jQuery, but is it tidy? Will I have learnt anything in order to fix it next time the problem occurs?
HTML:
<div id="a" class="box"> Some content<br />Div A</div>
<div id="b" class="box"> Some content<br />Div B</div>
<div id="c" class="box"> Some content<br />Div C</div>
<div id="d" class="box"> Some content<br />Div D</div>
<div id="e" class="box"> Some content<br />Div E</div>
<div id="f" class="box"> Some content<br />Div F</div>
CSS:
.wrapper { width: 444px; }
.box {
width: 200px;
border: 1px solid #333;
float: left;
margin: 0 10px;
}
#a { height: 200px; }
#b { height: 180px; }
#c { height: 100px; }
#d { height: 80px; }
#e { height: 50px; }
#f { height: 50px; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 display:inline-block
http://jsfiddle.net/Awg3u/1/
You can use display:inline-block
http://jsfiddle.net/Awg3u/1/
这就是你的想法吗?它确实使用了 jQuery 并添加了一些额外的标记 - 不确定这是否可以接受。
http://jsfiddle.net/Awg3u/7/
我只在 Chrome/FF 中进行了测试,所以到目前为止,还没有在 IE 中检查过。
Is this what you had in mind? It does use jQuery and adds a bit of extra markup - not sure if that's acceptable.
http://jsfiddle.net/Awg3u/7/
I've only tested in Chrome/FF so far, and haven't checked in IE yet.
您基本上想要找到每个盒子的最大高度,然后取最大的并将所有其他盒子设置为该高度。
应该有任意数量的 jQuery 插件可以做到这一点,或者你可以推出自己的插件
You basically want to find the maximum height for each box and then take the largest and set every other box to that height.
There should be any number of jQuery plugins that can do this or you can roll your own
不确定我是否正确理解你想要什么。
如果不对请纠正我。
如果您希望行的所有第一个 div 具有相同的高度(基于第一个 div),您可以执行以下操作:
http://jsfiddle.net/Awg3u/3/
Not sure if I understand what you want correctly.
If not correct me please.
If you want the all the first divs of the rows to have the same height (based on the first div) you could do something like:
http://jsfiddle.net/Awg3u/3/
您可以在每 2 个块之后添加清除 div,如下所示:
或者您可以将行包装在新的 DIV 中。
You can add clearing div after every 2 blocks as follows:
Or you can wrap your rows in a new DIV.