X
HOME HTML5 CSS3 JAVASCRIPT SQL PHP5 JQUERY ANGULAR BOOTSTRAP3 XML MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

CSS Reference

CSS Reference CSS Selectors CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Color Values CSS Color Names CSS Color HEX CSS3 Browser Support

CSS Properties

align-content align-items align-self animation animation-delay animation-direction animation-duration animation-fill-mode animation-iteration-count animation-name animation-play-state animation-timing-function backface-visibility background background-attachment background-clip background-color background-image background-origin background-position background-repeat background-size border border-bottom border-bottom-color border-bottom-left-radius border-bottom-right-radius border-bottom-style border-bottom-width border-collapse border-color border-image border-image-outset border-image-repeat border-image-slice border-image-source border-image-width border-left border-left-color border-left-style border-left-width border-radius border-right border-right-color border-right-style border-right-width border-spacing border-style border-top border-top-color border-top-left-radius border-top-right-radius border-top-style border-top-width border-width bottom box-shadow box-sizing caption-side clear clip color column-count column-fill column-gap column-rule column-rule-color column-rule-style column-rule-width column-span column-width columns content counter-increment counter-reset cursor direction display empty-cells flex flex-basis flex-direction flex-flow flex-grow flex-shrink flex-wrap float font @font-face font-family font-size font-size-adjust font-stretch font-style font-variant font-weight hanging-punctuation height icon justify-content @keyframes left letter-spacing line-height list-style list-style-image list-style-position list-style-type margin margin-bottom margin-left margin-right margin-top max-height max-width @media min-height min-width nav-down nav-index nav-left nav-right nav-up opacity order outline outline-color outline-offset outline-style outline-width overflow overflow-x overflow-y padding padding-bottom padding-left padding-right padding-top page-break-after page-break-before page-break-inside perspective perspective-origin position quotes resize right tab-size table-layout text-align text-align-last text-decoration text-decoration-color text-decoration-line text-decoration-style text-indent text-justify text-overflow text-shadow text-transform top transform transform-origin transform-style transition transition-delay transition-duration transition-property transition-timing-function unicode-bidi vertical-align visibility white-space width word-break word-spacing word-wrap z-index

CSS3 @media Query


Example

Change the background-color if the document is smaller than 300 pixels wide:

@media screen and (max-width: 300px) {
    body {
        background-color: lightblue;
    }
}

Try it yourself »

More "Try it Yourself" examples below.


Definition and Usage

With a @media query, you can write different CSS code for different media types.

This helps when you want different layout for different media types such as a screen or a printer, but also when you want different layout for different devices, which is very useful when making web pages with responsive design.

You can also have different layout when a user resizes the browser window up or down to a certain width, or height.


Browser Support

The numbers in the table specifies the first browser version that fully supports the @media rule.

Rule
@media 21 9 3.5 4.0 9


CSS Syntax

@media mediatype and|not|only (media feature) {
    CSS-Code;
}

You can also have different stylesheets for different media:

<link rel="stylesheet" media="mediatype and|not|only (media feature)" href="mystylesheet.css">

Media Types

Value Description
all Used for all media type devices
aural Deprecated. Used for speech and sound synthesizers
braille Deprecated. Used for braille tactile feedback devices
embossed Deprecated. Used for paged braille printers
handheld Deprecated. Used for small or handheld devices
print Used for printers
projection Deprecated. Used for projected presentations, like slides
screen Used for computer screens, tablets, smart-phones etc.
speech Used for screenreaders that "reads" the page out loud
tty Deprecated. Used for media using a fixed-pitch character grid, like teletypes and terminals
tv Deprecated. Used for television-type devices

Media Features

Value Description
aspect-ratio Specifies the ratio between the width and the height of the display area
color Specifies the number of bits per color for the device
color-index Specifies the number of colors the device can display
device-aspect-ratio Specifies the ratio between the width and the height of the device
device-height Specifies the height of the device, such as a computer screen
device-width Specifies the width of the device, such as a computer screen
grid Specifies whether the device is a grid device or not
height Specifies the height of the display area, such as a browser window
max-aspect-ratio Specifies the minimum ratio between the width and the height of the display area
max-color Specifies the maximum number of bits per color for the device
max-color-index Specifies the maximum number of colors the device can display
max-device-aspect-ratio Specifies the minimum ratio between the width and the height of the device
max-device-height Specifies the maximum height of the device, such as a computer screen
max-device-width Specifies the maximum width of the device, such as a computer screen
max-height Specifies the maximum height of the display area, such as a browser window
max-monochrome Specifies the maximum number of bits per "color" on a greyscale the device
max-resolution Specifies the maximum resolution of the device, using dpi or dpcm
max-width Specifies the maximum width of the display area, such as a browser window
min-aspect-ratio Specifies the minimum ratio between the width and the height of the display area
min-color Specifies the minimum number of bits per color for the device
min-color-index Specifies the minimum number of colors the device can display
min-device-aspect-ratio Specifies the minimum ratio between the width and the height of the device
min-device-width Specifies the minimum width of the device, such as a computer screen
min-device-height Specifies the minimum height of the device, such as a computer screen
min-height Specifies the minimum height of the display area, such as a browser window
min-monochrome Specifies the minimum number of bits per "color" on a greyscale the device
min-resolution Specifies the minimum resolution of the device, using dpi or dpcm
min-width Specifies the minimum width of the display area, such as a browser window
monochrome Specifies the number of bits per "color" on a greyscale the device
orientation Specifies the whether the display is in landscape mode or portrait mode
resolution Specifies the resolution of the device, using dpi or dpcm
scan Specifies progressive or interlaca scanning of a television
width Specifies the width of the display area, such as a browser window


Examples

More Examples

Example

Using @media query to make responsive design:

@media only screen and (max-width: 500px) {
    .gridmenu {
        width:100%;
    }

    .gridmain {
        width:100%;
    }

    .gridright {
        width:100%;
    }
}

Try it yourself »

Related Pages

CSS tutorial: CSS Media Types



Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook