@media
Quick Summary for @media
The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.
Code Usage for @media
/* At the top level of your code */ @media screen and (min-width: 900px) {   article {     padding: 1rem 3rem;   } }  /* Nested within another conditional at-rule */ @supports (display: flex) {   @media screen and (min-width: 900px) {     article {       display: flex;     }   } } 
More Details for @media

@media

The @media CSS at-rule can be used to apply part of a style sheet based on the result of one or more media queries. With it, you specify a media query and a block of CSS to apply to the document if and only if the media query matches the device on which the content is being used.

Note: In JavaScript, the rules created using @media can be accessed with the CSSMediaRule CSS object model interface.

Syntax

The @media at-rule may be placed at the top level of your code or nested inside any other conditional group at-rule.

/* At the top level of your code */ @media screen and (min-width: 900px) {   article {     padding: 1rem 3rem;   } }  /* Nested within another conditional at-rule */ @supports (display: flex) {   @media screen and (min-width: 900px) {     article {       display: flex;     }   } } 

For a discussion of media query syntax, please see Using media queries.

Description

Media types

Media types describe the general category of a device. Except when using the not or only logical operators, the media type is optional and the all type is implied.

all

Suitable for all devices.

print

Intended for paged material and documents viewed on a screen in print preview mode. (Please see paged media for information about formatting issues that are specific to these formats.)

screen

Intended primarily for screens.

Note: CSS2.1 and Media Queries 3 defined several additional media types (tty, tv, projection, handheld, braille, embossed, and aural), but they were deprecated in Media Queries 4 and shouldn't be used.

Media features

Media features describe specific characteristics of the user agent, output device, or environment. Media feature expressions test for their presence or value, and are entirely optional. Each media feature expression must be surrounded by parentheses.

any-hover

Does any available input mechanism allow the user to hover over elements? Added in Media Queries Level 4.

any-pointer

Is any available input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4.

aspect-ratio

Width-to-height aspect ratio of the viewport

color

Number of bits per color component of the output device, or zero if the device isn't color

color-gamut

Approximate range of colors that are supported by the user agent and output device. Added in Media Queries Level 4.

color-index

Number of entries in the output device's color lookup table, or zero if the device does not use such a table

device-aspect-ratio

Width-to-height aspect ratio of the output device. Deprecated in Media Queries Level 4.

device-height

Height of the rendering surface of the output device. Deprecated in Media Queries Level 4.

device-width

Width of the rendering surface of the output device. Deprecated in Media Queries Level 4.

display-mode

The display mode of the application, as specified in the web app manifest's display member. Defined in the Web App Manifest spec.

forced-colors

Detect whether user agent restricts color palette. Added in Media Queries Level 5.

grid

Does the device use a grid or bitmap screen?

height

Height of the viewport.

hover

Does the primary input mechanism allow the user to hover over elements? Added in Media Queries Level 4.

inverted-colors

Is the user agent or underlying OS inverting colors? Added in Media Queries Level 5.

monochrome

Bits per pixel in the output device's monochrome frame buffer, or zero if the device isn't monochrome.

orientation

Orientation of the viewport.

overflow-block

How does the output device handle content that overflows the viewport along the block axis? Added in Media Queries Level 4.

overflow-inline

Can content that overflows the viewport along the inline axis be scrolled? Added in Media Queries Level 4.

pointer

Is the primary input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4.

prefers-color-scheme

Detect if the user prefers a light or dark color scheme. Added in Media Queries Level 5.

prefers-contrast

Detects if the user has requested the system increase or decrease the amount of contrast between adjacent colors. Added in Media Queries Level 5.

prefers-reduced-motion

The user prefers less motion on the page. Added in Media Queries Level 5.

resolution

Pixel density of the output device.

scripting

Detects whether scripting (i.e. JavaScript) is available. Added in Media Queries Level 5.

update

How frequently the output device can modify the appearance of content. Added in Media Queries Level 4.

width

Width of the viewport including width of scrollbar.

Logical operators

The logical operators not, and, and only can be used to compose a complex media query. You can also combine multiple media queries into a single rule by separating them with commas.

and

Used for combining multiple media features together into a single media query, requiring each chained feature to return true for the query to be true. It is also used for joining media features with media types.

not

Used to negate a media query, returning true if the query would otherwise return false. If present in a comma-separated list of queries, it will only negate the specific query to which it is applied. If you use the not operator, you must also specify a media type.

