Skip to content

Instantly share code, notes, and snippets.

@geraldyeo
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save geraldyeo/4bc72e854e7528c189e6 to your computer and use it in GitHub Desktop.

Select an option

Save geraldyeo/4bc72e854e7528c189e6 to your computer and use it in GitHub Desktop.
CSS maintain aspect ratio
/*
* Pure CSS aspect ratio with no spacer images or js! :)
* http://dabblet.com/gist/2590942
*/
body {
width: 36%;
margin: 8px auto;
}
div.stretchy-wrapper {
width: 100%;
padding-bottom: 56.25%; /* 16:9 */
position: relative;
background: blue;
}
div.stretchy-wrapper > div {
position: absolute;
top: 0; bottom: 0; left: 0; right: 0;
color: white;
font-size: 24px;
text-align: center;
}
/* Other aspect ratios to try:
* 56.25% = 16:9
* 75% = 4:3
* 66.66% = 3:2
* 62.5% = 8:5
*/
<div class="stretchy-wrapper"><div>Resize your browser</div></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment