【问题标题】:How to align objects to centre of page?如何将对象对齐到页面中心?
【发布时间】:2014-08-27 06:09:22
【问题描述】:

我尝试使用以下 CSS 但objects were not positioned at the centre 对齐网页中的对象。我该怎么办?

left:50%;
margin-left:-25%;

【问题讨论】:

  • 你的定位看起来很糟糕,你需要在这里分享一些标记,或者如果你想对齐中心,只需使用margin: auto..

标签: html css alignment


【解决方案1】:

你可以试试这个吗?效果很好

适用于: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>

【讨论】:

  • 问题是关于对象,而不是文本。一个更有帮助的例子是使用三个或 4 个以页面为中心的对象,同时以特定的关系排列。
【解决方案2】:

对象的父元素(它们的容器)必须设置为 position:relative

对象本身应设置为 position:absolute, left:50%, margin-left 负值(以像素为单位),即对象值的一半。

或者,您也可以尝试简单地将父元素设置为 text-align:center 并将对象本身设置为“margin:0 auto;”

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 2019-10-14
    • 2019-11-28
    • 2012-02-15
    • 2013-09-27
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2013-12-08
    相关资源
    最近更新 更多