Absolute positioned div vertically centering - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Absolute Position

Description

Absolute positioned div vertically centering

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <title>Lorem ipsum dolor sit am</title> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">
.box {<!--from  ww w  .  j  a va2 s  . com-->
   width:161px;
   height:161px;
   background:Chartreuse;
   position:relative;
   margin-bottom:16px;
}

.box .subbox {
   display:table;
   position:absolute;
   width:100%;
   height:100%;
   top:0;
   left:0;
   z-index:6;
}

.box .subbox .text {
   display:table-cell;
   vertical-align:middle;
   text-align:center;
   color:yellow;
   padding:16px;
   font-weight:bold;
   font-size:25px;
}

.box .somethingelse {
   position:absolute;
   z-index:2;
}
</style> 
 </head> 
 <body> 
  <div class="box"> 
   <div class="subbox"> 
    <div class="text">
      Lorem ipsum dolor sit amet, consectetu 
    </div> 
   </div> 
   <div class="somethingelse">
     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ullamcorper molestie lacus sed fermentum. Aliquam erat volutpat. Vestibulum tortor neque, ornare vitae tempo 
   </div> 
  </div> 
  <div class="box"> 
   <div class="subbox"> 
    <div class="text">
      Lorem ipsum 
    </div> 
   </div> 
   <div class="somethingelse">
     Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ullamcorper molestie lacus sed fermentum. Aliquam erat volutpat. Vestibulum tortor neque, ornare vitae tempo 
   </div> 
  </div>  
 </body>
</html>

Related Tutorials