【发布时间】:2014-08-27 06:09:22
【问题描述】:
我尝试使用以下 CSS 但objects were not positioned at the centre 对齐网页中的对象。我该怎么办?
left:50%;
margin-left:-25%;
【问题讨论】:
-
你的定位看起来很糟糕,你需要在这里分享一些标记,或者如果你想对齐中心,只需使用
margin: auto..
我尝试使用以下 CSS 但objects were not positioned at the centre 对齐网页中的对象。我该怎么办?
left:50%;
margin-left:-25%;
【问题讨论】:
margin: auto..
你可以试试这个吗?效果很好
适用于:Internet Explorer - Google Chrome - Safari - Mozilla
<!-- Div content -->
<div style="margin:0 auto;text-align:center">
<!-- Div align in the middle -->
<div style="margin-left:auto;margin-right:auto;text-align:center">
<p>Hello my text is in the middle :-) </p>
</div>
</div>
【讨论】:
对象的父元素(它们的容器)必须设置为 position:relative
对象本身应设置为 position:absolute, left:50%, margin-left 负值(以像素为单位),即对象值的一半。
或者,您也可以尝试简单地将父元素设置为 text-align:center 并将对象本身设置为“margin:0 auto;”
【讨论】: