Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between HTML LINK Media and CSS Media Queries

I know there are 2 ways to add Media queries:

HTML LINK:

<LINK REL="stylesheet" TYPE="text/css" MEDIA="(max-width: 1024px)" HREF="foo.css">

CSS:

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

I have read the documentation and I understand the obvious difference between the 2 methods. However, the following are 2 questions I am in doubt about if you can clarify please:

  1. Does the browser handle the HTML Media Link differently to the CSS Media Query? What I mean is, I know if CSS media queries are added inside css, all the css files are downloaded to all devices anyways and only the respective media queries are taken into effect when the browser interprets the compiled css. But if the Media Link is added in HTML, does it mean that browsers will only download the foo.css only when for devices with matching specified width? Is there a difference in the way browser handles the HTML media links when compared to Css media queries or is it all the same but just different ways of adding to the webpage?

  2. Lets say if foo.css 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 HTML Link like this:

<LINK REL="stylesheet" TYPE="text/css" MEDIA="(max-width: 1024px)" HREF="foo.css">

Would this become nested media query the way browsers look at it? What I dont understand is, if the above is added using html link, I dont know if the browser will actually look at it like this which becomes invalid:

@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;
        } 

    }

}

So my question is, if I have further media queries inside the css file that is added using HTML media link, is that valid?

EDIT: I had a look in the developer tool using chrome from my desktop and I can see that the tablet files are downloaded even when browsed from a desktop device:

  1. So for question 1, is it safe to assume all browsers included older ones and mobile browsers do the same thing i.e download all files even if they are placed at HTML links?

  2. For question 2, I can see that chrome does use the media queries that are inside tablet's css when the browser screen is resized to tablet width. The css file linked for 1024px in html are taken as media="(max-width: 1024px)". But then, wouldn't that mean the media queries placed inside tablet's css file are actually nested media queries? Although it works, isnt it logically wrong? Does some stricter browser not consider this as valid?

like image 850
Neel Avatar asked Sep 07 '14 14:09

Neel


People also ask

Do media queries go in HTML or CSS?

Media queries are commonly associated with CSS, but they can be used in HTML and JavaScript as well.

What is media query in HTML and CSS?

A media query consists of a media type and can contain one or more expressions, which resolve to either true or false. @media not|only mediatype and (expressions) { CSS-Code; }

What is media link in HTML?

The HTML link media attribute is used to specify what media/device the target resource is optimized for. This attribute used to specify a different style for different media type. The media attribute can accept several values. Syntax: <link media="value">

What is the use of media queries in CSS?

Media queries allow you to apply CSS styles depending on a device's general type (such as print vs. screen) or other characteristics such as screen resolution or browser viewport width. Media queries are used for the following: To conditionally apply styles with the CSS @media and @import at-rules.


3 Answers

With HTML media queries, the CSS files are downloaded whether or not the media query is satisfied or not. But the prasing of unwanted CSS is kind of deferred and this advances your initial render. In a way, you can think of making it, non-render blocking. But with CSS media queries, they are completely parsed and processed whether or not the query is satisfied.

like image 187
Shihabudheen US Avatar answered Oct 05 '22 22:10

Shihabudheen US


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)">
  • Apparently, 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
like image 24
Salman A Avatar answered Oct 06 '22 00:10

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 24
bfontaine Avatar answered Oct 06 '22 00:10

bfontaine