Media queries - CSS: Cascading Style Sheets 编辑

Media queries let you adapt your site or app depending on the presence or value of various device characteristics and parameters.

They are a key component of responsive design. For example, a media query can shrink the font size on small devices, increase the padding between paragraphs when a page is viewed in portrait mode, or bump up the size of buttons 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. Use @import to conditionally apply an entire style sheet.

Media queries in HTML

In HTML, media queries can be applied to various elements:

  • In the <link> element's media attribute, they define the media to which a linked resource (typically CSS) should be applied.
  • In the <source> element's media attribute, they define the media to which that source should be applied. (This is only valid inside <picture> elements.)
  • In the <style> element's media attribute, they define the media to which the style should be applied.

Media queries in JavaScript

In JavaScript, you can use the 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.

Reference

At-rules

Guides

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
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.

Specifications

SpecificationStatusComment
Media Queries Level 5Editor's Draft
CSS Conditional Rules Module Level 3Candidate Recommendation
Media Queries Level 4Candidate Recommendation
Media QueriesRecommendation
CSS Level 2 (Revision 1)RecommendationInitial definition

See also

  • Use @supports to apply styles that depend on browser support for various CSS technologies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:117 次

字数:6035

最后编辑:6年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文