日期:2014-05-17  浏览次数:20849 次

浮动div居中显示问题
怎么让里面2个div居中?
<div style=" width:100%; height:200px; border:1px solid red; text-align:center; margin:0px auto;">
<div style=" width:150px;; height:120px; border:1px solid yellow; margin:10px 10px 0px 0px; float:left; "></div>
<div style=" width:150px; height:120px; border:1px solid red; float:left; margin:10px 10px 0px 0px; "></div>
</div>
HTML

------解决方案--------------------

<div style=" width:100%; height:200px; border:1px solid red;margin:0px auto;">
 <div style="width:345px; margin:0px auto;">
 <div style=" width:150px; height:120px; border:1px solid yellow; margin:10px 10px 0px 0px; float:left; "></div>
 <div style=" width:150px; height:120px; border:1px solid red; float:left; margin:10px 10px 0px 0px; "></div>
 </div>
</div> 

------解决方案--------------------

<div style=" width:100%; height:200px; border:1px solid red;  margin:0px auto;">
<div style=" width:20%;; height:120px; border:1px solid ; display:inline-block;  margin:10px auto 0px 30%;"></div>
<div style=" width:20%; height:120px; border:1px solid red; display:inline-block; margin:10px auto 0px auto;"></div>
</div>

外层div是百分数,内层如果px设置长度,那么想要居中则要需要计算margin的宽度。把内层的div宽度也设置成百分比可以解决。
------解决方案--------------------
引用:
Quote: 引用:

在用一个div把2个div装起 就行了 固定宽度 居中


引用:
在用一个div把2个div装起 就行了 固定宽度 居中


除了固定宽度外还有别的办法吗?