是否可以将div置于div中,这样当父div调整大小时,它仍然保持在中心?到目前为止,我试过了
保证金:0自动;和 margin-left:auto; margin-right:auto;两者都有效,但是一旦更改了div大小,它就不会重新定位。谢谢:)))))
标记:
<div id='bottomcontent' style="float: left; position: absolute; left: 0; bottom: 0; background-color:#0b0b0b; height:100px; width:796px;">
<div id="bottomleftbutton" style="margin-left: 25px;">stuff here </div>
<div id="bottomcentertext" style="margin-left: auto; margin-right: auto;">so far
<h4>17</h4>
places match your criteria</div>
<div id="bottomrightbutton" style="float: right; margin-right: 25px;">stuff here </div>
</div>
答案 0 :(得分:2)
要使margin:auto;
正常工作,您需要在div
上设置宽度。
所以,如果你做了这样的事情
<div id="bottomcentertext" style="
margin-left: auto;
margin-right: auto;
width:300px; //NEW
color:white;">
它有效。
答案 1 :(得分:1)
用这个样式设置内部div:
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
请在此处查看示例:http://jsfiddle.net/2K4Gp/