Vertically align an image inside a div with responsive height - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Responsive Layout

Description

Vertically align an image inside a div with responsive height

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <title>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morb</title> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <style id="compiled-css" type="text/css">
.responsive-container {<!--from   w w w .  j  av  a  2  s  . co  m-->
   position:relative;
   width:100%;
   border:2px solid Chartreuse;
}

.dummy {
   padding-top:100%;
}

.img-container {
   position:absolute;
   top:0;
   bottom:0;
   left:0;
   right:0;
   text-align:center;
   font:0/0 a;
}

.img-container .centerer {
   display:inline-block;
   vertical-align:middle;
   height:100%;
}

.img-container img {
   vertical-align:middle;
   display:inline-block;
   max-height:100%;
   max-width:100%;
}
</style> 
 </head> 
 <body> 
  <div class="responsive-container"> 
   <div class="dummy"></div> 
   <div class="img-container"> 
    <div class="centerer"></div> 
    <img src="https://www.java2s.com/style/demo/InternetExplorer.png" alt=""> 
   </div> 
  </div>  
 </body>
</html>

Related Tutorials