Make a relatively positioned div square with CSS - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Relative Position

Description

Make a relatively positioned div square with CSS

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">
.FlexEmbed {<!--   www  . ja v a 2  s  . c o  m-->
   display:block;
   overflow:hidden;
   position:relative;
}

.FlexEmbed-ratio {
   display:block;
   padding-bottom:100%;
   width:100%;
}

.FlexEmbed-content {
   bottom:0;
   height:100%;
   left:0;
   position:absolute;
   top:0;
   width:100%;
}

.UserImage {
   background-size:cover;
   background-image:url('https://www.java2s.com/style/demo/Google-Chrome.png');
}
</style> 
 </head> 
 <body> 
  <div class="FlexEmbed FlexEmbed-ratio"> 
   <div class="UserImage FlexEmbed-content"></div> 
  </div>  
 </body>
</html>

Related Tutorials