本文翻译自:How to align a
to the middle (horizontally/width) of the page [duplicate]
#1楼
#2楼
This also works in Internet Explorer, but auto margins do not. 这也适用于Internet Explorer,但自动边距不适用。
.centered {
position: absolute;
display: inline-block;
left: -500px;
width: 1000px;
margin: 0 50%;
}
#3楼
<body>
<center>
... Your code here ...
</center>
</body>
This worked for me with all the browsers I have tried. 这适用于我尝试过的所有浏览器。
#4楼
Other classes hidden in external stylesheet links. 隐藏在外部样式表链接中的其他类。 Other classes embedded in something like animg
(like for older external CSS print format controls). 嵌入在img
类的其他类(如旧的外部CSS打印格式控件)。 Legend code with IDs and/or CLASSES will conflict with a named div
class. 带有ID和/或CLASSES的图例代码将与命名的div
类冲突。
#5楼
#outPopUp { position: absolute; width: 300px; height: 200px; z-index: 15; top: 50%; left: 50%; margin: -100px 0 0 -150px; background: red; }
<div id="outPopUp"></div>
#6楼
You can also use it like this: 您也可以像这样使用它:
<div style="width: 60%; margin: 0px auto;">
Your contents here...
</div>