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

Content on the right floating underneath content on the left

Here is the HTML code snippet I am working with: <div id="main_content"> <div id="left_side> <h1> MY BIG HEADER </h1> </div> <div id="content_area"> SOME CONTENT HERE SOME CONTENT HERE S ...

Tips for arranging elements in proper order following a rotation

Having trouble aligning rotated divs? Let's say we rotate .straight by 30deg, and now we want to find the new offset coordinates of its bottom right corner. This way, we can perfectly match up the bottom left corners of .curve with this new coordinate ...

Is it possible for jquery to run an entire HTML file using ajax?

Based on information from a discussion on Stack Overflow, it seems that I can utilize the jquery getScript() method to run a remote JavaScript file loaded via Ajax. However, in my specific scenario, the script file is located within an HTML file. Is ther ...

Examining the scroll-down feature button

I'm currently experimenting with a scroll down button on my website and I'm perplexed as to why it's not functioning properly. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" c ...

Modify the bootstrap form dynamically in response to the user's input. Update the form layout instantly as the user types, with no need for clicking any buttons

Imagine a scenario where, as soon as you enter your credit card number, the form automatically undergoes a change without requiring a button click, similar to how a credit card logo pops up. The form detects changes instantly after the input field has be ...

When examining two arrays for similarities

I am dealing with two arrays within my component arr1 = ["one", "two"] arr2 = ["one", "two"] Within my HTML, I am utilizing ngIf in the following manner *ngIf="!isEnabled && arr1 != arr2" The isEnabled condition functions as expected, however ...

Fading colored images and backgrounds using Javascript

Although js and html are not my strong points, I am attempting to create two simple effects on a single page. As the user scrolls down the page, I want the background image or color to change as different divs come into view and then move off the screen. ...

Looking to design a popup using CSS styles

How can I design a pop-up window using CSS for a specific div? I also want to display additional information within another div inside the popup. ...

When a PHP if statement is triggered by reading a text file, it activates a checked

I am working on a PHP project that involves an on-off button and a txt file. The goal is to have the button disabled (with 'unchecked' echoed) when the value in the txt file is true, and enabled (with 'checked' echoed) when the value is ...

The jQuery script is functioning flawlessly in one specific direction

Currently, I am utilizing a basic script to alter the color of elements based on the div being hovered over. While this works smoothly in one direction down the line, when trying to reverse order and backtrack, the colors do not function as intended. The ...

Ways to navigate to a different page using HTML response from an AJAX request

After receiving an HTML document as a response from an AJAX call, I need to create a redirection page using this HTML response. Can anyone provide guidance on how to achieve this? ...

Transmit information using $broadcast when a button is clicked, and retrieve that information using $scope.$on

I am trying to create a function that will broadcast data from the server upon button click, and then navigate to a new route using $state.go('new-route'). In the controller of this new state, I want to retrieve the transmitted data. However, whe ...

Tips for displaying a pop-up when pressing a link:

I've spent a considerable amount of time on this project without making much progress. However, I came across a tutorial with a beautiful menu design that I decided to replicate. Here is the code: HTML: <!DOCTYPE html> <html> <head> ...

Animating range tick movements as the range thumb moves

My custom range input includes a span that displays the range's value and a div with multiple p elements acting as ticks. To create these ticks, I had to use a custom div because using "appearance: none" on the range hides the ticks by default. The ti ...

Implementing a NextJS button using the Link component

I am curious about how to incorporate a button inside a NextJS Link component. Usually, when you wrap the button in a Link and click on it, the onClick event is triggered and then it redirects to the value specified in the href attribute. Is there a way t ...

Troubleshooting Issue with Jssor Slider in Internet Explorer 11 due to Relative Dimensions

Encountering an issue with the relative dimensions of the slider container in IE11 (Jssor 18.0). Here is the HTML structure: An outer div with specified absolute dimensions (width and height in pixels). An inner div, serving as the slider's contain ...

Interactive search tool for toggling visibility of elements

Hello everyone, this is my initial post on StackOverflow. Keeping my fingers crossed that it goes smoothly! <input type="Text" id="filterTextBox" placeholder="Filter by name"/> <script type="text/javascript" src="/resources/events.js"></scr ...

CSS modified after opening a modal dialog that has loaded external HTML content

Within my ASP.NET MVC project, I am utilizing a tab wizard. On one of the tabs, I trigger a modal dialog by loading HTML content from an external API. However, once I close the wizard and navigate to the next tab, the table style (specifically border color ...

Only referring to a CSS class within a particular element

I have defined the following CSS: .grade a{ color: white; } It is working, but maybe too well... Now, I have an HTML structure like this: <li class="grade"> <a><i class="fa fa-star fa-fw"></i></a> My Text < ...

HTML- Any suggestions on how to troubleshoot my sticky navbar not functioning properly?

I'm having trouble creating a sticky navbar. I've attempted to use z-index: 999 but it's not behaving as expected. * { margin: 0; padding: 0; } .navbar { display: flex; align-items: center; justify-items: center; position: ...