【问题标题】:dynamically center div over another div在另一个 div 上动态居中 div
【发布时间】:2013-07-08 12:59:20
【问题描述】:

我希望将没有固定宽度的 div 居中放置在视频上,因此如果调整窗口大小,该 div 将随之缩小,但仍保持在中心。我目前的设置接近于使用 left:50% 和一半负边距的正确设置。问题显然是它向左移动,因为负边距是固定的。有任何想法吗?谢谢!

我的 HTML:

<div id = 'top-container'>
   <video id='top-vid' autoplay loop width=100% height='auto' class='no-mobile'>
       <source src='DS_Intro.mov' type='video/mp4'>
       <source src='test.webm' type='video/webm'>
   </video>

   <div id = 'top-content'>
       <div id = 'top-text'>
           <div id = 'top-img'>
               <img src='ds_white.png' width = "100%" height = 'auto'>
           </div>
           <h1 id = 'top-slogan'>a boutique video production studio</h1>
       </div>
   </div>
</div>

我的 CSS:

#top-container{
    width:100%;
    height:100%;
}

#top-content{
    max-width:1200px;
    margin-right:auto;
    margin-left:auto;
}

#top-img{
    width:100%;
    padding-bottom: 10%;
}

#top-slogan{
    text-align:center;
    padding-bottom:10%;
    font-weight: 100;
    color:#5DBCD2;

}

#top-text {
    top: 50%;
    left: 50%;
    margin-left: -360px;
    position:absolute;
    width:50%;

}

这里是FIDDLE

【问题讨论】:

  • 在#top-text 规则中尝试margin: 50% auto 0;。

标签: css html center


【解决方案1】:

这可以通过使用transform: translate(-50%, -50%); 通过使用它,内部元素可以是动态的(不需要负边距),请参阅此示例

http://jsfiddle.net/Mfsfm/2/

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 2010-11-19
    • 2017-03-27
    • 1970-01-01
    • 2011-11-20
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多