绝对位置和保证金0自动

时间:2015-04-08 14:37:54

标签: css positioning absolute

我似乎无法弄清楚如何将文本放在包装器的中心。我有一个响应式幻灯片,我想要顶部和居中的文本。



ul.bjqs {
    position:relative;
    list-style:none;
    padding:0;
    margin:0;
    z-index: 1;
    overflow:hidden;
    display:none;
}
.slideshow-tageline {
    position: absolute;
    max-width: 1140px;
    left:0;
    right:0;
    margin:0 auto;
    z-index:99;
    color: #fff;
    font-family:'Montserrat', sans-serif;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-size: 60px;
    line-height: 125%;
    text-shadow: 2px 1px 1px rgba(0, 0, 0, 0.25);
}
.tagline-wrapper {
    position: relative;
    max-width: 1140px;
    width:100%;
    margin: 0 auto;
}

<div id="banner-fade">
    <ul class="bjqs">
      <li> <div class="tagline-wrapper"><span class="slideshow-tageline">Every Moment Matters</span></div><img src="/includes/img/homepage/slider_images/slider_1.jpg"></li>
      <li> <div class="tagline-wrapper"><span class="slideshow-tageline">Every Moment Matters</span></div><img src="/includes/img/homepage/slider_images/slider_2.jpg" ></li>
      <li> <div class="tagline-wrapper"><span class="slideshow-tageline">Every Moment Matters</span></div><img src="/includes/img/homepage/slider_images/slider_3.jpg"></li>
      <li> <div class="tagline-wrapper"><span class="slideshow-tageline">Every Moment Matters</span></div><img src="/includes/img/homepage/slider_images/slider_4.jpg"></li>
      <li> <div class="tagline-wrapper"><span class="slideshow-tageline">Every Moment Matters</span></div><img src="/includes/img/homepage/slider_images/slider_5.jpg"></li>
    </ul>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

要将元素置于绝对位置,您可以执行以下操作:

.myElement{
    position: absolute;
    left: 50%;
    -webkit-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -ms-transform: translateX(-50%);
    transform: translateX(-50%);    
}

您可以使用top并将translateY垂直居中。 示例:http://codepen.io/mbrillaud/pen/zxQQXy

编辑:如果你想水平和垂直居中,你需要使用transform:translate(-50%, - 50%)因为,我不知道为什么,它们似乎互相覆盖。