Use @media, @media screen and or @media only screen and Have a difference?

Asked

Viewed 4,406 times

16

I was wondering if there is any difference in css when using one of these media queries,

Example A:

@media(max-width:770px) {
    .texto{
        color:red
    }
}

Example B:

@media screen and (max-width: 770px) {
    .texto{
        color:red;
    }
}

Example C:

@media only screen and (max-width: 770px) {
    body {
        color:red;
    }
}

I ran tests here on every one of them, and they all worked.

Differs?

3 answers

20


Yes there is difference, up to pq not all the @media is screen, can be print for example

@media(max-width:770px) {
    .texto{
        color:red
    }
}

In the example above it means that for any media that the width is up to 770px, even if it is a TV, Monitor and even a Printer, the CSS rule contained there will be applied.

Already in this example below means that in media like screen and with screens up to 770px wide the rule will be applied. (this and is very important because the CSS is only applied if the two rules screen and max-width correspond)

@media screen and (max-width: 770px) {
    .texto{
        color:red;
    }
}

About the and

Note that your @media rule can be quite complex, and you can chain several parameters that should be met for the rule to be applied. For example:

@media only screen 
  and (min-device-width: 320px) 
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) {
    body { line-height: 1.4; }
}

Notice that you are "adding" requirements to the and, soon for this css to be applied need that:

a mídia tem que ser tela 
**e** ter no mínimo 320px de largura 
**e** no máximo 480px de largura
**e** a tela deve ter densidade de pixel de 2

If one of the requirements does not return true the rule is not applied

The and Operator is used for Combining Multiple media Features Together into a single media query, requiring each chained Feature to Return true in order for the query to be true. It is also used for Joining media Features with media types.


In this other example the selector only means that only for media screen and with screens up to 600px the rule will be applied. This rule will not apply to other media such as print for example

@media only screen and (max-width: 600px) {
    body {
        color:red;
    }
}

Example of use

Let’s assume that you have an element in the color Yellow, which appears right on the Computer Screen, but when printing the Yellow will not look cool and you would like it to be Black, how could you solve this? Simple, using @media only print. So in your CSS you would have something like:

div {
    background-color: yellow;
    width: 100px;
    height: 100px;
}

@media only print {
    div {
        background-color: black;
    }
}
<div></div>

Note that with the example above when you give a Ctrl+P to div It won’t turn up Yellow, it’ll turn black!

About the Only

The only Operator is used to apply a style only if an entire query Matches, and is Useful for Preventing Older browsers from Applying Selected Styles. If you use the only Operator, you must specify an Explicit media type.

In Portuguese
The operator only is used to apply a style only if an entire query matches and is useful to prevent older browsers from applying selected styles. If you use the operator only, should specify a type of explicit media. (in case if the media type is screen, print or speech)

A clarification on the Only

The keyword ːonly' can also be used to Hide style Sheets from Older user Agents. User Agents must process media queries.

It seems the attribute only can also be used for old browsers to ignore the css rule contained in @media, since they may not distinguish between different media types, see that they may not see difference between a @media screen of a @media print, for these browsers everything would be one thing. So to make them ignore the rule is to use the only.

In short: In browsers who understand the difference between print and screen there is no need for the only, however, in the browser that does not understand the differences between the media only so he won’t recognize the tag and ignore it.

Fonte1: https://developer.mozilla.org/en-US/docs/Web/CSS/@media
Fonte2: https://www.w3.org/TR/css3-mediaqueries/

  • 1

    Thank you Hugo, but I have a doubt, what do you mean by screen type media? could you explain me?

  • 1

    Can give an example of when the condition screen and (max-width: 770px) it is valid that only screen and (max-width: 770px) is not (to demonstrate the difference that the only ago)?

  • @Renanosorio exist printed media, and screen media. For example, you can have a yellow color for text on the screen, but you want the print to be black, then you use @media only print and put the color black so the text is more readable after printed.

  • @Andersoncarloswoss gave the example in the comment, but I will include in the reply with more details, worth the touch

  • 2

    @hugocsl Thank you!

  • @hugocsl But in this case only print instead of only print is not enough? https://output.jsbin.com/lotecixemi

  • 1

    @Andersoncarloswoss I think I understood what you meant, that only print would be redundant since only with print the rule would apply only in print itself and not on screen?

  • @hugocsl Exactly. I could not visualize a situation in which the only it would really be necessary.

  • @Andersoncarloswoss apparently Lgus browsers, I believe the older ones, do not distinguish between media types, so using the only in front of the rule browsers who do not understand this value will ignore the rule. Note the "also"... The keyword ?only' can also be used to Hide style Sheets from Older user Agents. User Agents must process media queries.

  • 1

    @hugocsl congratulations on the explanation, super didactic and answered everything I needed. Thank you

  • 1

    @Renanosorio I thought I could answer with a few lines :D, but after that was paying attention to the details I thought it could be a more complete rss response

Show 6 more comments

0

The big difference has to do with the resolutions you want the 1 line of code and for that resolution(assumes the resolution and adjusts to other screens) the 2 line and screen and the resolution (can read in other screen adjusting) and the 3 line of code and only for screen and that resolution(only reads on screens and that specific formatting) but the link explains better than me ( when I say screen are monitors like pc ) google dev info

-2

There is a difference. only is it good for anything today? Probably not.

According to the W3C Media Specification Document only serves to prevent specified styles from being displayed in older browsers that do not support media Features, such as "max-width" for example.

The CSS linked below, for example, would be ignored by browsers who do not understand the media Feature color.

<link rel="stylesheet" media="only screen and (color)" href="example.css" />

The keyword ːonly' can also be used to Hide style Sheets from Older user Agents. User Agents must process media queries.

https://www.w3.org/TR/css3-mediaqueries/

  • 2

    ONLY is of utmost importance as it says it is only for devices with Screen in the case of only screen, or if it is a CSS just for printing in the case of only print

  • My point is that the operator only is not strictly necessary to differentiate screen of print. You can do it just with @media screen and @media print. only the function of controlling the display of styles in legacy browsers. "The keyword only prevents older browsers that do not support media queries from applying data styles" https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/CSS_Media_queries

  • It seems that some browsers do not distinguish between media types, so for this only seems to cause these browsers to ignore the rule contained therein. Anyway I think it unwise not to use the only if you really want to be specific to a particular media

  • I disagree with your interpretation of the specification. The use is clear in "is Useful for Preventing Older browsers from Applying Selected Styles". You can be cautious and put, evil will not do, but definitely does not fit in utmost importance.

  • There is still the option of using the OR that in the case would be using comma separation, example: @media only screen and (min-device-width: 320px) , &#xA; only screen and (min-device-width: 321px)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.