Skip to content

Instantly share code, notes, and snippets.

@krivaten
Last active August 29, 2015 14:05
Show Gist options
  • Save krivaten/fa864b67b741f61b42a2 to your computer and use it in GitHub Desktop.
Save krivaten/fa864b67b741f61b42a2 to your computer and use it in GitHub Desktop.
CSS Aspect Ratio
/**
* Ratio styles
*/
.ratio {
position: relative;
width: 100%;
// 1:1 ratio (default)
&:before {
content: "";
display: block;
padding-top: 100%;
}
// 2:1 ratio
&.ratio-2-1:before{
padding-top: 50%;
}
// 1:2 ratio
&.ratio-1-2:before{
padding-top: 200%;
}
// 4:3 ratio
&.ratio-4-3:before{
padding-top: 75%;
}
// 16:9 ratio
&.ratio-16-9:before{
padding-top: 56.25%;
}
.ratio-content{
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.thumbnail, .content {
@include transform(translate3d(0, -50%, 0));
width: 100%;
position: absolute;
top: 50%;
}
// thumbnail overlay
.thumbnail {
z-index: 0;
}
// text content that is vertically aligned
.content {
color: red;
padding: 0 15px;
z-index: 10;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment