Media queries are a key component of responsive design that allow you to apply CSS styles depending on the presence or value of device characteristics.
It's common to apply a media query based on the viewport size so that layout choices can be made for devices with different screen sizes. For example, you may have a smaller font size for devices with small screens, increase the padding between paragraphs when a page is viewed in portrait mode, or increase the size of buttons on touchscreens.
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.
In HTML, media queries can be applied to various elements:
- In the
mediaattribute, they define the media to which a linked resource (typically CSS) should be applied.
- In the
mediaattribute, they define the media to which that source should be applied. (This is only valid inside
- In the
mediaattribute, they define the media to which the style should be applied.
Window.matchMedia() method to test the window against a media query.
You can also use
MediaQueryList.addListener() to be notified whenever the state of a query changes.
With this functionality, your site or app can respond to changes in the device configuration, orientation, or state.
You can learn more about programmatically using media queries in Testing media queries.
- Using media queries
Introduces media queries, their syntax, and the operators and media features which are used to construct media query expressions.
- Testing media queries programmatically
- Using media queries for accessibility
Learn how Media Queries can help users understand your website better.
|Media Queries Level 4 |
|CSS Conditional Rules Module Level 3 |