2016-07-28 79 views
0

我在一个容器中并排放置了四行三个div。整个事情应该是敏感的,所以它可以自动调整到可变容器宽度,即桌面三格,平板电脑旁边的两个div和智能手机,单独一个div。 我认为当为每个div设置30%的宽度时,剩余的10%将按照边距在两个div之间分割,即每个div之间5%的边距。但是现在我得到了我的第三个分区权利的全部10%的保证金,这意味着所有的div都粘在一起了。如何在div之间获得2 x 5%的保证金(保证金 - 右侧:5%不起作用)?三个并列的div,但它们之间没有自动保证金

.aaa { 
 
    position: relative; 
 
    display: block; 
 
    text-align: left; 
 
\t margin-top:30px; 
 
\t padding:2px 25px 2px 25px; 
 
\t text-align:center; 
 
\t float:left; 
 
\t width:30%; 
 
\t height:239px; 
 
    background-color:blue; 
 
} 
 

 
.bbb { 
 
    position: relative; 
 
    display: block; 
 
    text-align: left; 
 
\t margin-top:30px; 
 
\t padding:2px 25px 2px 25px; 
 
\t text-align:center; 
 
\t float:left; 
 
\t width:30%; 
 
\t height:239px; 
 
    background-color:grey; 
 
} 
 

 
.ccc { 
 
    position: relative; 
 
    display: block; 
 
    text-align: left; 
 
\t margin-top:30px; 
 
\t padding:2px 25px 2px 25px; 
 
\t text-align:center; 
 
\t float:left; 
 
\t width:30%; 
 
\t height:239px; 
 
    background-color:green 
 
} 
 

 
.ddd { 
 
    position: relative; 
 
    display: block; 
 
    text-align: left; 
 
\t margin-top:30px; 
 
\t padding:2px 25px 2px 25px; 
 
\t text-align:center; 
 
\t float:left; 
 
\t width:30%; 
 
\t height:239px; 
 
    background-color:yellow 
 
} 
 

 
.eee { 
 
    position: relative; 
 
    display: block; 
 
    text-align: left; 
 
\t margin-top:30px; 
 
\t padding:2px 25px 2px 25px; 
 
\t text-align:center; 
 
\t float:left; 
 
\t width:30%; 
 
\t height:239px; 
 
    background-color:red 
 
} 
 

 
.fff { 
 
    position: relative; 
 
    display: block; 
 
    text-align: left; 
 
\t margin-top:30px; 
 
\t padding:2px 25px 2px 25px; 
 
\t text-align:center; 
 
\t float:left; 
 
\t width:30%; 
 
\t height:239px; 
 
    background-color:black 
 
}
<div class="container"> 
 
<div class="aaa"><a href="#"><h1>aaa</h1></a></div> 
 
<div class="bbb"><a href="#"><h1>bbb</h1></a></div> 
 
<div class="ccc"><a href="#"><h1>ccc</h1></a></div> 
 
<div class="ddd"><a href="#"><h1>ddd</h1></a></div> 
 
<div class="eee"><a href="#"><h1>eee</h1></a></div> 
 
<div class="fff"><a href="#"><h1>fff</h1></a></div> 
 
</div>

回答

2

确保您使用box-sizing使得padding被正确地计算。您也可能希望利用媒体查询来实现您在较小屏幕上减少并排元素的目标。

https://jsfiddle.net/a6ry5m7v/

当然,还有更现代的方法(如flex容器),但根据您最初的意见,这似乎是您所期望

的方法
相关问题