我有一个容器宽度为100%的页面,所以它的整个屏幕宽度,我在网格结构中有几个DIV,它们都有浮动:留在它们上面,没有设置宽度,只有10px的边距。用CSS填充div来填充父容器的宽度
是否有一种方法,使用CSS或jQuery,使div填充整个宽度并证明自己适合差距,所以边距会根据屏幕大小而变化。
我有一个容器宽度为100%的页面,所以它的整个屏幕宽度,我在网格结构中有几个DIV,它们都有浮动:留在它们上面,没有设置宽度,只有10px的边距。用CSS填充div来填充父容器的宽度
是否有一种方法,使用CSS或jQuery,使div填充整个宽度并证明自己适合差距,所以边距会根据屏幕大小而变化。
在这个线程没有JavaScript在所有浏览器包括IE 6的工作原理纯CSS/HTML解决退房thirtydot的答案。
Fluid width with equally spaced DIVs
http://jsfiddle.net/thirtydot/EDp8R/
HTML:
<div id="container">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<span class="stretch"></span>
</div>
CSS:
#container {
border: 2px dashed #444;
height: 125px;
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
/* just for demo */
min-width: 612px;
}
.box1, .box2, .box3, .box4 {
width: 150px;
height: 125px;
vertical-align: top;
display: inline-block;
*display: inline;
zoom: 1
}
.stretch {
width: 100%;
display: inline-block;
font-size: 0;
line-height: 0
}
.box1, .box3 {
background: #ccc
}
.box2, .box4 {
background: #0ff
}
如果以百分比(即宽度,边距,填充)指定所有尺寸,则可以使用CSS进行此操作。
或者,如果你想有一个特定的保证金或填充,你可以使用jQuery
$(window).resize(function() {
var $columns = $('.column'),
numberOfColumns = $columns.length,
marginAndPadding = 0,
newColumnWidth = ($('#container').width()/numberOfColumns) - marginAndPadding,
newColumnWidthString = newColumnWidth.toString() + "px";
$columns.css('width', newColumnWidthString);
}).resize();
使用CSS3这现在超级简单。
UPDATE
增加了微软IE浏览器的支持(相信它或不...)。我不太确定FF的东西,因为Mozilla改变了一些东西。我没有那么多时间。因此,如果有人也许可以纠正我...
UPDATE II
迁代码片断
.container {
border: 1px solid black;
display: -webkit-box;
-webkit-box-pack: justify;
-webkit-box-align: center;
display: -moz-box;
-moz-box-pack: justify;
-moz-box-align: center;
display: -ms-flexbox;
-ms-flex-pack: justify;
-ms-flex-align: center;
display: box;
box-pack: justify;
box-align: center;
}
.child {
background-color: red;
}
<div class="container">
<div class="child">
Some Content
</div>
<div class="child">
Some Content
</div>
<div class="child">
Some Content
</div>
</div>
上测试,请考虑使用:使用“space-between”(http://css-tricks.com/snippets/css/a-guide-to-flexbox/)稳定(相对于整个Flexbox的稳定性) – kurttheviking
你说的div目前没有固定的宽度,但确实有一定的余量。你打算让div的宽度是固定的,并且它们之间的空间是流畅的? – shanethehat
是的,这正是我想要的 – benhowdle89