我想让一个 div
元素居中显示,是否可能?在IE浏览器中,text-align: center
无法生效。
这个很好地运行着
width:40%; // the width of the content div
right:0;
margin-right:30%; // 1/2 the remaining space
这个可以很好地适应布局的大小。
CSS 示例如下:
.centered-div {
position:fixed;
background-color:#fff;
text-align:center;
width:40%;
right:0;
margin-right:30%;
}
<div id="outer" style="z-index:10000;width:99%;height:200px;margin-top:300px;margin-left:auto;margin-right:auto;float:left;position:absolute;opacity:0.9;">
<div id="inner" style="opacity:1;background-color:White;width:300px;height:200px;margin-left:auto;margin-right:auto;">Inner</div></div>
将div浮动到背景的最大宽度,设置一个不透明的div并使用margin auto居中。
不是。
您可以让内容浮动到元素的右侧 (float: left
) 或左侧 (float: right
),但不能同时浮动到两侧。
<center>
标签包裹 div,问题是如果没有其他代码的话,它也会居中 div 的内容。希望能帮到你 :)