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.
- Scenario 1: Screen width is 638px: This matches my expectation.
- Scenario 2: Screen width is 639px: This is wrong! I expect
column639still to be hidden! - Scenario 3: Screen width is 640px: This matches my expectation.
- Scenario 4: Screen width is 641px: This matches my expectation.
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:
- Google Chrome: I can reproduce, (
86.1.6938.200,64-bit) - Mozilla Firefox: I can reproduce, (
87.0.4280.88,64-bit) - JSFiddle: I cannot reproduce (all scenarios as expected): https://jsfiddle.net/vop23ang/
- CodePen: I can reproduce (all scenarios as expected, you need to Change View to resize horizontally): https://codepen.io/nikolas-charalambidis/pen/YzGYPGE
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:

Scenario 2: Screen width is 639px:



