相关阅读请跳转至:
7 Media types
Contents
7.1 Introduction to media types
One of the most important features of style sheets is that they specify how a document is to be presented on different media: on the screen, on paper, with a speech synthesizer, with a braille device, etc.
Certain CSS properties are only designed for certain media (e.g., the property only applies to paged media). On occasion, however, style sheets for different media types may share a property, but require different values for that property. For example, the property is useful both for screen and print media. The two media types are different enough to require different values for the common property; a document will typically need a larger font on a computer screen than on paper. Therefore, it is necessary to express that a style sheet, or a section of a style sheet, applies to certain media types.
7.2 Specifying media-dependent style sheets
There are currently two ways to specify media dependencies for style sheets:
- Specify the target medium from a style sheet with the @media or @importat-rules.
Example(s):
@import url("fancyfonts.css") screen;@media print { /* style sheet for print goes here */}
- Specify the target medium within the document language. For example, in HTML 4 (), the "media" attribute on the LINK element specifies the target media of an external style sheet:
Link to a target medium The body...
The rule is defined in the .
7.2.1 The @media rule
An @media rule specifies the target (separated by commas) of a set of (delimited by curly braces). Invalid statements must be ignored per and The @media construct allows style sheet rules for various media in the same style sheet:
@media print { body { font-size: 10pt } } @media screen { body { font-size: 13px } } @media screen, print { body { line-height: 1.2 } }
Style rules outside of @media rules apply to all media types that the style sheet applies to. At-rules inside @media are invalid in CSS2.1.
7.3 Recognized media types
The names chosen for CSS media types reflect target devices for which the relevant properties make sense. In the following list of CSS media types the names of media types are normative, but the descriptions are informative. Likewise, the "Media" field in the description of each property is informative.
- all
- Suitable for all devices. braille
- Intended for braille tactile feedback devices. embossed
- Intended for paged braille printers. handheld
- Intended for handheld devices (typically small screen, limited bandwidth). print
- Intended for paged material and for documents viewed on screen in print preview mode. Please consult the section on for information about formatting issues that are specific to paged media. projection
- Intended for projected presentations, for example projectors. Please consult the section on 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 for details. tty
- Intended for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities). Authors should not use with the "tty" media type. tv
- Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).
Media type names are case-insensitive.
Media types are mutually exclusive in the sense that a user agent can only support one media type when rendering a document. However, user agents may use different media types on different canvases. For example, a document may (simultaneously) be shown in 'screen' mode on one canvas and 'print' mode on another canvas.
Note that a multimodal media type is still only one media type. The 'tv' media type, for example, is a multimodal media type that renders both visually and aurally to a single canvas.
@media and @import rules with unknown media types (that are nonetheless valid identifiers) are treated as if the unknown media types are not present. If an @media/@import rule contains a malformed media type (not an identifier) then the statement is invalid.
Note: Media Queries supercedes this error handling.
Example(s):
For example, in the following snippet, the rule on the P element applies in 'screen' mode (even though the '3D' media type is not known).
@media screen, 3D { P { color: green; }}
Note.Future updates of CSS may extend the list of media types. Authors should not rely on media type names that are not yet defined by a CSS specification.
7.3.1 Media groups
This section is informative, not normative.
Each CSS property definition specifies which media types the property applies to. Since properties generally apply to several media types, the "Applies to media" section of each property definition lists media groupsrather than individual media types. Each property applies to all media types in the media groups listed in its definition.
CSS 2.1 defines the following media groups:
- continuousor paged.
- visual,audio,speech, or tactile.
- grid (for character grid devices), orbitmap.
- interactive (for devices that allow user interaction), orstatic(for those that do not).
- all(includes all media types)
The following table shows the relationships between media groups and media types:
Media Types | Media Groups | |||
---|---|---|---|---|
continuous/paged | visual/audio/speech/tactile | grid/bitmap | interactive/static | |
braille | continuous | tactile | grid | both |
embossed | paged | tactile | grid | static |
handheld | both | visual, audio, speech | both | both |
paged | visual | bitmap | static | |
projection | paged | visual | bitmap | interactive |
screen | continuous | visual, audio | bitmap | both |
speech | continuous | speech | N/A | both |
tty | continuous | visual | grid | both |
tv | both | visual, audio | bitmap | both |
语法:
@media :<sMedia> {
sRules }取值:
- <sMedia>:
- 指定设备名称。 { sRules}:
- 样式表定义。
说明:
判断媒介(对象)类型来实现不同的展现。此特性让CSS可以更精确作用于不同的媒介类型,同一媒介的不同条件(分辨率、色数等等).
media_query: [only | not]?[ and ]*expression: ( [: ]? )media_type: all | aural | braille | handheld | print | projection | screen | tty | tv | embossedmedia_feature: width | min-width | max-width | height | min-height | max-height | device-width | min-device-width | max-device-width | device-height | min-device-height | max-device-height | device-aspect-ratio | min-device-aspect-ratio | max-device-aspect-ratio | color | min-color | max-color | color-index | min-color-index | max-color-index | monochrome | min-monochrome | max-monochrome | resolution | min-resolution | max-resolution | scan | grid