CSS - 媒体查询



CSS中的媒体查询用于根据屏幕尺寸、分辨率和其他用户设备特性应用不同的CSS样式。媒体查询使用@media规则,在满足特定条件时包含额外的CSS属性块。媒体查询也可用于单独设置页面的可打印版本样式。

语法

@media not|only mediatype and (media feature) and (media feature) { CSS-Code; }

这里,media-type可以是screen、print、speech等,而media-feature可以是宽度、高度、纵横比、方向等特性。

你可能已经注意到,同一个网站在移动设备和桌面设备上的显示效果不同。这种屏幕相关的样式是使用CSS媒体查询实现的。在本教程中,我们将学习媒体查询及其相关的属性。

目录


 

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

媒体类型

媒体类型用于CSS媒体查询,根据设备应用不同的样式。最常见的媒体类型是allprintscreen。如果未指定媒体类型,则匹配所有设备。

  • all: 默认值。此值适用于所有媒体类型。
  • print: 此值仅在打印文档时有效。
  • screen: 此值仅对屏幕有效,例如计算机、平板电脑和智能手机。
JavaScript提供了一个名为CSSMediaRule的CSS对象模型接口,可用于访问使用@media CSS @规则创建的规则。

CSS媒体类型Print

有时为用户生成的打印版本不需要屏幕中显示的所有样式。打印版本通常以灰度样式或简单的浅色生成。建议这种设计,因为深色背景会消耗打印机更多的墨水。

示例

以下示例演示了使用媒体类型为print的CSS媒体查询。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body{ background-color: black; color: white; padding: 10px; } @media print { body { background-color: white; color: black; padding: 10px; } } button { background-color: aqua; padding: 5px; } </style> </head> <body> <h3> Tutorialspoint </h3> <p>CSS Media Type - Print</p> <p> Background is white for printable version of this page. Check out... </p> <button onclick="printPage()">Print Page</button> <script> function printPage() { window.print(); } </script> </body> </html>

CSS媒体类型All

这用于指定在所有屏幕尺寸、可打印版本和其他版本中使用的通用样式。

示例

以下示例演示了使用媒体类型为all的CSS媒体查询。

Open Compiler
<!DOCTYPE html> <html> <head> <style> @media all { body{ background-color: black; color: white; padding: 10px; } } </style> </head> <body> <h3>Tutorialspoint </h3> <p> CSS Media Type - All </p> <p> In printable version or any screen size, the styles of this page is same. </p> <button onclick="printPage()"> Print Page </button> <script> function printPage() { window.print(); } </script> </body> </html>

CSS媒体类型Screen

此值仅对屏幕有效,例如计算机、平板电脑和智能手机。

示例

以下示例演示了如果屏幕尺寸大于500px,背景颜色将更改为粉红色,文本颜色将更改为蓝色。

