This question already has an answer here:
- Maintain the aspect ratio of a div with CSS 21 answers
Is it possible to set same height as width (ratio 1:1)?
Example
+----------+
| body |
| 1:3 |
| |
| +------+ |
| | div | |
| | 1:1 | |
| +------+ |
| |
| |
| |
| |
| |
+----------+
CSS
div {
width: 80%;
height: same-as-width
}
Simple and neet : use
vw
units for a responsive height/width according to the viewport width.DEMO
HTML:
CSS for a 1:1 aspect ratio:
Table to calculate height according to the desired aspect ratio and width of element.
This technique allows you to :
position:absolute;
These units are supported by IE9+ see canIuse for more info
It is possible without any Javascript :)
This article describes it perfectly - http://www.mademyday.de/css-height-equals-width-with-pure-css.html
The HTML:
The CSS:
Expanding upon the padding top/bottom technique, it is possible to use a pseudo element to set the height of the element. Use float and negative margins to remove the pseudo element from the flow and view.
This allows you to place content inside the box without using an extra div and/or CSS positioning.
width: 80vmin; height: 80vmin;
CSS does 80% of the smallest view, height or width
http://caniuse.com/#feat=viewport-units
really this belongs as a comment to Nathan's answer, but I'm not allowed to do that yet...
I wanted to maintain the aspect ratio, even if there is too much stuff to fit in the box. His example expands the height, changing the aspect ratio. I found adding
to the .element helped. See http://jsfiddle.net/B8FU8/3111/
Extremely simple method jsfiddle
HTML
CSS