@media rule

Prev Tutorial Next Tutorial

@media rule

@media rule is used to define different style rules for different media types or devices. For example font size is different when print page and different on web page. In CSS2 this was called media types, while in CSS3 it is called media queries.

Media queries look at the capability of the device, and can be used to check many things, they are;

  • width and height of the browser window
  • width and height of the device
  • orientation (is the tablet/phone in landscape or portrait mode?)
  • resolution


@media not|only mediatype and (media feature) {

Media type

allUsed for all media type devices have same style
screenUsed for computer screens, tablets, smart-phones etc.
printUsed for printers

Change the background-color if the window width is smaller than 400 pixels


<!DOCTYPE html>
body {
    background-color: cyan;

@media screen and (max-width: 400px) {
    body {
        background-color: #eee;
<p>Resize your window, when width of page is more 
than 400px, then its background is cyan and if width is 
less than 400px then its background is gray.</p>

Same like above example you can design a web page for mobile device, mobile screen is smaller than computer screen, when your web page is open on mobile at that time it behave different.

Media Features

colorSpecifies the number of bits per color component for the output device
max-heightSpecifies the maximum height of the display area, such as a browser window
max-widthSpecifies the maximum width of the display area, such as a browser window

Browser supported


Prev Tutorial Next Tutorial

Download Projects

Google Adsense Advertisements
Free Classified Site – Bedpage

Yahoo Advertisements