@media

概要

@media CSS at-rule 與一組被大括弧隔開、並由 media query 條件定義的 CSS 巢狀區塊有關。@media at-rule 不只能用在最頂層的 CSS 區塊,也能在任何 CSS conditional-group at-rule 使用。

@media at-rule 能透過 CSS object model 介面 CSSMediaRule (en-US) 通行。

語法

@media = 
@media <media-query-list> { <stylesheet> }

A <media-query> is composed of a optional media type and/or a number of media features.

Media types

all

Suitable for all devices.

print

Intended for paged material and for documents viewed on screen in print preview mode. Please consult the section on paged media, and the media section of the Getting Started tutorial for information about formatting issues that are specific to paged media.

screen

Intended primarily for color computer screens.

speech

Intended for speech synthesizers. Note: CSS2 had a similar media type called 'aural' for this purpose. See the appendix on aural style sheets for details.

備註: CSS2.1 and Media Queries 3 defined several additional media types (tty, tv, projection, handheld, braille, embossed, aural), but they were deprecated in Media Queries 4 and shouldn't be used.

Media Features

Each media feature tests for one specific feature of the browser or device.

Name Summary Notes
width (en-US) Viewport width
height Viewport height
aspect-ratio (en-US) Width-to-height aspect ratio of the viewport
orientation (en-US) Orientation of the viewport
resolution (en-US) Pixel density of the output device
scan Scanning process of the output device
grid (en-US) Is the device a grid or bitmap?
update-frequency (en-US) How quickly (if at all) can the output device modify the appearance of the content Added in Media Queries Level 4
overflow-block (en-US) How does the output device handle content that overflows the viewport along the block axis? Added in Media Queries Level 4
overflow-inline (en-US) Can content that overflows the viewport along the inline axis be scrolled? Added in Media Queries Level 4
color (en-US) Number of bits per color component of the output device, or zero if the device isn't color.
color-index (en-US) Number of entries in the output device's color lookup table, or zero if the device does not use such a table.
display-mode (en-US) The display mode of the application, as specified in the web app manifest's display member. Defined in the Web App Manifest spec.
monochrome (en-US) Bits per pixel in the output device's monochrome frame buffer, or 0 if the device is not monochrome.
inverted-colors (en-US) Is the user agent or underlying OS inverting colors? Added in Media Queries Level 4
pointer (en-US) Is the primary input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4
hover (en-US) Does the primary input mechanism allow the user to hover over elements? Added in Media Queries Level 4
any-pointer (en-US) Is any available input mechanism a pointing device, and if so, how accurate is it? Added in Media Queries Level 4
any-hover (en-US) Does any available input mechanism allow the user to hover over elements? Added in Media Queries Level 4
light-level Current ambient light level Added in Media Queries Level 4
scripting (en-US) Is scripting (e.g. JavaScript) available? Added in Media Queries Level 4
device-width (en-US) 已棄用 Width of the rendering surface of the output device Deprecated in Media Queries Level 4
device-height (en-US) 已棄用 Height of the rendering surface of the output device Deprecated in Media Queries Level 4
device-aspect-ratio (en-US) 已棄用 Width-to-height aspect ratio of the output device Deprecated in Media Queries Level 4
-webkit-device-pixel-ratio (en-US) 非標準 Number of physical device pixels per CSS pixel Nonstandard; WebKit/Blink-specific. If possible, use the resolution (en-US) media feature instead.
-webkit-transform-3d (en-US) 非標準 Are CSS 3D transforms supported? Nonstandard; WebKit/Blink-specific
-webkit-transform-2d (en-US) 非標準 Are CSS 2D transforms supported? Nonstandard; WebKit-specific
-webkit-transition (en-US) 非標準 Are CSS transitions supported? Nonstandard; WebKit-specific
-webkit-animation (en-US) 非標準 Are CSS animation (en-US)s supported? Nonstandard; WebKit-specific

Examples

@media print {
  body { font-size: 10pt }
}
@media screen {
  body { font-size: 13px }
}
@media screen, print {
  body { line-height: 1.2 }
}
@media only screen
  and (min-device-width: 320px)
  and (max-device-width: 480px)
  and (-webkit-min-device-pixel-ratio: 2) {
    body { line-height: 1.4 }
}

Specifications

Specification
Media Queries Level 4
# media-descriptor-table
CSS Conditional Rules Module Level 3
# at-media

Browser compatibility

BCD tables only load in the browser

See also