Skip to content

Instantly share code, notes, and snippets.

@benmj
Created September 9, 2013 21:14
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 benmj/6501618 to your computer and use it in GitHub Desktop.
Save benmj/6501618 to your computer and use it in GitHub Desktop.
An easy way to maintain a specific ratio on fluid containers using only CSS.
/**
* Ratios.less
*
* Ben Jacobs <ben.jacobs@buildium.com> 2013/09/09
*
* Inspired by a [blog post][1] on maintaing aspect ratio of fluid content
* using only CSS.
*
* [1]: http://ansciath.tumblr.com/post/7347495869/css-aspect-ratio by ansciath
*/
.make-ratio(@width, @height) {
&:after {
padding-top: percentage(@height / @width);
}
}
.ratio-container {
display: inline-block;
position: relative;
width: 100%;
&:after {
content: '';
display: block;
}
.ratio-element {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
}
&.ratio-4-3 { .make-ratio(4, 3); }
&.ratio-3-1 { .make-ratio(3, 1); }
&.ratio-3-2 { .make-ratio(3, 2); }
&.ratio-2-1 { .make-ratio(2, 1); }
&.ratio-1-1 { .make-ratio(1, 1); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment