Maintain aspect ratio of elements within a fluid CSS grid layout - HTML CSS CSS Layout

HTML CSS examples for CSS Layout:Layout

Description

Maintain aspect ratio of elements within a fluid CSS grid layout

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">
ul {<!--from   ww  w  . ja  v a 2s.  com-->
   border:3px solid Chartreuse;
   display:grid;
   grid-gap:6%;
   grid-template-columns:2fr 2fr 2fr 2fr;
   list-style-type:none;
   counter-reset:div
}

li:nth-child(4n)~li {
   margin-top:6%
}

li {
   background-color:yellow;
   font-size:0;
   counter-increment:div
}

li:before {
   content:'';
   padding-top:100%;
   width:0;
}

li:before , li>div {
   display:inline-block;
   vertical-align:middle;
}

li>div {
   font-size:6vmax;
   width:100%;
   text-align:center;
}

li>div:before {
   content:counter(div)
}
</style> 
 </head> 
 <body> 
  <ul> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
   <li> 
    <div></div> </li> 
  </ul>  
 </body>
</html>

Related Tutorials