CSS3 @media query
Example
If the document width is less than 300 pixels, change the background color (background-color):
@media screen and (max-width: 300px)
{
body
{
background-color:
lightblue;
}
}
Try it »
Definition and usage
Using @media queries, you can define different styles for different media types.
@media can set different styles for different screen sizes. In particular, if you need to design a responsive page, @media is very useful.
When you resize the browser, the page is re-rendered according to the browser's width and height.
Browser support
The numbers in the table indicate the version number of the first browser that supported the @media rule.
| Rule | |||||
|---|---|---|---|---|---|
| @media | 21 | 9 | 3.5 | 4.0 | 9 |
CSS Syntax
@media not|only mediatype and (mediafeature and|or|not mediafeature) {
CSS-Code;
}
not, and, and only can be used together to construct complex media queries. You can also combine multiple media queries into one rule by separating them with commas.
Meanings of the not, only, and and keywords:
-
not: The not operator is used to negate a media query, returning true if the condition is not met and false otherwise. If it appears in a comma-separated list of queries, it negates only the specific query to which it is applied. If the not operator is used, a media type must also be specified.
-
only: The only operator is used to apply styles only when the entire query matches, and is useful for preventing older browsers from applying the selected styles. When only is not used, older browsers interpret screen and (max-width: 500px) simply as screen, ignoring the rest of the query and applying the styles to all screens. If the only operator is used, a media type must also be specified.
-
, (comma) The comma is used to combine multiple media queries into one rule. Each query in a comma-separated list is processed independently of the others. Therefore, if any query in the list is true, the entire media statement returns true. In other words, the list behaves like a logical or operator.
-
and: The and operator is used to combine multiple media query rules into a single media query; the media query is true when every rule is true. It is also used to combine media features with media types.
Media types describe the general category of a device. Unless the not or only logical operators are used, the media type is optional and the all type is applied implicitly.
You can also use different style files for different media:
....
Media type
| Value | Description |
|---|---|
| all | For all devices |
| aural | Deprecated. For speech and sound synthesizers |
| braille | Deprecated. For braille tactile feedback devices |
| embossed | Deprecated. For braille printers |
| handheld | Deprecated. For handheld devices or smaller devices, such as PDAs and small phones |
| For printers and print previews | |
| projection | Deprecated. For projection devices |
| screen | For computer screens, tablets, smartphones, etc. |
| speech | For speech devices such as screen readers |
| tty | Deprecated. For fixed-character grids such as teletypes, terminals, and portable devices with limited character capabilities |
| tv | Deprecated. For television and web TV |
Media features
| Value | Description |
|---|---|
| aspect-ratio | Defines the ratio of the width to the height of the visible area of the page in the output device |
| color | Defines the number of color components per color group in the output device. If the device is not a color device, the value is 0 |
| color-index | Defines the number of entries in the color lookup table of the output device. If no color lookup table is used, the value is 0 |
| device-aspect-ratio | Defines the ratio of the visible width to the height of the output device's screen. |
| device-height | Defines the visible height of the output device's screen. |
| device-width | Defines the visible width of the output device's screen. |
| grid | Used to query whether the output device uses a raster or dot matrix. |
| height | Defines the height of the visible area of the page in the output device. |
| max-aspect-ratio | Defines the maximum ratio of the visible width to the height of the output device's screen. |
| max-color | Defines the maximum number of color components per color group in the output device. |
| max-color-index | Defines the maximum number of entries in the color lookup table of the output device. |
| max-device-aspect-ratio | Defines the maximum ratio of the visible width to the height of the output device's screen. |
| max-device-height | Defines the maximum visible height of the output device's screen. |
| max-device-width | Defines the maximum visible width of the output device's screen. |
| max-height | Defines the maximum visible area height of the page in the output device. |
| max-monochrome | Defines the maximum number of monochrome components per pixel in a monochrome frame buffer. |
| max-resolution | Defines the maximum resolution of the device. |
| max-width | Defines the maximum visible area width of the page in the output device. |
| min-aspect-ratio | Defines the minimum ratio of the width to the height of the visible area of the page in the output device. |
| min-color | Defines the minimum number of color components per color group in the output device. |
| min-color-index | Defines the minimum number of entries in the color lookup table of the output device. |
| min-device-aspect-ratio | Defines the minimum ratio of the visible width to the height of the output device's screen. |
| min-device-width | Defines the minimum visible width of the output device's screen. |
| min-device-height | Defines the minimum visible height of the output device's screen. |
| min-height | Defines the minimum visible area height of the page in the output device. |
| min-monochrome | Defines the minimum number of monochrome components per pixel in a monochrome frame buffer |
| min-resolution | Defines the minimum resolution of the device. |
| min-width | Defines the minimum visible area width of the page in the output device. |
| monochrome | Defines the number of monochrome components per pixel in a monochrome frame buffer. If the device is not monochrome, the value is 0 |
| orientation | Defines whether the height of the visible area of the page in the output device is greater than or equal to the width. |
| resolution | Defines the resolution of the device. For example: 96dpi, 300dpi, 118dpcm |
| scan | Defines the scanning process of television-like devices. |
| width | Defines the width of the visible area of the page in the output device. |
More examples
Example
Use @media queries to create a responsive design:
@media only screen and (max-width: 500px)
{
.gridmenu
{
width:
100%;
}
.gridmain
{
width:
100%;
}
.gridright
{
width:
100%;
}
}
Try it »
More examples
CSS media queries for adapting to various device sizes
Related pages
CSS Tutorial: CSS media types