Skip to content

Instantly share code, notes, and snippets.

@jmc2082
Created April 22, 2016 17:31
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 jmc2082/8842da13304a750b2e4cbd3ff95eb9d5 to your computer and use it in GitHub Desktop.
Save jmc2082/8842da13304a750b2e4cbd3ff95eb9d5 to your computer and use it in GitHub Desktop.
Less / CSS aspect ratio on an html element
// aspect ratio
.try-aspect(@width, @height) {
position: relative;
&:before {
display: block;
content: "";
width: 100%;
padding-top: (@height / @width) * 100%;
}
> .content {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
}
.sixteen-nine {
.try-aspect(16, 9);
background-color: @yellow;
}
// html
<div class="sixteen-nine">
<div class="content">
insert content here
this will maintain a 16:9 aspect ratio
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment