616

What is the difference between screen and only screen in media queries?

<link media="screen and (max-device-width: 480px)" rel="stylesheet" href="m.css" />
<link media="only screen and (max-device-width: 480px)" rel="stylesheet" href="m.css" />

Why are we required to use only screen? Does screen not itself provide enough information to be rendered only for screen?

I've seen many responsive websites using any of these three different ways:

@media screen and (max-width:632px)
@media (max-width:632px)
@media only screen and (max-width:632px)

6 Answers 6

679

Let's break down your examples one by one.

@media (max-width:632px)

This one is saying for a window with a max-width of 632px that you want to apply these styles. At that size you would be talking about anything smaller than a desktop screen in most cases.

@media screen and (max-width:632px)

This one is saying for a device with a screen and a window with max-width of 632px apply the style. This is almost identical to the above except you are specifying screen as opposed to the other available media types the most common other one being print.

@media only screen and (max-width:632px)

Here is a quote straight from W3C to explain this one.

The keyword ‘only’ can also be used to hide style sheets from older user agents. User agents must process media queries starting with ‘only’ as if the ‘only’ keyword was not present.

As there is no such media type as "only", the style sheet should be ignored by older browsers.

Here's the link to that quote that is shown in example 9 on that page.

Hopefully this sheds some light on media queries.

EDIT:

Be sure to check out @hybrids excellent answer on how the only keyword is really handled.

8
  • 33
    For anyone who was looking for a better explanation of why the only keyword will hide style sheets from older browsers, see the answer by @hybrid below. He explains it very well.
    – JMTyler
    Commented May 3, 2013 at 19:46
  • 2
    hybrid's answer is good, but I like this answer too because it addresses media queries inside CSS as opposed to only addressing the media attribute of the link element.
    – chharvey
    Commented Nov 2, 2015 at 17:04
  • 2
    what device doesnt have a screen?
    – Kokodoko
    Commented Mar 28, 2016 at 19:30
  • 4
    @Kokodoko ones that don't need one. But seriously, printers and screen readers don't necessarily have screens. Plus anything else currently or in the future that don't identify as screen such as anything listed in the other media types. Commented Mar 28, 2016 at 20:04
  • 1
    @Kokodoko CSS exists to create a separation of concerns between content and presentation. Presentation covers much more than just what can be seen on a screen. It's an important distinction to keep in mind regardless of what you might be designing for. Commented Mar 28, 2016 at 21:38
291

The following is from Adobe docs.


The media queries specification also provides the keyword only, which is intended to hide media queries from older browsers. Like not, the keyword must come at the beginning of the declaration. For example:

media="only screen and (min-width: 401px) and (max-width: 600px)"

Browsers that don't recognize media queries expect a comma-separated list of media types, and the specification says they should truncate each value immediately before the first nonalphanumeric character that isn't a hyphen. So, an old browser should interpret the preceding example as this:

media="only"

Because there is no such media type as only, the stylesheet is ignored. Similarly, an old browser should interpret

media="screen and (min-width: 401px) and (max-width: 600px)"

as

media="screen"

In other words, it should apply the style rules to all screen devices, even though it doesn't know what the media queries mean.

Unfortunately, IE 6–8 failed to implement the specification correctly.

Instead of applying the styles to all screen devices, it ignores the style sheet altogether.

In spite of this behavior, it's still recommended to prefix media queries with only if you want to hide the styles from other, less common browsers.


So, using

media="only screen and (min-width: 401px)"

and

media="screen and (min-width: 401px)"

will have the same effect in IE6-8: both will prevent those styles from being used. They will, however, still be downloaded.

Also, in browsers that support CSS3 media queries, both versions will load the styles if the viewport width is larger than 401px and the media type is screen.

I'm not entirely sure which browsers that don't support CSS3 media queries would need the only version

media="only screen and (min-width: 401px)" 

as opposed to

media="screen and (min-width: 401px)"

to make sure it is not interpreted as

media="screen"

It would be a good test for someone with access to a device lab.

