Skip to content

Instantly share code, notes, and snippets.

@yusufhm
Created April 17, 2015 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusufhm/96f8d8a50f33c2887b37 to your computer and use it in GitHub Desktop.
Save yusufhm/96f8d8a50f33c2887b37 to your computer and use it in GitHub Desktop.
CSS fix - aspect ratio
<!-- taken from http://stackoverflow.com/a/12121309/351590 -->
<div class="wrapper">
<div class="main">
This is your div with the specified aspect ratio.
</div>
</div>
<style>
.wrapper {
width: 50%;
/* whatever width you want */
display: inline-block;
position: relative;
}
.wrapper:after {
padding-top: 56.25%;
/* 16:9 ratio */
display: block;
content: '';
}
.main {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
/* fill parent */
background-color: deepskyblue;
/* let's see it! */
color: white;
}
</style>
@yusufhm
Copy link
Author

yusufhm commented Apr 17, 2015

@yusufhm
Copy link
Author

yusufhm commented May 6, 2015

@yusufhm
Copy link
Author

yusufhm commented Sep 29, 2015

@yusufhm
Copy link
Author

yusufhm commented Sep 29, 2015

Briefly:

To calculate the percentage needed for any aspect ratio we can use the following formula:

B / (A / 100) = C%

So for 16:9 (where 16 is A and 9 is B):

9 / .16 = 56.25 (%)

And that’s all there is to it!

Some Other Common Ratios

For the not so mathematically inclined, here are some of the percentages for common aspect ratios:

75% = 4:3
66.66% = 3:2
62.5% = 8:5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment