Skip to content

Instantly share code, notes, and snippets.

@itsdonnix
Created June 9, 2022 02:01
Show Gist options
  • Save itsdonnix/f4f9fc091e0eec895224dd10a758ce54 to your computer and use it in GitHub Desktop.
Save itsdonnix/f4f9fc091e0eec895224dd10a758ce54 to your computer and use it in GitHub Desktop.
CSS Aspect Ratio with Fallback
<!--
CSS Aspect Ratio with Fallback by Una Kravets
Source: https://codepen.io/una/pen/BazyaOM
-->
<div>I am always 16x9</div>
<style>
div {
background: lightblue;
width: 100%;
// New aspect-ratio property
aspect-ratio: 16 / 9;
}
// Fallback (current, using padding hack)
@supports not (aspect-ratio: 16 / 9) {
div::before {
float: left;
padding-top: 56.25%;
content: '';
}
div::after {
display: block;
content: '';
clear: both;
}
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment