Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-media-queries] css media query width and window.innerWidth may not be equal #4713

Open
mjf-g opened this issue Jan 29, 2020 · 2 comments

Comments

@mjf-g
Copy link

mjf-g commented Jan 29, 2020

According to the CSS specifications, both of them are the width of the viewport: [1], [2]. So they should be equal. But the window.innerWidth is a signed integer while media queries can specify float precision: [1], [3].

So they may not be equal, for example:
Web page zoom level: 300%;
Physical pixel width: 1000px;
"window.matchMedia('(width: ' + window.innerWidth + 'px)').matches" will be set to false.

References:

  1. https://www.w3.org/TR/css3-mediaqueries/#width
  2. https://drafts.csswg.org/cssom-view/#dom-window-innerwidth
  3. https://drafts.csswg.org/cssom-view/#ref-for-dom-window-innerwidth
@fantasai fantasai added the mediaqueries-4 Current Work label Mar 11, 2020
@frivoal
Copy link
Collaborator

frivoal commented May 28, 2020

I suppose we could specify that media queries round up the the closest integer pixel value before doing the comparison, but while this would solve this particular problem, this would cause the opposite problems when using inequalities:

So they may not be equal, for example:
Web page zoom level: 300%;
Physical pixel width: 1000px;
"window.matchMedia('(width > 333px)').matches" would return false, even though it is actually true.

Hopefully we can fix this problem on the cssom side, but even if we cannot, given that width mediaqueries are more useful for inequalities than for equalities, I'm inclined to keep the behavior of mediaqueries unchanged.

@frivoal frivoal added Agenda+ and removed mediaqueries-4 Current Work labels May 28, 2020
@css-meeting-bot
Copy link
Member

The CSS Working Group just discussed css media query width and window.innerWidth may not be equal, and agreed to the following:

  • RESOLVED: No change to @media width/height
