I wanted to create a square div by pure CSS what it's height
is dynamic and it's width
will scale base on height
.
As we can see in the snipet below, the container
height is dynamic base on its content.
The red square
take full height of the container
and the width should be equal height
.
I know we can maintain aspect ratio of a div base on its width but i can not find a way when the height
is dynamic.
Any help would be appreciated! Thanks so much!
Note that: The height is dynamic so the solution with vh
did not work and the snipet is just a playground.
#container {
width: 400px;
padding: 20px;
border: solid 1px black;
position: relative;
}
.square {
position: absolute;
top: 0;
right: 0;
bottom: 0;
background-color: red;
width: 100px /*Hmm its just a dummy value*/
}
<div id="container">
<div class="content">
I'm a dynamic content
</div>
<div class="square"></div>
</div>