Can you use CSS calc() function to make the height of an element match its width? My attempt is not working

RiverOceanSea picture RiverOceanSea · Feb 3, 2018 · Viewed 9.1k times · Source

The calc() function is not working as I expected it to in my code.

Instead of the height being calculated to 20% of the container's width, the height is collapsing.

html:

I've tried adding a calculation, like "height: calc(20% - 0px);" but the height is still just collapsing to zero.

Can anyone see if I am doing something wrong? Thanks!

Answer

user10199185 picture user10199185 · Aug 8, 2018

just put

height:0; padding-top:100%; 

The padding is taking it's calculations from the width of the element.