【问题标题】:When i align a div to the center of a page using css it goes from the left side of the image not the center当我使用 css 将 div 对齐到页面的中心时,它从图像的左侧而不是中心
【发布时间】:2014-02-08 14:56:03
【问题描述】:

这是 HTML

<div id="logo">
   <img src="image.png" 
</div>

这是它的 CSS

#logo {
    position: absolute;
    display:block;
    /*background:#00FFCC;*/
    top: 5px;
    left:50%;
    max-width: 520px;
    max-height: 150px;
    min-height: 150px;
    min-width: 520px;
}

图像在 div 的中心对齐,但是当我将 50% 作为 left 属性时,它使用 div 的左侧作为中心点,所以 div 的左侧在中心而不是中间,怎么改?

【问题讨论】:

  • logo div 需要有 position: absolute 吗?

标签: css html alignment center


【解决方案1】:

您可以简单地使用它来代替(或margin-left):

margin:0 auto;

【讨论】:

    【解决方案2】:

    要使徽标中的 img 居中,请执行此操作

    FIDDLE

    img
    {
        position: absolute;
        left:0;right:0;
        margin:auto;
    }
    

    FIDDLE

    img
    {
      display:block;
      margin: 0 auto;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-06-08
      • 2012-04-18
      • 2013-09-27
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      相关资源
      最近更新 更多