Skip to content

Getting the current source size value of an img #6532

Open

Description

@eeeps

Sizes is hard and debugging it is hard.

Being able to automate debugging without implementing a sizes parser would be nice. I'd like to be able to compare img.clientWidth with something (img.currentSizes?) that gave the current source size value in px. So, when:

window.innerWidth = 1000

this img

<img sizes="(min-width: 600px) 80vw, 100vw">

would have an currentSizes (or whatever) of 800.

Activity

  1. tabatkins commented on Mar 25, 2021

    @tabatkins
    Contributor

    Would this be different from calling img.getBoundingClientRect().width?

  2. eeeps commented on Mar 25, 2021

    @eeeps
    ContributorAuthor

    Yes; I want something that will return 300 in this example, rather than 100.

    Then, I can compare that something to img.getBoundingClientRect().width to see how wrong my sizes is (at this viewport size).

  3. eeeps commented on Mar 26, 2021

    @eeeps
    ContributorAuthor

    Spent a morning figuring out what I can do on my own: https://codepen.io/eeeps/pen/MWJyJKG?editors=1111. Not sure what I have is correct...

    (This would be a lot cleaner/easier with something like w3c/css-houdini-drafts#798)

    In any case, turning a source-size-value into an integer representing the resolved sizes value in px isn't trivial, and it would be nice if the browser, which has already done the conversion as part of srcset selection, exposed the value directly.

    A couple of other thoughts:

  4. zcorpan commented on Mar 26, 2021

    @zcorpan
    Member

    This makes sense to me, and it seems easy for browsers to expose this information, and as demonstrated, relatively hard for web developers to get right. If you want to get it right including error handling or CSS syntax edge cases (e.g. /* , */), it's really hard to get right (or you'd need to pull in a CSS parsing library).

  5. tabatkins commented on Mar 30, 2021

    @tabatkins
    Contributor

    Yeah, the use-case seems a little narrow, but reasonable, and the cost to expose it (pretty low) vs the cost to figure it out yourself (pretty high) seems to lean towards it being worthwhile to add.

  6. zcorpan commented on Mar 30, 2021

    @zcorpan
    Member

    So... isn't this already exposed by naturalWidth though? (and naturalHeight for height)

  7. eeeps commented on Apr 1, 2021

    @eeeps
    ContributorAuthor

    @zcorpan Good point!

    When your w descriptors are accurate and you're using srcset and sizes, yes.

    Sometimes, though, no. Here are some tests of some edge cases I could think of: https://codepen.io/eeeps/pen/bGgqExq

    For linting purposes, I care a bit about the "incorrect w descriptor" case.

    When it comes to client hints (linting them, or polyfilling them), I care a lot about the "no srcset" case.

    I think all of the ❌s would be ✅s if we did #3981, though?

  8. nhoizey commented on Apr 6, 2021

    @nhoizey

    @eeeps I like this img.currentSizes idea, sizes is indeed complex to debug.

    I wonder if the code from the parse-sizes package (used in this "Sizes" extension for Firefox and Chrome) could help build something, while it's not native yet.

  9. tigt commented on Feb 3, 2022

    @tigt

    If both Chrome and FF are using a package to provide something, that seems like a pretty strong vote for platform functionality

  10. eeeps commented on Jun 27, 2023

    @eeeps
    ContributorAuthor

    I think sizes=auto (and its emerging complexities) makes this even more important. Developers are going to assume that sizes=auto is doing the right thing when if they're not careful, it won't. cc) @zcorpan

    I'm imagining a Lighthouse test or whatever that can check if sizes.currentSizes === img.clientWidth onload for imgs with sizes=auto. If not, there's been some hard-to-see problem.

  11. austinh commented on Jun 11, 2026

    @austinh

    We came across a need for this at airbnb: #12565

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions