htmlcssgoogle-chromeresponsive-designmedia-queries

CSS max-width has inaccurate pixel precision


I have an issue with max-width media rule. I test to hide the elements on different max-width values and the behavior is inconsistent. I have tested 4 scenarios varying in the screen width.

Question:

What causes the pixels inaccuracy in the Scenario 2 and why is 639px value treated differently than others?

Reproducibility:

Strangely, it cannot be reproduced everywhere (resize the window to the width of 693px:

Code:

<!DOCTYPE html>
<html>
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <h1>max-width issue</h1>
        <div class="column column639">
            <h2>Column 1</h2>
        </div>
          
        <div class="column column640">
            <h2>Column 2</h2>
        </div>
    </body>
</html>
.column {
    width: 100%;
    background-color: cyan;
}

@media screen and (max-width: 639px) {
    .column639 {
        visibility: hidden;
    }
}

@media screen and (max-width: 640px) {
    .column640 {
        visibility: hidden;
    }
}

Scenarios:

Scenario 1: Screen width is 638px: enter image description here

Scenario 2: Screen width is 639px: enter image description here

Scenario 3: Screen width is 640px: enter image description here

Scenario 4: Screen width is 641px: enter image description here


Solution

  • This was reported and accepted as a bug reproducible on multiple versions. Currently, it's on review by the development team.

    Follow the issue #1162102 for more details, however, I will update the answer once the issue is resolved.


    EDIT: Resolution as won't fix. The behavior for the 640px boundary is said unfortunate: https://issues.chromium.org/issues/40162653#comment37

    I have no clue as a non-FE developer on what to do...