Skip to content

Instantly share code, notes, and snippets.

@lumpysimon
Created February 22, 2021 14:44
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 lumpysimon/b5806c4be26beb6d45938c4fc9d9078c to your computer and use it in GitHub Desktop.
Save lumpysimon/b5806c4be26beb6d45938c4fc9d9078c to your computer and use it in GitHub Desktop.
aspect ratios in CSS
[style*="--aspect-ratio"] {
aspect-ratio: var(--aspect-ratio);
object-fit: cover;
}
@supports not (aspect-ratio) {
[style*="--aspect-ratio"] {
position: relative;
}
[style*="--aspect-ratio"]::before {
content: "";
display: block;
padding-bottom: calc(100% / (var(--aspect-ratio)));
}
[style*="--aspect-ratio"] > :first-child {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 100%;
width: 100%;
object-fit: cover;
}
}
<figure style="--aspect-ratio: 16/9">
</figure>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment