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

what is the difference between complete and finish
what is the difference between mass and weight
what is the difference between weather and climate
whats the difference between jokes
what is the difference between affect and effect
what is the difference between ram and rom
what is the difference between data and information
what is the difference between pandemic and epidemic

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)

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.

What is the difference between purpose and meaning?, One easy way to remember the difference between these two abbreviations is by employing a simple mnemonic device. Difference Between Amazon Go and Walmart (Comments Off on Difference Between Amazon Go and Walmart) In 2018, Amazon launched its first Amazon Go store and marked the beginning of a new retail era, a whole new shopping experience where shoppers could walk in, grab the stuff they want, and walk out without worrying about cash registers.

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.

What Is The Difference Between "It's" And "Its"?, What is the difference between isolation and quarantine? Isolation and quarantine are public health practices used to protect the public by preventing exposure to  Sativa and indica are the two main types of cannabis plants. The often-applied rule of thumb is that sativas are more invigorating and energizing, while indicas are more relaxing and calming

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

What's The Difference Between "ie" vs. "eg"?, While conversational use of these words might not require precise definitions, knowing the difference is important to help you better understand  Which vs. That: What’s the Difference in Usage? To understand when to use that or which, it’s important to understand clauses.A defining clause (also called an essential clause or a restrictive clause) gives information essential to the meaning of the sentence.

Answer by @hybrid is quite informative, except it doesn't explains 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 screen where as 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 second Style-sheet ignoring the first.

What is the difference between isolation and quarantine?, What is the difference between Alzheimer's and dementia? Get an overview of each and learn about early symptoms, risk factors, diagnosis and treatment. Virus vs bacteria: Any difference in symptoms? Symptoms usually reflect the area of the body infected, and the infecting organism. For example, a bacterial infection of the skin may cause a discharge, swelling, pain and redness in a certain area, whereas a viral infection, such as hepatitis C may cause abdominal pain, joint pain, nausea or

@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.

What's the difference between a pandemic, an epidemic, endemic , Both "What is the difference between X and Y?" and "What are the differences between X and Y?" are grammatical and will be understood. According to my  Bacteria and viruses can cause many common infections. But what are the differences between these two kinds of infectious organisms? Bacteria are tiny microorganisms that are made up of a single cell.

Dementia vs. Alzheimer's Disease: What Is the Difference?, Learn what the differences are between a Facebook page and a Facebook profile to help you choose what's right for your business. The names Great Britain and United Kingdom are often used interchangeably. However, they are not actually synonymous. The reason for the two names, and the difference between them, has to do with the expansive history of the British Isles. The British Isles are a group of islands off the northwestern coast of Europe. The largest of these

"What is the difference" or "what are the differences"?, One thing is for sure -- neither of them are "snowballs. is what you need to know about the difference between your Italian ice and snow cone. The primary difference between the two is the type of employer sponsoring the plans—401(k) plans are offered by private, for-profit companies, whereas 403(b) plans are only available to

What is the Difference Between a Facebook Page and Profile , The difference is not really tied to function calls (and you can spot the difference without making function calls). There's a difference between int j = ++i; and int k = i++; even when there's no function call involved.

Comments
  • 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.
  • 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.
  • what device doesnt have a screen?
  • @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.
  • @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.
  • 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?
  • Good, very lucid answer. Thanks!
  • Link shared was really helpful. Thanks
  • 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.
  • @PeterKnut Why are you even supporting such old devices? Genuinely curious.
  • I'm still not very clear on it. I've updated my question too. Can you give any example?
  • 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?
  • 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