Open Compiler
<!DOCTYPE html> <html> <head> <style> .container { display: flex; flex-direction: row; padding: 10px; gap: 10px; } .child{ padding: 10px; background-color: #f0f0f0; border: 2px solid; } @media screen and (max-width: 500px) { .container { flex-direction: column; } } </style> </head> <body> <p> Orientation of child elements depend on screen size, for screen size less than 500px, children align in two different columns. </p> <div class="container"> <div class="child" > HTML </div> <div class="child"> CSS </div> </div> </body> </html>

媒体查询的宽度范围

在媒体查询中,也可以这样指定屏幕宽度范围。

@media (width > 700px) { /* Styles for screens that are at least 700px wide */ }

示例

以下示例演示了当屏幕宽度在600px到800px之间时,背景颜色更改为黄色,文本颜色更改为红色。

Open Compiler
<!DOCTYPE html> <html> <head> <style> p { color: blue; } @media (600px < width < 800px) { p { background-color: yellow; color: red; } } </style> </head> <body> <h1>Tutorialspoint</h1> <p>CSS Media Type - Screen</p> <p>Resize the browser size to see the effect.</p> </body> </html>

逗号分隔的媒体类型

媒体查询中的逗号类似于逻辑“OR”运算符。

以下声明将应用于屏幕宽度小于500px或可打印版本。也可以使用OR运算符代替逗号。

@media (min-width: 500px), print { /* Styles */ }

示例

以下示例演示了使用逗号分隔的媒体类型。在打印模式下以及屏幕尺寸>500px时,背景颜色会发生变化。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body { background-color: white; color: black; } @media (min-width: 500px), print { body { background-color: black; color: white; } } button { background-color: violet; padding: 5px; } </style> </head> <body> <h1>CSS Media Type - Screen and Print</h1> <p> Resize the window to less than 500px to see the background and font color changes to default. </p> <p> Click on below button to see the effect when you print the page. ( Enable background graphics options if any at print section ) </p> <button onclick="printPage()">Print Page</button> <script> function printPage() { window.print(); } </script> </body> </html>

仅使用CSS媒体类型

仅使用媒体类型仅在整个媒体查询匹配时才应用样式。这有助于防止旧版浏览器应用样式。

示例

以下示例演示了当屏幕宽度在500px到700px之间时,背景颜色更改为粉红色,文本颜色更改为蓝色。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body { color: red; } @media only screen and (min-width: 500px) and (max-width: 700px) { body { color: blue; background-color: pink; } } </style> </head> <body> <h1>Tutorialspoint</h1> <p>CSS Media Type - Screen</p> <p> Resize the browser window to see the effect. </p> </body> </html>

CSS媒体查询AND运算符

AND运算符用于组合媒体查询中的多个条件。这两个条件都必须为真,样式才能应用。

示例

此示例演示了当屏幕宽度在500px到700px之间时,将添加样式。

Open Compiler
<!DOCTYPE html> <!DOCTYPE html> <html lang="en"> <head> <style> body { background-color: lightgray; color: black; } @media (min-width: 500px) and (max-width: 700px) { body { background-color: lightgreen; color: blue; } } </style> </head> <body> <h1> Media Query with AND Operator</h1> <p> Resize the browser window between 500px and 700px to see the effect. </p> </body> </html>

媒体查询NOT运算符

NOT运算符否定媒体查询,仅当媒体查询不匹配时才应用样式。

NOT运算符在媒体查询中最后进行评估,因此上述媒体查询将按如下方式评估。

@media not (all and (monochrome)) { /* … */ } /* It's not evaluated like this:*/ @media (not all) and (monochrome) { /* … */ }

示例

在此示例中,NOT运算符否定条件width < 700。

Open Compiler
<!DOCTYPE html> <!DOCTYPE html> <html lang="en"> <head> <style> body { background-color: lightgray; color: black; } @media not screen and (width < 700px) { body { background-color: orange; color: white; } } </style> </head> <body> <h1> Media Query with NOT Operator </h1> <p> Resize the browser window to less than 700px to see the effect. </p> </body> </html>

创建复杂的媒体查询

可以使用andnotonly运算符组合多个条件来创建复杂的媒体查询。还可以将多个媒体查询组合成逗号分隔的列表。

示例

在此示例中,我们组合了多个媒体查询,尝试更改浏览器宽度以查看多种效果。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body { background-color: lightgray; color: black; } @media only screen and (min-width: 500px) and (max-width: 700px) { body { background-color: pink; color: blue; } } @media not screen and (max-width: 700px) { body { background-color: orange; color: white; } } </style> </head> <body> <h1> Media Query with AND, NOT, and ONLY Operators </h1> <p> Resize the browser window to see the effects. </p> </body> </html>

CSS媒体查询屏幕方向

我们可以为用户设备的特定方向(横向或纵向)定义样式。此默认值为纵向。

@media (orientation: portrait) { /* Styles */ }

示例

以下示例演示了当屏幕宽度大于500px且设备处于横向方向时,文本颜色更改为蓝色。

Open Compiler
<!DOCTYPE html> <html> <head> <style> body { color: red; } @media (min-width: 500px) and (orientation: landscape) { body { color: blue; } } </style> </head> <body> <h3>Resize the browser window to see the effect.</h3> <p> The text color changed to blue when the viewport width is at least 500px and the device is in landscape orientation. </p> </body> </html>

媒体查询功能列表

CSS媒体查询功能用于根据特定特性、输出设备或环境将不同的样式应用于网页。

以下是与媒体功能相关的CSS属性列表。

媒体功能 描述 示例
any-hover 检查用户的任何输入设备是否可以将鼠标悬停在页面上的元素上。
any-pointer 确定用户是否拥有指向设备(例如鼠标)以及该设备是否准确。
aspect-ratio 检查视口或渲染表面的纵横比。
color 指定输出设备所需的颜色分量的位数。
color-gamut 根据用户设备可以显示的颜色范围,将不同的样式应用于网页。
color-index 检查设备可以显示多少种颜色。
device-aspect-ratio 检查输出设备的宽度和高度之间的纵横比。此媒体功能已过时,很少使用。请改用aspect-ratio媒体功能。
device-height 检查输出设备显示区域的高度。此媒体功能已过时,很少使用。请改用height媒体功能。
device-width 检查输出设备显示区域的宽度。此媒体特性已过时,很少使用。建议使用 width 媒体特性。
display-mode 根据 web 应用程序的当前显示模式检测和设置 web 内容样式。(fullscreen | standalone | minimal-ui | browser | window-controls-overlay)
dynamic-range 检查用户代理和输出设备是否支持高亮度、对比度和色深。
forced-colors 检查用户是否在其设备上启用了强制颜色模式。
grid 检查用户设备或屏幕是否支持网格布局。
height 确定视窗的高度。
hover 确定用户的设备是否能够悬停在元素上。
monochrome 确定输出设备的单色帧缓冲区中用于表示每个像素的位数。
orientation 检查设备的屏幕或页面是处于纵向还是横向。
overflow-block 确定用户设备如何处理垂直方向上超出初始容器边界的内容。
overflow-inline 确定用户设备如何处理水平方向上超出初始容器边界的内容。
pointer 检查用户是否拥有可以用来指向和点击的指向设备,例如鼠标或触摸屏。
prefers-color-scheme 确定用户是否更倾向于网站使用浅色模式或深色模式。
prefers-contrast 检查用户是否希望网站具有更高或更低的对比度。
prefers-reduced-motion 检查用户是否在其设备上启用了设置以减少不必要的移动动画。
resolution 检查屏幕上每英寸显示的像素数量。
width 确定视窗的宽度。
update 检查用户设备更改内容外观后,内容在屏幕上的显示效果。
广告