【问题标题】:Issue to center a div horizontally使 div 水平居中的问题
【发布时间】:2015-01-04 00:59:11
【问题描述】:

我试图将我的 div .down-arrow 水平居中,但没有成功。 DIV 是绝对定位的,但 margin:0px 自动;似乎没有工作。问题是什么?谢谢 http://jsfiddle.net/7UNrP/

HTML:

  <header>
<div class="down-arrow">arrow</div>

    </header>

CSS:

header {
position: relative;
  width: 100%;
  height: 600px;
  min-height: 300px;
  margin-right: auto;
  margin-left: auto;
  background-image: url('http://lorempixel.com/output/nature-q-c-1020-711-1.jpg');
  background-size: cover;
  background-position: center center;
  background-color: rgb(222, 222, 222);
}
.down-arrow {
    position: absolute;
    margin:0px auto;
    bottom: 20px;
    display: inline;
    padding: 10px;
    color: #FFF;
    border: 0;
    font-weight: 400;
    font-size: 12px;
    background: red;
    -webkit-animation: icon 1.2s infinite;
}


@-webkit-keyframes icon {
    from {
        opacity: 1;
        bottom: 20px;
    }
    to {
        opacity: 0;
        bottom: 10px;
    }
}

【问题讨论】:

    标签: html css alignment


    【解决方案1】:

    问题是你没有告诉箭头 div 除了bottom:20px 之外的位置,所以它默认为left:0;

    JSfiddle Demo

    您需要将此添加到您的箭头 CSS 中

    left:50%; /*push the div halfway over*/
    -webkit-transform:translateX(-50%); /* bring it back by half its own width */
    transform:translateX(-50%);
    

    您可能想参考这篇有很多相同问题的帖子。我将详细介绍此解决方案。

    Reference Question

    【讨论】:

      【解决方案2】:

      问题是由于使用 margin:0 autodisplay:inlineposition: absolute。您可以通过将text-align:center 应用到header 轻松地将其居中,因为您的内部内容具有inline 布局。

      Example

      【讨论】:

      • 感谢您的建议,但由于某种原因,它在实时环境中不起作用,请参见此处:goo.gl/rIFzou您知道为什么吗?
      • @Greg :我猜是因为inlineblock 元素的混合。像this 这样的东西可以解决。
      • 谢谢。不幸的是,这个解决方案的 div 不是完全水平对齐的,它向右多出几个 px。我会寻求保利的解决方案。无论如何感谢您的帮助
      【解决方案3】:

      你可以使用

      text-align:center;
      

      header这样的css中

      header {
      position: relative;
      width: 100%;
      height: 600px;
      min-height: 300px;
      margin-right: auto;
      margin-left: auto;
      background-image: url('http://lorempixel.com/output/nature-q-c-1020-711-1.jpg');
      background-size: cover;
      background-position: center center;
      background-color: rgb(222, 222, 222);
      text-align: center;
      }
      

      编辑:

      这会水平对齐中心的 .down-arrow div,并使其与容器底部保持 20 像素的距离

         .down-arrow {
      position: absolute;
      margin-left: 50%;
      /* bottom: 20px; */
      /* display: inline; */
      padding: 10px;
      color: #FFF;
      border: 0;
      font-weight: 400;
      font-size: 12px;
      background: red;
      -webkit-animation: icon 1.2s infinite;
      }
      

      【讨论】:

      • 感谢您的建议,但由于某种原因,它在实时环境中不起作用,请参见此处:goo.gl/rIFzou您知道为什么吗?
      • 你能告诉我们实时网址吗?也可以试试 text-align:center !important;
      • 见这里:goo.gl/rIFzou Tks 我刚刚尝试添加 !important;但不会改变任何东西
      • @Greg 尝试使用 position: relative;在 .down-arrow 中,这应该可以解决问题。 PS:看看我的回答中的编辑。我希望它有帮助:)
      • 谢谢,不幸的是 position:relative div 没有放置在距离容器底部 20px 处
      猜你喜欢
      • 2022-11-18
      • 2013-03-30
      • 2015-02-10
      • 2013-01-26
      • 2015-12-16
      • 2014-05-22
      • 2012-02-03
      相关资源
      最近更新 更多