|
| 1 | +--- |
| 2 | +layout: "@layouts/BlogPost.astro" |
| 3 | +title: "aspect-ratio Is One Of My Favorite CSS Properties" |
| 4 | +date: "2024-08-12" |
| 5 | +description: "The aspect-ratio CSS property makes responsive design (especially with images/videos) so much easier and it only takes a few minutes to learn." |
| 6 | +tags: ["CSS"] |
| 7 | +--- |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +The `aspect-ratio` CSS property is a relatively simple property that solves many problems with responsive design in CSS. In this quick article I will explain everything you need to know about this property (including many lesser known features). |
| 12 | + |
| 13 | +_If you prefer to learn visually, check out the video version of this article._ |
| 14 | +`youtube: pKO1ktPQByk?start=369` |
| 15 | + |
| 16 | +## `aspect-ratio` Basics |
| 17 | + |
| 18 | +`aspect-ratio` is a simple CSS property that lets you force any element to stay at a specific aspect ratio no matter what size it grows to. It is defined by putting the desired width ratio followed by a `/` and then the desired height ratio. For example, if you want an element to always be a 16:9 aspect ratio, you would use `aspect-ratio: 16 / 9;`. |
| 19 | + |
| 20 | +```css {2} |
| 21 | +.element { |
| 22 | + aspect-ratio: 16 / 9; |
| 23 | + background-color: blue; |
| 24 | + width: 50%; |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +<div style="background-color: var(--theme-blue); width: 50%; aspect-ratio: 16 / 9; margin-left: auto; margin-right: auto;" /> |
| 29 | + |
| 30 | +The only thing you need to be aware of is that at least one dimension (width or height) must be an `auto` based size. This is pretty self explanatory, though, since if you specify a hard coded width and height, the `aspect-ratio` proprety would do nothing. |
| 31 | + |
| 32 | +```css {5-6} |
| 33 | +.element { |
| 34 | + /* Doesn't work */ |
| 35 | + aspect-ratio: 16 / 9; |
| 36 | + background-color: red; |
| 37 | + width: 100px; |
| 38 | + height: 100px; |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +<div style="background-color: var(--theme-red); width: 100px; height: 100px; aspect-ratio: 16 / 9; margin-left: auto; margin-right: auto;" /> |
| 43 | + |
| 44 | +By default if both sizes are set to `auto`, the `aspect-ratio` will change the `height` to match the aspect ratio and let the `width` be whatever `auto` size is. |
| 45 | + |
| 46 | +```css {4-5} |
| 47 | +.element { |
| 48 | + aspect-ratio: 3 / 1; |
| 49 | + background-color: blue; |
| 50 | + width: auto; |
| 51 | + height: auto; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +<div style="background-color: var(--theme-blue); width: auto; height: auto; aspect-ratio: 3 / 1; margin-left: auto; margin-right: auto;" /> |
| 56 | + |
| 57 | +## `aspect-ratio` Advanced Features |
| 58 | + |
| 59 | +The basic use case of `aspect-ratio` will cover 90% of your use cases, but there are a few additional ways you can use this property that you should know about. The first is that this property can actually be defined without specifying a height ratio. If you only specify a width ratio, the height ratio will default to 1. |
| 60 | + |
| 61 | +```css {3} |
| 62 | +.element { |
| 63 | + /* Same as 2 / 1 */ |
| 64 | + aspect-ratio: 2; |
| 65 | + background-color: blue; |
| 66 | + width: 50%; |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +<div style="background-color: var(--theme-blue); width: 50%; aspect-ratio: 2; margin-left: auto; margin-right: auto;" /> |
| 71 | + |
| 72 | +Another interesting thing with `aspect-ratio` is you can specify a value of `auto` alongside your aspect ratio which will do interesting things with elements such as videos and images which need to be loaded in. While the image/video is loading, the element will be the aspect ratio you specified, but once the image/video is loaded, the element will adjust to the aspect ratio of the image/video. |
| 73 | + |
| 74 | +```css {2} |
| 75 | +.element { |
| 76 | + aspect-ratio: auto 16 / 9; |
| 77 | + background-color: blue; |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +_Assumming the image itself has an aspect ratio of 1:1._ |
| 82 | + |
| 83 | +<div style="display: flex; gap: 2rem; justify-content: center; align-items: flex-start;"> |
| 84 | + <div style="background-color: var(--theme-blue); width: 100%; aspect-ratio: auto 16 / 9; margin-left: auto; margin-right: auto; color: white; display: flex; justify-content: center; align-items: center;"> |
| 85 | + Loading... |
| 86 | + </div> |
| 87 | + <div style="background-color: var(--theme-purple); width: 100%; aspect-ratio: 1 / 1; margin-left: auto; margin-right: auto; color: white; display: flex; justify-content: center; align-items: center;"> |
| 88 | + Loaded |
| 89 | + </div> |
| 90 | +</div> |
| 91 | + |
| 92 | +This is useful if you want to have a placeholder for an image or video that is loading, but then have the element adjust to the actual aspect ratio of the image/video once it is loaded. |
| 93 | + |
| 94 | +## Conclusion |
| 95 | + |
| 96 | +The `aspect-ratio` CSS property is a simple property, but it has tons of use cases (especially around responsive design). Also, it is available in all major browsers so you can use it today without any issues. |
0 commit comments