Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically center div over another div

Tags:

html

css

center

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

like image 474
nictoriousface Avatar asked Jul 10 '13 14:07

nictoriousface


1 Answers

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/

like image 170
koningdavid Avatar answered Oct 30 '22 09:10

koningdavid