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.
Media queries are a key component of 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, use the @media at-rule 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, 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@mediaany-hoverany-pointeraspect-ratiocolorcolor-gamutcolor-indexdevice-aspect-ratiodevice-heightdevice-widthdisplay-modedynamic-rangeforced-colorsgridheighthorizontal-viewport-segmentshoverinverted-colorsmonochromeorientationoverflow-blockoverflow-inlinepointerprefers-color-schemeprefers-contrastprefers-reduced-dataprefers-reduced-motionprefers-reduced-transparencyresolutionscanscriptingupdatevertical-viewport-segmentsvideo-dynamic-rangewidth
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
Glossary terms
Guides
- Using media queries
-
Introduces media queries, their syntax, and the operators and media features used to construct media query expressions.
- Learn: Media query fundamentals
-
Introduction to media queries and approaches for using them to create responsive designs.
- Testing media queries
-
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
-
Learn how media queries can help users understand your website better.
- Printing
-
Tips and techniques for helping improve web content printer output.
- Responsive images
-
Learn how to use media queries with
sizesto implement responsive image solutions on websites.
Related concepts
- CSS containment module
- CSS conditional rules module
@supportsat-rule- Using feature queries
- CSS environment variables
env()function
- CSS paged media module
@pageat-rule
- CSS object model module
MediaQueryListinterfaceMediaListinterfacemediaTextproperty
MediaQueryListEventobject
- Device Posture API
device-posturedescriptor
- HTML
- SVG
mediaattribute
Specifications
| Specification |
|---|
| Media Queries Level 3> |
| Media Queries Level 4> |
| Media Queries Level 5> |
See also
- Container queries
- Using the
srcsetandsizesattributes - CSS paged media
- Use
@supportsto apply styles that depend on browser support for various CSS technologies.