Vertically center image within responsive square div - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Responsive Layout

Description

Vertically center image within responsive square div

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">
.thumbnail_container {<!--from   w ww  . ja v a2s  .  c  om-->
   position:relative;
   width:26%;
   padding-bottom:26%;
   background:green;
   float:left;
}

.thumbnail {
   position:absolute;
   width:100%;
   height:100%;
   background:red;
   text-align:center;
}

.thumbnail img {
   position:absolute;
   top:0;
   bottom:0;
   left:0;
   right:0;
   margin:auto;
}

.blue {
   background:blue
}

img {
   max-height:100%;
   max-width:100%;
}

.vcenter-ext {
   background:orange
}

.center-int {
   background:purple
}
</style> 
 </head> 
 <body> 
  <div class="thumbnail_container vcenter-ext"> 
   <div class="thumbnail vcenter-int"> 
    <img src="https://www.java2s.com/style/demo/Firefox.png"> 
   </div> 
  </div> 
  <div class="thumbnail_container vcenter-ext"> 
   <div class="thumbnail blue vcenter-int"> 
    <img src="https://www.java2s.com/style/demo/Opera.png"> 
   </div> 
  </div> 
  <div class="thumbnail_container vcenter-ext"> 
   <div class="thumbnail vcenter-int"> 
    <img src="https://www.java2s.com/style/demo/Opera.png"> 
   </div> 
  </div> 
  <div class="thumbnail_container vcenter-ext"> 
   <div class="thumbnail blue vcenter-int"> 
    <img src="https://www.java2s.com/style/demo/Safari.png"> 
   </div> 
  </div>  
 </body>
</html>

Related Tutorials