English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The @media rule can be used to define style rules for multiple media types within a single stylesheet. It must be followed by a comma-separated list of media types and a block containing the rules.
You can define different styles for different media types using the @media rule.
@media can set different styles for different screen sizes, especially if you need to set responsive page designs, @media is very useful.
When you resize the browser, the page will also be re-rendered according to the width and height of the browser.
The syntax of this rule is as follows:
@media media type,... { /* media-specific rules */ }
You can also use different CSS styles for different media:
<link rel="stylesheet" media="mediatype and|not|only (media feature)" href="style.css">
The following example demonstrates how to use the @media attribute.
@media screen{ body { color: #32cd32; font-family: Arial, sans-serif; font-size: 14px; } } @media print { body { color: #ff6347; font-family: Times, serif; font-size: 12pt; } } @media screen, print { body { line-height: 1.2; } }Test to see‹/›
Note:Style rules outside the scope of @media apply to all media types applicable to the stylesheet. Internal rules @media in CSS2.1Invalid in medium
Value | Description |
---|---|
all | Used for all devices |
aural | Deprecated. Used for voice and sound synthesizers |
braille | Deprecated. Applied to Braille tactile feedback devices |
embossed | Deprecated. Used for printing for the blind |
handheld | Deprecated. Used for handheld devices or smaller devices, such as PDAs and small phones |
Used for printers and print previews | |
projection | Deprecated. Used for projection devices |
screen | Used for computer screens, tablets, smartphones, and so on. |
speech | Applied to voice output devices such as screen readers |
tty | Deprecated. Used for fixed character grids, such as telegrams, terminal devices, and portable devices with limited character sets |
tv | Deprecated. Used for television and Internet television |
Value | Description |
---|---|
aspect-ratio | The ratio of the width to the height of the visible area of the page in the output device |
color | The number of each group of color components defined for the output device. If it is not a color device, the value is equal to 0 |
color-index | The number of entries defined in the color lookup table of the output device. If no color lookup table is used, the value is equal to 0 |
device-aspect-ratio | Define the ratio of the visible width to height of the screen of the output device. |
device-height | Define the visible height of the screen of the output device. |
device-width | Define the visible width of the screen of the output device. |
grid | Used to query whether the output device uses a raster or dot matrix. |
height | Define the height of the visible area of the page in the output device. |
max-aspect-ratio | Define the maximum ratio of the visible width to height of the screen of the output device. |
max-color | Define the maximum number of color components in each group of color components of the output device. |
max-color-index | Define the maximum number of entries in the color lookup table of the output device. |
max-device-aspect-ratio | Define the maximum ratio of the visible width to height of the screen of the output device. |
max-device-height | Define the maximum visible height of the screen of the output device. |
max-device-width | Define the maximum visible width of the screen of the output device. |
max-height | Define the maximum height of the visible area of the page in the output device. |
max-monochrome | Define the maximum number of monochrome components per pixel in a monochrome frame buffer. |
max-resolution | Define the maximum resolution of the device. |
max-width | Define the maximum width of the visible area of the page in the output device. |
min-aspect-ratio | Define the minimum ratio of the width to height of the visible area of the page in the output device. |
min-color | Define the minimum number of color components in each group of color components of the output device. |
min-color-index | Define the minimum number of entries in the color lookup table of the output device. |
min-device-aspect-ratio | Define the minimum ratio of the visible width to height of the screen of the output device. |
min-device-width | Define the minimum visible width of the screen of the output device. |
min-device-height | Define the minimum visible height of the screen of the output device. |
min-height | Define the minimum height of the visible area of the page in the output device. |
min-monochrome | Define the minimum number of monochrome components per pixel in a monochrome frame buffer. |
min-resolution | Define the minimum resolution of the device. |
min-width | Define the minimum width of the visible area of the page in the output device. |
monochrome | Define the number of monochrome components per pixel in a monochrome frame buffer. If it is not a monochrome device, the value is 0 |
orientation | Define whether the height of the visible area of the page in the output device is greater than or equal to the width. |
resolution | Define the resolution of the device. For example:96dpi, 300dpi, 118dpcm |
scan | Define the scanning process of television-type devices. |
width | Define the width of the visible area of the page in the output device. |
Browser compatibility of @media attribute, the numbers in the table below represent the minimum version number of browsers that support this attribute; all mainstream browsers support this rule.
|
Please refer to the following tutorials:CSS Media Types.