Note: In Level 3, the not keyword can't be used to negate an individual media feature expression, only an entire media query.

only

Applies a style only if an entire query matches. It is useful for preventing older browsers from applying selected styles. When not using only, older browsers would interpret the query screen and (max-width: 500px) as screen, ignoring the remainder of the query, and applying its styles on all screens. If you use the only operator, you must also specify a media type.

, (comma)

Commas are used to combine multiple media queries into a single rule. Each query in a comma-separated list is treated separately from the others Thus, if any of the queries in a list is true, the entire media statement returns true. In other words, lists behave like a logical or operator.

Accessibility concerns

To best accommodate people who adjust a site's text size, use ems when you need a <length> for your media queries.

Both em and px are valid units, but em works better if the user changes the browser text size.

Also consider using Level 4 media queries to improve the user's experience. For example, prefers-reduced-motion to detect if the user has requested that the system minimize the amount of animation or motion it uses.

Security

Because media queries provide insights into the capabilities—and by extension, the features and design—of the device the user is working with, there is the potential that they could be abused to construct a "fingerprint" which identifies the device, or at least categorizes it to some degree of detail that may be undesirable to users.

Because of this potential, a browser may opt to fudge the returned values in some manner in order to prevent them from being used to precisely identify a computer. A browser might also offer additional measures in this area; for example, if Firefox's "Resist Fingerprinting" setting is enabled, many media queries report default values rather than values representing the actual device state.

Formal syntax

@media <media-query-list> {   <group-rule-body> }

where <media-query-list> = <media-query>#

where <media-query> = <media-condition> | [ not | only ]? <media-type> [ and <media-condition-without-or> ]?

where <media-condition> = <media-not> | <media-and> | <media-or> | <media-in-parens><media-type> = <ident><media-condition-without-or> = <media-not> | <media-and> | <media-in-parens>

where <media-not> = not <media-in-parens><media-and> = <media-in-parens> [ and <media-in-parens> ]+<media-or> = <media-in-parens> [ or <media-in-parens> ]+<media-in-parens> = ( <media-condition> ) | <media-feature> | <general-enclosed>

where <media-feature> = ( [ <mf-plain> | <mf-boolean> | <mf-range> ] )<general-enclosed> = [ <function-token> <any-value> ) ] | ( <ident> <any-value> )

where <mf-plain> = <mf-name> : <mf-value><mf-boolean> = <mf-name><mf-range> = <mf-name> [ '<' | '>' ]? '='? <mf-value> | <mf-value> [ '<' | '>' ]? '='? <mf-name> | <mf-value> '<' '='? <mf-name> '<' '='? <mf-value> | <mf-value> '>' '='? <mf-name> '>' '='? <mf-value>

where <mf-name> = <ident><mf-value> = <number> | <dimension> | <ident> | <ratio>

Examples

Testing for print and screen media types

@media print {   body { font-size: 10pt; } }  @media screen {   body { font-size: 13px; } }  @media screen, print {   body { line-height: 1.2; } }  @media only screen   and (min-width: 320px)   and (max-width: 480px)   and (resolution: 150dpi) {     body { line-height: 1.4; } } 

Introduced in Media Queries Level 4 is a new range syntax that allows for less verbose media queries when testing for any feature accepting a range, as shown in the below examples:

@media (height > 600px) {     body { line-height: 1.4; } }  @media (400px <= width <= 700px) {     body { line-height: 1.4; } } 

For more examples, please see Using media queries.

Specifications

Specification
Media Queries Level 4 # media-descriptor-table
CSS Conditional Rules Module Level 3 # at-media

See also

Using media queries In JavaScript, @media can be accessed via the CSS object model interface CSSMediaRule. Extended Mozilla media features Extended WebKit media features

Last modified: Jan 19, 2022, by MDN contributors

Select your preferred language English (US)DeutschEspañolFrançais日本語한국어PolskiPortuguês (do Brasil)Русский中文 (简体)正體中文 (繁體) Change language

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Categories in CSS
css
Search CSS
Search CSS by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

Subscribe to weekly updates about things i have added to the site or thought interesting during the last week.

You could also follow me on twitter or not... does anyone even use twitter anymore?

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote


Me
Random CSS Property

translateZ()

The translateZ() CSS function repositions an element along the z-axis in 3D space, i.e., closer to or farther away from the viewer. Its result is a <transform-function> data type.
translateZ() css reference