CSS media queries
The CSS media queries module enables testing and querying of viewport values and browser or device features, to conditionally apply CSS styles based on the current user environment. Media queries are used in the CSS @media rule and other contexts and languages such as HTML and JavaScript.
The **CSS media queries** module enables testing and querying of viewport values and browser or device features, to conditionally apply CSS styles based on the current user environment. Media queries are used in the CSS `@media` rule and other contexts and languages such as HTML and JavaScript.
Media queries are a key component of [responsive design](/docs/Learn_web_development/Core/CSS_layout/Responsive_Design). They enable conditional setting of CSS styles depending on the presence or value of device characteristics. It's common to use a media query based on viewport size to set appropriate layouts on devices with different screen sizes — for example three columns on a wide screen or a single column on a narrow screen.
Other common examples include increasing the font size and hiding navigation menus when printing a page, adjusting the padding between paragraphs when a page is viewed in portrait or landscape mode, or increasing the size of buttons to provide a larger hit area on touchscreens.
In [CSS](/docs/Web/CSS), use the @media [at-rule](/docs/Web/CSS/Guides/Syntax/At-rules) to conditionally apply part of a style sheet based on the result of a media query. To conditionally apply an entire style sheet, use @import.
When designing reusable HTML components, you may also use [container queries](/docs/Web/CSS/Guides/Containment/Container_queries), which allow you to apply styles based on the size of a containing element rather than the viewport or other device characteristics.
## Reference
### At-rules and descriptors
- @import
- @custom-media
- @media
- any-hover
- any-pointer
- aspect-ratio
- color
- color-gamut
- color-index
- device-aspect-ratio
- device-height
- device-width
- display-mode
- dynamic-range
- forced-colors
- grid
- height
- horizontal-viewport-segments
- hover
- inverted-colors
- monochrome
- orientation
- overflow-block
- overflow-inline
- pointer
- prefers-color-scheme
- prefers-contrast
- prefers-reduced-data
- prefers-reduced-motion
- prefers-reduced-transparency
- resolution
- scan
- scripting
- update
- vertical-viewport-segments
- video-dynamic-range
- width
The CSS media queries level 5 module also introduces the `environment-blending`, `nav-controls`, and `video-color-gamut` `@media` descriptors. Currently, no browsers support these features.
> [!NOTE]
> CSS media queries level 4 deprecated three `@media` descriptors: device-aspect-ratio, device-height, and device-width.
### Data types and operators
- [`<media-types>`](/docs/Web/CSS/Reference/At-rules/@media#media_types)
- [`<media-features>`](/docs/Web/CSS/Reference/At-rules/@media#media_features)
- resolution
- [Logical operators](/docs/Web/CSS/Reference/At-rules/@media#logical_operators)
### Glossary terms
- [media](/docs/Glossary/Media/CSS)
- [media query](/docs/Glossary/Media_query)
## Guides
- [Using media queries](/docs/Web/CSS/Guides/Media_queries/Using)
- : Introduces media queries, their syntax, and the operators and media features used to construct media query expressions.
- [Learn: Media query fundamentals](/docs/Learn_web_development/Core/CSS_layout/Media_queries)
- : Introduction to media queries and approaches for using them to create responsive designs.
- [Testing media queries](/docs/Web/CSS/Guides/Media_queries/Testing)
- : Describes how to use media queries in your JavaScript code to determine the state of a device, and to set up listeners that notify your code when the results of media queries change (such as when the user rotates the screen or resizes the browser).
- [Using media queries for accessibility](/docs/Web/CSS/Guides/Media_queries/Using_for_accessibility)
- : Learn how media queries can help users understand your website better.
- [Printing](/docs/Web/CSS/Guides/Media_queries/Printing)
- : Tips and techniques for helping improve web content printer output.
- [Responsive images](/docs/Web/HTML/Guides/Responsive_images)
- : Learn how to use media queries with `sizes` to implement responsive image solutions on websites.
## Related concepts
- [CSS containment](/docs/Web/CSS/Guides/Containment) module
- @container at-rule
- [Using container queries](/docs/Web/CSS/Guides/Containment/Container_queries)
- [Using size and style container queries](/docs/Web/CSS/Guides/Containment/Container_size_and_style_queries)
- [CSS conditional rules](/docs/Web/CSS/Guides/Conditional_rules) module
- @supports at-rule
- [Using feature queries](/docs/Web/CSS/Guides/Conditional_rules/Using_feature_queries)
- [CSS environment variables](/docs/Web/CSS/Guides/Environment_variables)
- env() function
- [CSS paged media](/docs/Web/CSS/Guides/Paged_media) module
- @page at-rule
- [CSS object model](/docs/Web/API/CSS_Object_Model) module
- MediaQueryList interface
- matches property
- media property
- change event
- MediaList interface
- mediaText property
- MediaQueryListEvent object
- [Device Posture API](/docs/Web/API/Device_Posture_API)
- device-posture descriptor
- HTML
- `sizes` attribute for [`<img>`](/docs/Web/HTML/Reference/Elements/img#sizes), [`<link>`](/docs/Web/HTML/Reference/Elements/link#sizes), and [`<source>`](/docs/Web/HTML/Reference/Elements/source#sizes) for picture
- `media` attribute for [`<link>`](/docs/Web/HTML/Reference/Elements/link#media), [`<source>`](/docs/Web/HTML/Reference/Elements/source#media), and [`<style>`](/docs/Web/HTML/Reference/Elements/style#media) [HTML](/docs/Web/HTML)
- [`<meta name="viewport">`](/docs/Web/HTML/Reference/Elements/meta/name/viewport)
- SVG [`media`](/docs/Web/SVG/Reference/Attribute/media) attribute
## Specifications
## See also
- [Container queries](/docs/Web/CSS/Guides/Containment/Container_queries)
- [Using the `srcset` and `sizes` attributes](/docs/Web/HTML/Reference/Elements/img#using_the_srcset_and_sizes_attributes)
- [CSS paged media](/docs/Web/CSS/Guides/Paged_media)
- Use @supports to apply styles that depend on browser support for various CSS technologies.
Cite this
"CSS media queries". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy