TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Aspect-ratio is great

362 pointsby damirover 3 years ago

18 comments

r_hoods_ghostover 3 years ago
&quot;The text content in the third box is longer than the available space, so rather than maintaining the aspect ratio, the element expands vertically to fit the content... If we set an explicit height instead, the element doesn’t expand, but instead we get overflow&quot;<p>Why? Why would you have different default behaviours if you have one of the dimensions set but not the other? Why would you not have this specified in another property, the overflow property perhaps? Oh look, lets define a new property and call it aspect ratio, and then lets ensure it doesn&#x27;t maintain an element&#x27;s aspect ratio if you have content that is going to overflow if you specify a width. This is what drives me mad about css, it&#x27;s gotchas all the way down.
评论 #30283263 未加载
评论 #30284194 未加载
评论 #30286090 未加载
评论 #30284251 未加载
评论 #30283740 未加载
评论 #30283205 未加载
Waterluvianover 3 years ago
Oh my god.<p>I spent so freaking much time trying to solve “make the outermost div fill the screen of the device but always keep it a specific ratio.”<p>Hours and hours. And hours.
评论 #30281004 未加载
Crazyontapover 3 years ago
This is really amazing. The last CSS thing I learned about was flex-box and it so vastly improved my side projects. I&#x27;m sure this will come quite handy too.<p>What are more such things which you love about CSS but only learned about recently? Can greatly benefit CSS dinosaurs like me who know CSS but haven&#x27;t really kept up on the recent developments.
评论 #30283162 未加载
评论 #30281461 未加载
评论 #30281209 未加载
评论 #30282320 未加载
评论 #30281143 未加载
评论 #30284620 未加载
评论 #30281145 未加载
评论 #30286110 未加载
dep_bover 3 years ago
I&#x27;ve stopped working on web projects about ten years ago and it&#x27;s still shocking how much was missing from even the most basic things compared to other UI frameworks that often had it for decades.<p>It&#x27;s quite sad that there&#x27;s apparently no other direction than &quot;JavaScript and the web will replace every type of application eventually&quot;. I mean look how well Microsoft Word (native) works compared to Microsoft Teams (some JS hodgepodge). And not just the day by day use, but also stuff like AppleScript support. Especially highlighted by the stand still we used to have on mobile computer CPU&#x27;s until recently. Everything just got slower and slower.
评论 #30285312 未加载
c-smileover 3 years ago
aspect-ratio is actually a great mistake - it breaks CSS box model again (after flexbox).<p>What should be the dimensions of this<p><pre><code> div { width:100px; height:100px; aspect-ratio: 3 &#x2F; 2; } </code></pre> ?<p>Instead, there should be &quot;functions&quot; width(N%) and height(N%) and so<p><pre><code> div { width:100px; height: width(66%); } div { height:100px; width: height(150%); } div { height:40px; line-height: height(100%); } div { width:100px; margin: width(10%); } </code></pre> Therefore, instead of one more property that &quot;solves&quot; only one particular problem we will have bunch of solutions by adding just two pseudo-functions. And without breaking box model in very nasty way.<p>For that matter, Sciter (<a href="https:&#x2F;&#x2F;sciter.com" rel="nofollow">https:&#x2F;&#x2F;sciter.com</a>) has these width()&#x2F;height() functions 10 or so years already.
评论 #30282224 未加载
评论 #30281914 未加载
评论 #30282458 未加载
评论 #30281917 未加载
评论 #30283286 未加载
评论 #30284234 未加载
评论 #30283188 未加载
arecurrenceover 3 years ago
This has been one of my absolute favorite CSS improvements in recent years. Upon first seeing it people don&#x27;t typically pick up on the magnitude of benefit that the feature offers. Once it sets in, however, you start seeing it everywhere. Image dimension management had been an enduring problem throughout CSS history but now aspect ratio is supported across the board <a href="https:&#x2F;&#x2F;caniuse.com&#x2F;mdn-css_properties_aspect-ratio" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;mdn-css_properties_aspect-ratio</a>.
评论 #30281473 未加载
runarbergover 3 years ago
I have used aspect-ration in production and I concur. Say you want to display a user avatar, but the user might not have uploaded their avatar, so you fallback with a colored box (basically a div) with the user’s initials instead (or—in practice—your best guess of their initials). aspect-ratio will cover both of these. So you can you don’t need to set a different weight or padding depending of if you have an image or a div.
_defover 3 years ago
I&#x27;m so happy to see directives like these making it into css! It&#x27;s great to have super granular control of every dimension and stuff, but in terms of creating layouts etc it really needs higher abstraction properties like this. I think this is why css was always so frustrating for me: caring about the technical side of things instead of the actual design implementation, which I now realize is not entirely my fault, but rather by design of the sepcs. (I still need to learn about layout fundamentals etc, but this certainly helps getting a grasp of things)
robgibbonsover 3 years ago
Back in the day my trick was to use a transparent PNG of just a few pixels in each dimension to establish an aspect ratio for a fluid-width div.
评论 #30283863 未加载
baxuzover 3 years ago
Not really. It&#x27;s still tied to the element&#x27;s width and will overflow vertically. Even if using a flex&#x2F;grid container with a different axis, it will only constraint the aspect ratio to its width.<p>Which means that if you want to have something like a video player in the viewport like YouTube does, or lightbox-style previews which keep the entire media element in viewport you&#x27;ll still have to use JS to limit the element&#x27;s height, or if the design allows for it, use viewport height units + calc.
supernesover 3 years ago
I was under the impression that this has been a thing for a long while, but I checked and it&#x27;s actually been available in mainstream browser versions only since January 2021 (September for Safari, and just nine days ago for Chrome on Android). Combined global usage for supported platforms is around 84%.<p><a href="https:&#x2F;&#x2F;caniuse.com&#x2F;mdn-css_properties_aspect-ratio" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;mdn-css_properties_aspect-ratio</a>
jrochkind1over 3 years ago
I really want to use aspect-ratio, but a recent thread on HN, that now I can&#x27;t find, has me worried that I shouldn&#x27;t be counting on CSS until its been supported by the browsers for a couple years.<p>The recent thread was about all the reasons that even &quot;evergreen&quot; browsers might still have plenty of people using old versions, and was on a thread about a Chromebox still in use that could not have it&#x27;s version of Chrome updated.
评论 #30287643 未加载
wruzaover 3 years ago
Articles like that always make me smile, because I imagine like 20 years ago someone would write a lenghty blog post about GtkAspectFrame or GtkHBox and its “expand” and “fill” child properties. Or about GtkSizeGroup (which css still can’t figure out). Gtk is awesome, so amazing!
gbromiosover 3 years ago
this is great, but what sticks out to me is how quickly this seems to have been rolled out. Unless I&#x27;m misreading something, this went from first public draft [1] to full support [2] in less than two years?<p>Maybe I just haven&#x27;t been paying attention to the rate new CSS features, but from my perspective this totally just dropped out of the sky. Here&#x27;s hoping we can continue this pace.<p>[1] <a href="https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;2020&#x2F;WD-css-sizing-4-20200526" rel="nofollow">https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;2020&#x2F;WD-css-sizing-4-20200526</a> [2] <a href="https:&#x2F;&#x2F;caniuse.com&#x2F;?search=aspect-ratio" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;?search=aspect-ratio</a>
crancherover 3 years ago
This will save thousands and thousands of designer&#x2F;developer&#x2F;producer hours.
radoover 3 years ago
Yes, until you realise it messes up sub pixel calculations, unlike the padding hack…
Tomteover 3 years ago
If you just want this to avoid layout shift, you&#x27;re still better off using explicit width and height attributes (on the img tag), because it is per-image in the HTML itself, not in a common CSS declaration. Is that right?
评论 #30284662 未加载
lhballover 3 years ago
Good read.<p>It’s kinda funny that, on a CSS focused site, the position of the “latest” article label on the homepage is set above that of the site logo and navigation bar.<p>It’s nice to know even CSS experts make mistakes ¯\_(ツ)_&#x2F;¯.
评论 #30283276 未加载