The full IRC log of that discussion <heycam> Topic: css media query width and window.innerWidth may not be equal
<heycam> github: https://github.com//issues/4713
<heycam> florian: MQs lets you test the width (and height)
<heycam> ... of a page, and the MQ works in terms of floating point numbers
<heycam> ... there is also window.innerWidth, which relates to the same dimension, but it's an integer
<heycam> ... the fact that they're different is weird
<heycam> ... if you test the MQ against a value against the window.innerWidth you may get surprising results
<heycam> ... a strict equality MQ could fail
<heycam> ... in practice min-/max- is better. but this is odd, not sure what to do about that
<heycam> Rossen_: this is when you use window.matchMedia() with a value from window.innerWidth?
<Rossen_> "window.matchMedia('(width > window.InnerWidth)') << this is a problem
<heycam> dbaron: I think one thing that could be done is to propose an API that is better than window.innerWidth, and returns the float values
<heycam> Rossen_: I guess the questions that is MQ-scoped, is there anything we should be looking at doing for the MQ width or not?
<dbaron> (I think that's a thing that could be done by anyone who thinks this is an issue for them!)
<heycam> florian: I would like to not change it. but given I don't know how/whether to solve it...
<heycam> ... so would rather go in dbaron's direction
<heycam> ... rather than change MQs to work on integers
<heycam> Rossen_: the usage of @media width is very high
<heycam> ... making a change would be very disruptive, don't think we have that option
<heycam> Rossen_: any objections to not change @media width/height?
<heycam> RESOLVED: No change to @media width/height
<Rossen_> Zakim, end meeting
<Zakim> As of this point the attendees have been Rossen_, plinss, fantasai, stantonm, dbaron, faceless2_, GameMaker, jensimmons, heycam, miriam, chris, chrishtr, dino, drousso, leaverou,

chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Jun 4, 2020
…reater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Jun 5, 2020
…reater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1@xiaomi.com>
Cr-Commit-Position: refs/heads/master@{#775490}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this issue Jun 5, 2020
…reater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1@xiaomi.com>
Cr-Commit-Position: refs/heads/master@{#775490}
pull bot pushed a commit to Yannic/chromium that referenced this issue Jun 5, 2020
…reater than 1."

This reverts commit 6c7fa9e.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1@xiaomi.com>
Cr-Commit-Position: refs/heads/master@{#775490}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this issue Jun 13, 2020
…work if devicePixelRatio is greater than 1.", a=testonly

Automatic update from web-platform-tests
Revert "CSS @media (width: *px) may not work if devicePixelRatio is greater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1@xiaomi.com>
Cr-Commit-Position: refs/heads/master@{#775490}

--

wpt-commits: 7e369076c1a0f9549310f0229eb202e58c14382b
wpt-pr: 23974
xeonchen pushed a commit to xeonchen/gecko that referenced this issue Jun 13, 2020
…work if devicePixelRatio is greater than 1.", a=testonly

Automatic update from web-platform-tests
Revert "CSS @media (width: *px) may not work if devicePixelRatio is greater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1@xiaomi.com>
Cr-Commit-Position: refs/heads/master@{#775490}

--

wpt-commits: 7e369076c1a0f9549310f0229eb202e58c14382b
wpt-pr: 23974
gecko-dev-updater pushed a commit to marco-c/gecko-dev-comments-removed that referenced this issue Jun 15, 2020
…ork if devicePixelRatio is greater than 1.", a=testonly

Automatic update from web-platform-tests
Revert "CSS media (width: *px) may not work if devicePixelRatio is greater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokanchromium.org>
> Reviewed-by: David Bokan <bokanchromium.org>
> Cr-Commit-Position: refs/heads/master{#680047}

TBR=dchengchromium.org,bokanchromium.org,majinfeng1xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokanchromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1xiaomi.com>
Cr-Commit-Position: refs/heads/master{#775490}

--

wpt-commits: 7e369076c1a0f9549310f0229eb202e58c14382b
wpt-pr: 23974

UltraBlame original commit: da6eb0de8359a3dde49f73849690dd4a94101813
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified-and-comments-removed that referenced this issue Jun 15, 2020
…ork if devicePixelRatio is greater than 1.", a=testonly

Automatic update from web-platform-tests
Revert "CSS media (width: *px) may not work if devicePixelRatio is greater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokanchromium.org>
> Reviewed-by: David Bokan <bokanchromium.org>
> Cr-Commit-Position: refs/heads/master{#680047}

TBR=dchengchromium.org,bokanchromium.org,majinfeng1xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokanchromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1xiaomi.com>
Cr-Commit-Position: refs/heads/master{#775490}

--

wpt-commits: 7e369076c1a0f9549310f0229eb202e58c14382b
wpt-pr: 23974

UltraBlame original commit: da6eb0de8359a3dde49f73849690dd4a94101813
gecko-dev-updater pushed a commit to marco-c/gecko-dev-wordified that referenced this issue Jun 15, 2020
…ork if devicePixelRatio is greater than 1.", a=testonly

Automatic update from web-platform-tests
Revert "CSS media (width: *px) may not work if devicePixelRatio is greater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokanchromium.org>
> Reviewed-by: David Bokan <bokanchromium.org>
> Cr-Commit-Position: refs/heads/master{#680047}

TBR=dchengchromium.org,bokanchromium.org,majinfeng1xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokanchromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1xiaomi.com>
Cr-Commit-Position: refs/heads/master{#775490}

--

wpt-commits: 7e369076c1a0f9549310f0229eb202e58c14382b
wpt-pr: 23974

UltraBlame original commit: da6eb0de8359a3dde49f73849690dd4a94101813
mjfroman pushed a commit to mjfroman/moz-libwebrtc-third-party that referenced this issue Oct 14, 2022
…reater than 1."

This reverts commit 6c7fa9eae2437a99d8a4f1b0835030b688bc9285.

Reason for revert: w3c/csswg-drafts#4713

Original change's description:
> CSS @media (width: *px) may not work if devicePixelRatio is greater than 1.
>
> When devicePixelRatio is greater than 1, the viewport size of the CSS media
> query may appear as non-integers. This can happen in Chrome for Android if
> the UseZoomForDSF flag is enabled.
>
> For example:
> <meta name="viewport" content="width=device-width, initial-scale=1">
> <style>
>     @media (width: 393px) {
>         p {
>             color: red;
>         }
>     }
> </style>
> <p>text</p>
>
> If the device width is 1081 physical pixels and devicePixelRatio is 2.75. The
> viewport width of the CSS media query will be 393.091px.
>
> Change-Id: I25ee4da1ebdbd57202fa8c5e360138a8058f578f
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1695522
> Commit-Queue: David Bokan <bokan@chromium.org>
> Reviewed-by: David Bokan <bokan@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#680047}

TBR=dcheng@chromium.org,bokan@chromium.org,majinfeng1@xiaomi.com

Bug: 1043456
Change-Id: Idd69f034a59a3161b244c0921637e787118718ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2228028
Reviewed-by: David Bokan <bokan@chromium.org>
Commit-Queue: Jinfeng Ma <majinfeng1@xiaomi.com>
Cr-Original-Commit-Position: refs/heads/master@{#775490}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: c244cefe5c7761cf986c490f4e5ba06b702d06bd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants