What is the difference between screen and only screen in media queries
Let's break down your examples step by step.This targets styles for devices with a window width of 632px or less. Typically, this refers to devices smaller than desktop screens.This specifies applying the styles to devices with the media type 'screen' and a window width of 632px or less. It is almost identical to the previous example, but explicitly defines the media type 'screen' instead of relying on the default. This is commonly used to exclude print media, as 'print' is the most common alternative media type to 'screen'. Link to W3C specificationHere, we directly quote a W3C statement to explain this: The keyword 'only' can also be used to hide stylesheets from older user agents. User agents must process media queries starting with 'only' as if the keyword were not present. Since there is no media type 'only', older browsers should ignore the stylesheet. This is illustrated in the referenced example from the W3C specification section 9. I hope this provides some insight into media queries. Editor's note: Be sure to check @hybrids for an excellent answer on how the keyword is truly handled. The following content is adapted from Adobe's documentation: The media queries specification also provides the keyword , intended to hide media queries from older browsers. Like , the keyword must appear at the start of the declaration. For example: Browsers that cannot parse media queries need a comma-separated list of media types, and the specification states they should truncate each value before the first non-alphanumeric character that is not a hyphen. Thus, older browsers should interpret the previous example as: Since there is no media type 'only', the stylesheet will be ignored. Similarly, older browsers should interpret: as: In other words, it should apply the styles to all screen devices, even if it doesn't understand the media query. Unfortunately, IE 6-8 failed to implement this specification correctly. It does not apply styles to all screen devices but completely ignores the stylesheet. Despite this behavior, it is still recommended to prefix media queries with when you want to hide styles from browsers that don't support media queries. Thus, using: and in IE 6-8 will have the same effect: both prevent the styles from being applied. However, they will still be downloaded. In browsers supporting CSS3 media queries, both versions will load the styles if the viewport width exceeds 401px and the media type is 'screen'. I'm not entirely certain which browsers that don't support CSS3 media queries require the version: versus to ensure it isn't interpreted as: For those who can access device labs, this would be a good test. To design styles for many smartphones with smaller screens, you can write: To prevent older browsers from viewing iPhone or Android phone stylesheets, you can write: Read this article for more information: http://webdesign.about.com/od/css3/a/css3-media-queries.htm @hybrid's answer is comprehensive, though it doesn't address @ashitaka's point about 'mobile-first approaches': 'If using a mobile-first approach, we first use mobile CSS, then use min-width to target larger sites. Should we not use the keyword in this case?' The purpose of is solely to prevent unsupported browsers from applying styles intended for other devices, as if it started with 'screen' rather than 'only'. If it starts with 'only', the stylesheet will be ignored. Consider this example: Without 'only', it still works because the desktop stylesheet will override the Android styles, but it creates unnecessary overhead. In this case, if the browser doesn't support it, it will fall back to the second stylesheet, ignoring the first. Here, sets the media type for screen size. For example, it specifies the maximum width of the display area as 480px. Thus, it targets screens rather than other available media types. Here, prevents older browsers that don't support media queries from applying the specified styles. CSS media queries are primarily used to apply different style rules based on media types and conditions. Among these selectors, and are common usages with subtle functional differences. The media type targets styles for computer screens, tablets, smartphones, and other screen devices. When using the keyword in media queries, it means the included styles apply only to devices with the screen media type. For example: In this example, the background color applies only when the device screen width is 600px or less. The keyword was added to prevent older browsers from misinterpreting media queries. The keyword specifies that certain styles should only be applied by browsers that support media queries, not all devices. Adding before ensures that browsers without CSS media query support do not apply the styles to all media types. In this example, using versus makes no difference for modern browsers—they both apply the background color to screens 600px or less. However, for older browsers that don't support CSS media queries, the styles are not applied. In summary, the main difference between and lies in backward compatibility handling. The keyword ensures older browsers ignore styles intended for unsupported media types, while modern browsers can safely ignore since they support media queries. With modern browsers widely adopted, this distinction is less critical, but it's still prudent to consider potential compatibility issues when coding.