2013-04-10 101 views
1

比方说,我有与以下规格的容器:如何创建3个相同大小的盒子?

.container { 
width: 960px; 
margin: 0 auto; 
height: 500px; 
} 

现在,在中间我想补充的3盒彼此相邻排列,平行以下规格:

.box1 { 
background-color: #000; 
width: 300px; 
height: 200px; 
} 
.box2 { 
background-color: #999; 
width: 300px; 
height: 200px; 
} 
.box3 { 
background-color: #333; 
width: 300px; 
height: 200px; 
} 

我试着用margin-top和margin-left在每个上面,但是这很麻烦,而且让它们看起来与它们之间有足够的沟槽看起来一样麻烦。什么是创建这个最好的方法?

+1

广告向每个盒子元素提供“浮动:向左”,然后使用边距使它们间隔相等。 – 2013-04-10 00:56:37

回答

3

你必须把“float:left;”在每个班上。

.container { 
float:left; 
width: 960px; 
margin: 0 auto; 
height: 500px; 
} 

.box1 { 
float:left; 
background-color: #000; 
width: 300px; 
height: 200px; 
} 
.box2 { 
float:left; 
background-color: #999; 
width: 300px; 
height: 200px; 
} 
.box3 { 
float:left; 
background-color: #333; 
width: 300px; 
height: 200px; 
} 
+0

谢谢。但是,我将如何在它们之间增加空间?利润率左? – AAA 2013-04-10 01:01:56

+1

是......或保证金。我不知道你在做什么,但是我会在边框1和边框2中添加边距 - 右边:'X'px。只要做数学就可以在不超过容器宽度(960px)的情况下保留多长时间。 – 2013-04-10 01:09:46

1

对于每个.boxX项目,添加display: inline - 这将为您解决问题。

0

使用CSS属性:

display: inline-block 

在所有.box

1
.container { width: 960px; margin: 0 auto; height: 500px; } 
.container [class*='box'] { width:300px; height:200px; float: left; margin-right: 30px; } 
.container .box1 { background-color: #000; } 
.container .box2 { background-color: #999; } 
.container .box3 { background-color: #333; margin-right: 0; } 

http://jsfiddle.net/DRYBH/#fork

你也可以试试这个最小码

相关问题