Skip to content

Instantly share code, notes, and snippets.

@teknotica
Last active August 29, 2015 14:23
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 teknotica/fca87e2bfb2391fa28ec to your computer and use it in GitHub Desktop.
Save teknotica/fca87e2bfb2391fa28ec to your computer and use it in GitHub Desktop.
SASS snippet to keep ratio of element
.wrapper {
display: inline-block;
position: relative;
&:after {
content: "";
padding-top: 56.25%; // Use 100% for square ratio
display: block;
}
.inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
img {
display: block;
width: 100%;
height: 100%
}
}
}
/*
<!-- Example Markup -->
<div class="wrapper">
<div class="inner">
<h1>Keep ratio please</h1>
</div>
</div>
<div class="wrapper">
<div class="inner">
<img src="/images/image.jpg" alt="An image">
</div>
</div>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment