html - 问题使 div 水平居中

标签 html css alignment

我试图将我的 div .down-arrow 水平居中但没有成功。 DIV 是绝对定位的,但 margin:0px auto;似乎没有用。问题是什么?谢谢 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;
    }
}

最佳答案

问题是除了 bottom:20px 之外,您还没有告诉箭头 div 在哪里,所以它默认为 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

关于html - 问题使 div 水平居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23984038/

相关文章:

javascript - 如何更改 DataTables 中各个列的打印对齐方式?

c++ - 我需要在 C++ 中对齐吗?

swift - 使用 swift 将工具栏按钮移至工具栏左侧

c - 此代码如何将指针对齐到 64 位边界?

android - Android 浏览器不支持带百分比值的边框半径

html - 将方程式和图像存储在数据库中以便稍后显示为 pdf 和 html 的最佳方式

html - 如何将按钮与 html 和 css 对齐?

css - 想要在 Html.ActionLink 中使用带有文本的图像

jquery - 悬停缩略图时对缩略图和 H1 链接的过渡效果

html - Bootstrap 中的选项卡式复选框