Unusual CSS media queries behavior

During my project work, I faced a puzzling issue which can be illustrated with the following example:

Here is the sample CSS code:

    *, *::after, *::before {
    box-sizing: border-box;
    margin: 0;
    border: 0;
}

@media only screen and (max-width: 600px) {
    div {
        background: blue;
    }
}


@media only screen and (min-width: 601px) and (max-width: 1000px)  {
    div {
        background: green;
    }
}


@media only screen and (min-width: 1001px) {
    div {
        background: red;
    }
}

According to this code, my div should display as follows:

  • blue from 0px to 600px
  • green from 601px to 1000px
  • red from 1001px onwards

However, the actual rendering appears differently:

  • blue from 0px to 600px
  • white at 601px
  • green from 602px to 1000px
  • white at 1001 px
  • red from 1002px onwards

This discrepancy led me to believe that the (min-width:) condition is not inclusive.

In an attempt to resolve this issue, I modified the code to:

    *, *::after, *::before {
    box-sizing: border-box;
    margin: 0;
    border: 0;
}

@media only screen and (max-width: 600px) {
    div {
        background: blue;
    }
}


@media only screen and (min-width: 600px) and (max-width: 1000px)  {
    div {
        background: green;
    }
}


@media only screen and (min-width: 1000px) {
    div {
        background: red;
    }
}

With these adjustments, I anticipated the div's appearance to be:

  • blue from 0px to 600px
  • green from 601px to 1000px
  • red from 1001px onwards

Unexpectedly, the outcome was:

  • blue from 0px to 599px
  • green from 600px to 999px
  • red from 1000px onwards

This discovery suggested that the (min-width:) clause became inclusive.

Seeking further clarity, I revised the code once more:

    *, *::after, *::before {
    box-sizing: border-box;
    margin: 0;
    border: 0;
}

@media only screen and (max-width: 601px) {
    div {
        background: blue;
    }
}


@media only screen and (min-width: 601px) and (max-width: 1001px)  {
    div {
        background: green;
    }
}


@media only screen and (min-width: 1001px) {
    div {
        background: red;
    }
}

Surprisingly, it appeared that the (min-width:) condition was not all-inclusive yet again:

  • blue from 0px to 601px
  • green from 602px to 1001px
  • red from 1002px onwards

These results have left me feeling confused about how the conditions are interpreted.

Answer №1

According to the specification, both 'min' and 'max' prefixes are inclusive.

The majority of media features can use optional ‘min-’ or ‘max-’ prefixes to indicate "greater or equal to" and "smaller or equal to" constraints.

The problem arises when expecting pixel dimensions to be whole numbers, which is not always the case. This article delves deep into this issue:

You may believe that "Half a pixel? That's impossible", but under certain circumstances such as changing browser zoom levels, non-integer viewport sizes can occur and affect how media queries are applied on a page [...]

On newer Windows systems with higher resolutions, a default zoom level of 125% is set for text and icons, leading browsers like IE, Edge, and Firefox to adopt this value as their browser zoom, resulting in the manifestation of this bug on many Windows machines over the past few years.

For further insights, refer to the ongoing discussion about a similar issue in Bootstrap. A statement worth noting:

Chrome tends to round off decimal viewport widths even during zoom operations, possibly simplifying the application of media queries.

In conclusion, it might be best to choose between max-width or min-width, opt for overlapping rules, and let the latter rule take precedence.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The HTML modal window is experiencing loading issues

I am attempting to implement a modal box that appears on click in an HTML document. The HTML code looks like this: <a href="#openModal">Open Modal</a> <div id="openModal" class="modalDialog"> </div> <a href="#openModal">O ...

What is the best way to make my div equal in height to its preceding div sibling?

I want my new div to match the height of the previous one. I'm using percentages for width and height to ensure it displays properly on mobile devices as well. I've tried setting the parent elements to 100% width and height based on suggestions f ...

Issue with updating Angular list reference when deleting an item