4
  • 4
    So if we are talking about media queries in CSS files, we can drop "only", because older browsers don't understand media queries whatsoever, Can't we?
    – 1234ru
    Commented Jul 29, 2015 at 7:53
  • 1
    According to my experience, "only" is working as expected on Symbian OS (old Nokia touch phones). I used it a lot because those browser was really bad in CSS3 support, but it was still able to handle "only" and "min/max-width" in media queries right.
    – Peter Knut
    Commented Jun 22, 2018 at 7:40
  • @PeterKnut Why are you even supporting such old devices? Genuinely curious. Commented May 1, 2020 at 3:15
  • 3
    @Hasim, I wrote my comment in past tense: "I used it a lot" :) In current days, it is not necessery to support such archaic devices. I just wanted to say that only screen was usefull and in fact the only way how to avoid broken page because of tragic CSS3 support.
    – Peter Knut
    Commented May 2, 2020 at 8:23
48

To style for many smartphones with smaller screens, you could write:

@media screen and (max-width:480px) { … } 

To block older browsers from seeing an iPhone or Android phone style sheet, you could write:

@media only screen and (max-width: 480px;) { … } 

Read this article for more http://webdesign.about.com/od/css3/a/css3-media-queries.htm

3
  • 5
    I'm still not very clear on it. I've updated my question too. Can you give any example? Commented Dec 21, 2011 at 18:47
  • 5
    What if you use the Mobile First approach? So, we have the mobile css first and then use min-width to target larger sites. We shouldn't use the only keyword in that context, right?
    – Ashitaka
    Commented Aug 3, 2012 at 15:55
  • This answer was alot easy to understand! :) The only is just to keep the older versions such as IE 6 or opera 5 or 6 from loading the data that has to be presented for Android or Chrome 30 or IE 10..Exellent answer sandeep :) deserves a +1 Commented Oct 19, 2013 at 0:12
24

The answer by @hybrid is quite informative, except it doesn't explain the purpose as mentioned by @ashitaka "What if you use the Mobile First approach? So, we have the mobile CSS first and then use min-width to target larger sites. We shouldn't use the only keyword in that context, right? "

Want to add in here that the purpose is simply to prevent non supporting browsers to use that Other device style as if it starts from "screen" without it will take it for a screen whereas if it starts from "only" style will be ignored.

Answering to ashitaka consider this example

<link rel="stylesheet" type="text/css" 
  href="android.css" media="only screen and (max-width: 480px)" />
<link rel="stylesheet" type="text/css" 
  href="desktop.css" media="screen and (min-width: 481px)" />

If we don't use "only" it will still work as desktop-style will also be used striking android styles but with unnecessary overhead. In this case, IF a browser is non-supporting it will fallback to the second Style-sheet ignoring the first.

3
@media screen and (max-width:480px) { … } 

screen here is to set the screen size of the media query. E.g the maximum width of the display area is 480px. So it is specifying the screen as opposed to the other available media types.

@media only screen and (max-width: 480px;) { … } 

only screen here is used to prevent older browsers that do not support media queries with media features from applying the specified styles.

1
  • 1
    only is used to support older browsers that do not support media queries. So if we use only, the older browsers will support this query? Kinda confuse here.
    – MaXi32
    Commented Dec 1, 2021 at 23:51
1

What is the difference between "screen" and "only screen" in media queries?

Media queries is used to create responsive web design. Both the screen and only screen are used in media queries.

screen: It is used to set the screen size of media query. The screen size can be set by using max-width and min-width.

screen is used to style for many smartphones with smaller screens, you could write:

@media screen and (max-width:630px){ ... }

In the above code screen is saying for a screen and window size with a max-width of 630px, you want to apply said styles.

only screen: The only keyword is used to prevent older browsers that do not support media queries with media features from applying the specified styles.

To block older browsers from seeing the stylesheet, you will write:

@media only screen and (max-width:630px){ ... }

The keyword ‘only’ will hide style sheets from older user agents.

Not the answer you're looking for? Browse other questions tagged or ask your own question.