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/
Thank you Hugo, but I have a doubt, what do you mean by screen type media? could you explain me?
– Renan
Can give an example of when the condition
screen and (max-width: 770px)
it is valid thatonly screen and (max-width: 770px)
is not (to demonstrate the difference that theonly
ago)?– Woss
@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.– hugocsl
@Andersoncarloswoss gave the example in the comment, but I will include in the reply with more details, worth the touch
– hugocsl
@hugocsl Thank you!
– Renan
@hugocsl But in this case only
print
instead ofonly print
is not enough? https://output.jsbin.com/lotecixemi– Woss
@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
@hugocsl Exactly. I could not visualize a situation in which the
only
it would really be necessary.– Woss
@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.
– hugocsl
@hugocsl congratulations on the explanation, super didactic and answered everything I needed. Thank you
– Renan
@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
– hugocsl