Tagged “aspectratio”
Quick circle (by Adam Argyle)
I’m wary of list-based clickbait – and Adam’s recent 6 CSS snippets every front-end developer should know in 2023 feels like that – however I like this modern and minimal approach to creating a circle. I’ve previously seen aspect-ratio: 1
used to create a square box and it’s a lovely shorthand for “make width and height equal”. It makes sense that you can use it for a circle too, just by adding some border-radius
.
Avoiding img layout shifts: aspect-ratio vs width & height attributes (on Jake Archibald's blog)
Recently I’ve noticed some developers recommending using the CSS aspect-ratio
property directly on images. My understanding of aspect-ratio
was that it’s not so much intended for elements like img
which already have an intrinsic aspect ratio, but rather for the likes of div
which do not. Furthermore, when the goal is to prevent the layout shift that can occur after an image loads we should supply our images with width
and height
HTML attributes rather than using CSS.
In this timely post, Jake helpfully explains how width
and height
attributes are used by CSS as presentation hints to automatically set an aspect-ratio
that will also, in cases where the attributes were set wrongly, fall back to the image’s intrinsic aspect ratio. Therefore, concentrating on HTML alone is ideal for our content images. My previous approach seems sound but I now know a little more about why.
A First Look at aspect-ratio (on CSS-Tricks)
Chris Coyier takes the new CSS aspect-ratio
property for a spin and tests how it works in different scenarios.
See all tags.