使用最小高度浮动的 2 x 2 div 网格
我有 4 个 div 设置为 float:left;
,每个 div 的 width: 400px;
位于 div
的 width:800px;< /代码>。目前,这会创建一个由良好对齐的 div 组成的 2 x 2 网格,我设置了
min-height: 150px;
属性,因为我需要允许 div 在添加或删除新内容时展开。
如果添加新内容,则 div 会展开,这会弄乱我当前拥有的良好对齐的 2 x 2 网格 div,因为高度不均匀,这附近是否存在?
<div class="boxContainer">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
.boxContainer{
width: 800px;
height: auto;
}
.box{
float: left;
min-height: 150px;
width: 400px;
}
I have 4 divs set to float:left;
each with a width: 400px;
within a div
with width:800px;
. This currently creates a 2 x 2 grid of nicely aligned divs I set a property of min-height: 150px;
because I need to allow divs to expand as new content is added or removed.
If new content is added a div is expanded this messes up the nicely aligned 2 x 2 grid div I currently have because the heights are uneven is there anyway around this?
<div class="boxContainer">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
.boxContainer{
width: 800px;
height: auto;
}
.box{
float: left;
min-height: 150px;
width: 400px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用显示作为块,因为你向左浮动,这样你就必须分配宽度,这样它们就不会移出位置,一旦添加文本,高度就会自动增长,最小高度,不适用于较旧的浏览器,并且将被视为高度,这很糟糕:) http://www.webtoolkit.info/css-clearfix.html 你有一个漂亮的 css 解决方案来解决你的浮动左问题,它用在容器上以保持 div 自然 -
是的,检查如果你有
或或其他元素,有预定义的填充和边距,:)它们可能有点麻烦:)
我建议使用clearfix,因为它可以很好地与Web开发人员工具(例如firebug)上的检查元素配合使用和谷歌网络开发工具:)
所以你可以完全控制这里是代码:)
just use display as block, because you are floating left, that way you would have to assign , the width so they don't move out of position, once the text is added, the height will,grow automatically, min-height, does not work on older browsers and will be treated as height, which is terrible :) http://www.webtoolkit.info/css-clearfix.html you have a beautiful css solution to your float left problem, it's used on containers to keep the div natural –
yes, check if you have
or or other elements, that have predefined padding, and margin, :) they can be a bit of pain in the ass :)
I recommend using clearfix, because it works well with, inspect element on web developer tools such as firebug, and google web dev tools :)
so you have full control here is the code :)
这实际上对我不起作用: http://jsfiddle.net/MMDqX/ ;D
如果浏览器兼容性(至少 ie6 或更早版本(如果我没记错的话)不是问题,也许使用表格布局会起作用,即带有 display:table 的 div,
例如http://jsfiddle.net/MMDqX/1/
that actually does not work for me: http://jsfiddle.net/MMDqX/ ;D
if browser compatibility (at least ie6 or older if i remember correctly) is not a concern, maybe using a tabled layout would work, i.e. divs with display:table
e.g. http://jsfiddle.net/MMDqX/1/