My current task involves implementing a feature that displays selected items from a hierarchical structure on the right side. slice.component.ts : import { Component, Input, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core&a ...

Navigating the art of employing different fonts for a website

I am looking to incorporate the trainway font into my website, but I'm concerned that the user may not have it installed on their device. Is there a way to showcase text in a specific font without requiring the user to download and install it? Thank ...

Choosing from a dropdown menu by pressing keys

When I press the first letter of an option in my dropdown list, it works fine. However, if I try to press two, three, or four letters consecutively, the control vanishes. For example, if the option is 'Jquery' and I press J only, it works but pre ...

Transferring information from a component that includes slot content to the component within the slot content

This task may seem complex, but it's actually simpler than it sounds. I'm struggling with the correct terminology to enhance the title. I need to transfer data from a component that includes slot content to that slot content component. In partic ...

Is there a way to troubleshoot the issue pertaining to using routerLink in Angular version 17.2?

Currently working on a small app, but encountering an error with routerLink in the console. I am new to angular and seeking help. ✘ [ERROR] NG8002: Can't bind to 'routerLink' since it isn't a known property of 'a'. Here is ...

Executing a webservice method in an html page using javascript without the need to refresh the page

Is it possible to call a webservice from an index.html page using JavaScript? My webservice is located at "localhost/ws/service.asmx" and the specific web method I want to call is called HelloWorld. The index.html page contains an HTML submit button whic ...

When using the onclick function, an unexpected behavior occurs where instead of displaying content, the page

Below is a summarized version of my code: HTML <a onclick="showHideDiv('bio')" target='_self'> bio <div id="bio" class="shadowScreen" style="display: none;"> <p class="showBio"> Bio! </p> &l ...

What is the best method to remove the gap between my two horizontal div elements?

Is there a way to eliminate the small space between my two horizontal sections here? I've already attempted using margin: 0px; and padding: 0px; on the container, but it doesn't seem to be effective. Any assistance would be greatly appreciated! ...

Adding text to a Video Js fullscreen window on an iPad

Looking to enhance user experience on iPad while using a video js player, I want to make an image pop up upon completion of the video. Currently, I have it set up to work without full screen mode, but iPad users prefer watching videos in full screen. Is th ...

Slow and choppy animations with jQuery

I've been struggling with a slow and choppy jQuery animation despite trying various techniques to improve its speed. I've attempted stopping existing animations before starting new ones, ensuring animations are only performed when necessary, and ...

Artistic Canvas: Selected Image

I am trying to determine if the user has clicked on an image drawn in a canvas element. Despite clicking on the image, nothing seems to be happening. The alert function is not being triggered and the last condition in the code never evaluates to true. An ...

Issue with Custom Dropdown input not triggering blur event

I've been working on a custom dropup component and have encountered some strange issues with it. The main problem is the blur event not firing when clicking outside of the input field. My goal is to hide the options elements on blur, reset the compon ...

What is the best way to add an image element as a child of a list item only when its sibling checkbox is checked, upon clicking another button?

As a beginner in jQuery, I am working on developing a game where users can select and check 3 images from a list of ten. Following this selection, I aim to have the checked images added to a separate unordered list that I have already set up. <ul id="i ...

Problem with (click) event not triggering in innerHtml content in Angular 4

For some reason, my function isn't triggered when I click the <a... tag. Inside my component, I have the following code: public htmlstr: string; public idUser:number; this.idUser = 1; this.htmlstr = `<a (click)="delete(idUser)">${idUser}&l ...

clicking on the anchor does not trigger the modal to open

Below is an anchor tag I have set up: <a href="#" data-toggle="modal" data-target="#modalTC">terms and conditions</a> Here is the structure of my modal: <!-- Modal --> <div class="modal fade" id="modalTC" role="dialog"> <di ...

When the input field is not selected, switch it to display as plain text

I have a table where the cells become editable when clicked on by a user. I want the editable area to revert back to plain text with the updated value once the user deselects the text field. Is it possible to utilize AJAX to update a database with the new ...

The alignment of Bootstrap 4 cards is not coming together as expected

Good morning, I'm currently working on organizing my Bootstrap card elements into rows of 3 cards each. I want the cards to be uniform in height and width, with spacing between them. For instance, if I have 7 cards, I'd have 3 rows of three card ...

The CSS hover effect is not being implemented correctly. Can you identify the specific issue that is causing this problem?

I am working on creating a dynamic Search List for the Search Bar that changes color when hovered over and gets selected when clicked from the displayed item list. searchlist.jsx: import React from 'react' import { FaSearch } from 'react-ic ...