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
}
Using jQuery you can achieve this by doing
Check working example at http://jsfiddle.net/n6DAu/1/
There is a way using CSS!
If you set your width depending on the parent container you can set the height to 0 and set padding-bottom to the percentage which will be calculated depending on the current width:
This works well in all major browsers.
JSFiddle: https://jsfiddle.net/ayb9nzj3/
[Update: Although I discovered this trick independently, I’ve since learned that Thierry Koblentz beat me to it. You can find his 2009 article on A List Apart. Credit where credit is due.]
I know this is an old question, but I encountered a similar problem that I did solve only with CSS. Here is my blog post that discusses the solution. Included in the post is a live example. Code is reposted below.
HTML:
CSS: