Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:36
Show Gist options
  • Save AndersDJohnson/9381056 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/9381056 to your computer and use it in GitHub Desktop.
CSS Ratios
/* ELEMENT RATIOS
=============================================== */
/**
* To force an element to keep a certain ratio.
* Use as follows:
*
* <div class="ratio ratio-square">
* <div class="ratio-inner">
* <!-- contents, e.g. image -->
* </div>
* </div>
*
*/
.ratio {
height: 0;
position: relative;
}
.ratio-inner {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
overflow: hidden;
}
.ratio-square {
padding-bottom: 100%;
}
.ratio-golden {
padding-bottom: 61.8%;
}
/** Stuff **/
.one {
background: salmon;
}
.two {
background: aquamarine;
}
.container {
width: 25%;
}
<div class="container">
<div class="ratio ratio-square one">
<div class="ratio-inner">
square
</div>
</div>
<div class="ratio ratio-golden wrap two">
<div class="ratio-inner">
golden
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment