Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Media Queries -> Link vs @media [duplicate]

I know that media queries can be declared within either HTML or CSS:

HTML LINK:

<link rel="stylesheet" type="text/css" href="foo.css" media="(max-width: 1024px)">

CSS:

@media all and (max-width: 1024px) {
    ...
}

I have read the documentation and I think I understand most of the differences between the two methods. However, I'm still left with the following questions:

  1. For HTML linked resources with media queries, will browsers download the linked resource only when the media query's conditions are met?
  1. Lets say if a CSS file also has media queries for smaller widths other than 1024px, something like this:

    body {
        padding: 10px;
    }     
    @media all and (max-width: 900px) {
        body {
            padding: 5px;
        }     
     }    
    @media all and (max-width: 800px) {
        body {
            padding: 0px;
        }     
    }
    

    If the above file is added using an HTML link like this:

    <link rel="stylesheet" type="text/css" htef="foo.css" media="(max-width: 1024px)">
    

    Would this be treated as a nested media query? In other words, will the browser interpret it like this?

    @media all and (max-width: 1024px) {
        body {
            padding: 10px;
        } 
    
        @media all and (max-width: 900px) {
            body {
                padding: 5px;
            }   
        }
    
        @media all and (max-width: 800px) {
            body {
                padding: 0px;
            }   
        }
    }
    

    I may have further media queries inside a CSS file that is linked with a media query condition that I would want to apply as well.

like image 995
Neel Avatar asked Feb 03 '26 16:02

Neel


2 Answers

Here is what W3C has to say about this:

The media attribute says which media the resource applies to. The value must be a valid media query.

[...]

However, if the link is an external resource link, then the media attribute is prescriptive. The user agent must apply the external resource when the media attribute's value matches the environment and the other relevant conditions apply, and must not apply it otherwise.

Note: The external resource might have further restrictions defined within that limit its applicability. For example, a CSS style sheet might have some @media blocks. This specification does not override such further restrictions or requirements.

I tested the behavior in Chrome using the following markup:

<link rel="stylesheet" href="ge-960.css" media="screen and (min-width: 960px)">
<link rel="stylesheet" href="lt-960.css" media="screen and (max-width: 959px)">
  • Chrome downloaded all CSS files regardless of screen resolution. However, it applied the rules from matching stylesheet(s) only
  • And it honored all matching @media rules within the stylesheet declared with a media attribute
like image 192
Salman A Avatar answered Feb 06 '26 07:02

Salman A


Regarding the stylesheet download, here is what the current spec draft says:

User agents should re-evaluate media queries in response to changes in the user environment, for example if the device is tiled from landscape to portrait orientation, and change the behavior of any constructs dependent on those media queries accordingly.

This means you can’t just evaluate each media-query and then download the appropriate stylesheets because the environment can change, causing the re-evaluation of these media-queries. I think it could be optimized, but for now all browsers download all stylesheets, regardless of media-queries.

For your second question, specs don’t mention any difference between HTML- and CSS-declared media-queries. Nested media-queries are allowed since CSS3, and putting @media-rules in a stylesheet which is already tagged with media="…" should be the same as a pure CSS nested media-query.

like image 28
bfontaine Avatar answered Feb 06 '26 08:02

bfontaine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!