Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Square div with fixed height using only CSS

Everything is in the title !

How can I do to make my div's width equals to its fixed height using CSS only ?

HTML Code

<div class="container">
    <div class="square"></div>
</div>

CSS Code

.square{
    height:80%;
}

I know I can do the opposite (fixed width) using

.square{
   width : 20%;
   padding-top:20%
}

because padding-top is relative the width of the container.

I also know I can do it using simple JQuery but don't want to use javascript.

Thanks,

like image 849
John smith Avatar asked Oct 18 '25 14:10

John smith


1 Answers

Use vw unit :

.square {
  background: #000;
  width: 50vw;
  height: 50vw;
}
<div class="square"></div>
like image 68
Mehdi Brillaud Avatar answered Oct 21 '25 03:10

Mehdi Brillaud