Created
February 22, 2021 14:44
-
-
Save lumpysimon/b5806c4be26beb6d45938c4fc9d9078c to your computer and use it in GitHub Desktop.
aspect ratios in CSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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