English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

CSS reference manual

CSS @rules

CSS attribute大全

CSS @media Rule

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.

CSS @media rule usage syntax

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

Media type

ValueDescription
allUsed for all devices
auralDeprecated. Used for voice and sound synthesizers
brailleDeprecated. Applied to Braille tactile feedback devices
embossedDeprecated. Used for printing for the blind
handheldDeprecated. Used for handheld devices or smaller devices, such as PDAs and small phones
printUsed for printers and print previews
projectionDeprecated. Used for projection devices
screenUsed for computer screens, tablets, smartphones, and so on.
speechApplied to voice output devices such as screen readers
ttyDeprecated. Used for fixed character grids, such as telegrams, terminal devices, and portable devices with limited character sets
tvDeprecated. Used for television and Internet television

Media function

ValueDescription
aspect-ratioThe ratio of the width to the height of the visible area of the page in the output device
colorThe 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-indexThe 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-ratioDefine the ratio of the visible width to height of the screen of the output device.
device-heightDefine the visible height of the screen of the output device.
device-widthDefine the visible width of the screen of the output device.
gridUsed to query whether the output device uses a raster or dot matrix.
heightDefine the height of the visible area of the page in the output device.
max-aspect-ratioDefine the maximum ratio of the visible width to height of the screen of the output device.
max-colorDefine the maximum number of color components in each group of color components of the output device.
max-color-indexDefine the maximum number of entries in the color lookup table of the output device.
max-device-aspect-ratioDefine the maximum ratio of the visible width to height of the screen of the output device.
max-device-heightDefine the maximum visible height of the screen of the output device.
max-device-widthDefine the maximum visible width of the screen of the output device.
max-heightDefine the maximum height of the visible area of the page in the output device.
max-monochromeDefine the maximum number of monochrome components per pixel in a monochrome frame buffer.
max-resolutionDefine the maximum resolution of the device.
max-widthDefine the maximum width of the visible area of the page in the output device.
min-aspect-ratioDefine the minimum ratio of the width to height of the visible area of the page in the output device.
min-colorDefine the minimum number of color components in each group of color components of the output device.
min-color-indexDefine the minimum number of entries in the color lookup table of the output device.
min-device-aspect-ratioDefine the minimum ratio of the visible width to height of the screen of the output device.
min-device-widthDefine the minimum visible width of the screen of the output device.
min-device-heightDefine the minimum visible height of the screen of the output device.
min-heightDefine the minimum height of the visible area of the page in the output device.
min-monochromeDefine the minimum number of monochrome components per pixel in a monochrome frame buffer.
min-resolutionDefine the minimum resolution of the device.
min-widthDefine the minimum width of the visible area of the page in the output device.
monochromeDefine the number of monochrome components per pixel in a monochrome frame buffer. If it is not a monochrome device, the value is 0
orientationDefine whether the height of the visible area of the page in the output device is greater than or equal to the width.
resolutionDefine the resolution of the device. For example:96dpi, 300dpi, 118dpcm
scanDefine the scanning process of television-type devices.
widthDefine the width of the visible area of the page in the output device.

Browser Compatibility

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.

  • Firefox 1+

  • Google Chrome1+

  • Internet Explorer 9+

  • Apple Safari 1.3+

  • Opera 9.2+

Further Reading

Please refer to the following tutorials:CSS Media Types.