What does the 'only screen' code mean in a css media query?
From https://www.w3schools.com/cssref/css3_pr_mediaquery.asp
only: The only keyword prevents older browsers that do not support
media queries with media features from applying the specified styles.
It has no effect on modern browsers.
The screen
keyword references that it's a computer, mobile phone or tablet etc. There are two other media types, print
and speech
, as well as the default all
.
Simply: The only
keyword is optional and used for backwards compatibility. The screen
keyword will default to all
.
HTML5 & CSS @media only screen and
You might not be seeing the changes due to the specificity of the rules.
That is, the way you have it written:
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
body {
background-color: yellow;
}
Both of these have the same specificity, meaning the last rule is what gets shown.
So, even if you are at a screen-size under 600px
, the background-color: yellow
will be displayed.
Instead, you should define the non-media query rule first, followed by the media query rule second.
Try this:
body {
background-color: yellow;
}
@media only screen and (max-width: 600px) {
body {
background-color: lightblue;
}
}
What is the difference between media print and media screen
It might sound daft or too obvious but:
Screen = For Screens
Print = For Print
Media screen will basically style screens and media print will effectively work for printable pages.
(ref.1)
(ref.2)
'@media only ' and '@media all' difference
Here you have an explanation about all the media types
http://www.w3.org/TR/CSS2/media.html
Hope this helps
Related Topics
How to Get the Footer to Stay At the Bottom of a Web Page
How to Easily Horizontally Center a ≪Div≫ Using Css
Select Elements by Attribute in Css
Footer At Bottom of Page or Content, Whichever Is Lower
Bootstrap Center Vertical and Horizontal Alignment
Vertical Align Center in Bootstrap 4
Font Scaling Based on Width of Container
Media Queries: How to Target Desktop, Tablet, and Mobile
Why Is a Flex Item Limited to Parent Size
Format Text in a ≪Textarea≫
Size of Html5 Canvas Via CSS Versus Element Attributes
How to Add a Scrollbar to an Html5 Table
Font Awesome Not Working, Icons Showing as Squares
How to Style the Html5 Form Validation Error Messages With Css