Skip to content

Instantly share code, notes, and snippets.

@kimf
Created February 10, 2021 07:21
Show Gist options
  • Save kimf/afc4198b02ea2f559acb9b906a8e2a5f to your computer and use it in GitHub Desktop.
Save kimf/afc4198b02ea2f559acb9b906a8e2a5f to your computer and use it in GitHub Desktop.
Progressive Aspect Ratio (Found on CodyHouse)
<ul class="wrapper">
<li class="aspect-ratio-1:1"></li>
<li class="aspect-ratio-16:9"></li>
<li class="aspect-ratio-4:3"></li>
</ul>
// 👇 target all aspect-ratio classes
[class*="aspect-ratio"] {
--aspect-ratio: 16/9;
// 👇 padding-bottom hack
height: 0;
padding-bottom: calc(100%/(var(--aspect-ratio)));
// 👇 use aspect-ratio property if supported
@supports (aspect-ratio: 16/9) {
aspect-ratio: var(--aspect-ratio);
height: initial;
padding-bottom: initial;
}
}
// 👇 aspect-ratio utility classes
.aspect-ratio-16\:9 { --aspect-ratio: 16/9; }
.aspect-ratio-4\:3 { --aspect-ratio: 4/3; }
.aspect-ratio-1\:1 { --aspect-ratio: 1/1; }
// Demo stuff
body {
padding: 2em;
}
.wrapper {
display: flex;
align-items: flex-start;
flex-wrap: wrap;
gap: 1em;
> * {
background-color: blueviolet;
width: 200px;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment