Simple and neet : use vw
units for a responsive height/width according to the viewport width.
vw : 1/100th of the width of the viewport. (Source MDN)
DEMO
HTML:
<div></div>
CSS for a 1:1 aspect ratio:
div{
width:80vw;
height:80vw; /* same as width */
}
Table to calculate height according to the desired aspect ratio and width of element.
aspect ratio | multiply width by
-----------------------------------
1:1 | 1
1:3 | 3
4:3 | 0.75
16:9 | 0.5625
This technique allows you to :
- insert any content inside the element without using
position:absolute;
- no unecessary HTML markup (only one element)
- adapt the elements aspect ratio according to the height of the viewport using vh units
- you can make a responsive square or other aspect ratio that alway fits in viewport according to the height and width of the viewport (see this answer : Responsive square according to width and height of viewport or this demo)
These units are supported by IE9+ see canIuse for more info