dynamically center div over another div

点点圈 提交于 2019-12-05 17:25:00

问题


I'm looking to center a div without a fixed width over a video so if the window is resized, the div will shrink along with it but remain in the center. The current set up I have is close to correct using the left:50% with half negative margin. The issue is obviously that it moves left as the negative margin is fixed. Any ideas? Thanks!

My 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>

My 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%;

}

Here is the FIDDLE


回答1:


This is possible with the use of transform: translate(-50%, -50%); By using this the inner element can be dynamic(no need for negative margin's), see this example

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



来源:https://stackoverflow.com/questions/17573324/dynamically-center-div-over-another-div

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!