Centering elements in absolutely-positioned parent - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Absolute Position

Description

Centering elements in absolutely-positioned parent

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">
.cont-img {<!--   w w  w.  j av a2 s  .c  om-->
   width:401px;
   height:401px;
   position:relative;
}

.image {
   width:401px;
   height:401px;
   background:black;
}

.desc {
   position:absolute;
   top:0px;
   height:100%;
   width:100%;
   color:Chartreuse;
   display:table;
}

.centered-items {
   position:absolute;
   left:51%;
   top:51%;
   -webkit-transform:translate(-51%, -51%);
   transform:translate(-51%, -51%);
}
</style> 
 </head> 
 <body> 
  <div class="cont-img"> 
   <div class="image"> 
   </div> 
   <div class="desc"> 
    <div class="centered-items"> 
     <div class="item1">
       Lorem ip 
     </div> 
     <div class="item2">
       Lorem ip 
     </div> 
    </div> 
   </div> 
  </div>  
 </body>
</html>

Related Tutorials