- CSS Media Queries
- CSS3 Introduced Media Queries
- Browser Support
- Media Query Syntax
- CSS3 Media Types
- Media Queries Simple Examples
- Example
- Example
- More Media Query Examples
- CSS @media Reference
- Responsive Web Design — Media Queries
- Example
- Add a Breakpoint
- Example
- Always Design for Mobile First
- Example
- Another Breakpoint
- Example
- HTML Example
- Typical Device Breakpoints
- Example
- Orientation: Portrait / Landscape
- Example
- Hide Elements With Media Queries
- Example
- Change Font Size With Media Queries
- Variable Font Size.
- Example
- CSS @media Reference
CSS Media Queries
The @media rule, introduced in CSS2, made it possible to define different style rules for different media types.
Examples: You could have one set of style rules for computer screens, one for printers, one for handheld devices, one for television-type devices, and so on.
Unfortunately these media types never got a lot of support by devices, other than the print media type.
CSS3 Introduced Media Queries
Media queries in CSS3 extended the CSS2 media types idea: Instead of looking for a type of device, they look at the capability of the device.
Media queries can be used to check many things, such as:
- width and height of the viewport
- width and height of the device
- orientation (is the tablet/phone in landscape or portrait mode?)
- resolution
Using media queries are a popular technique for delivering a tailored style sheet to desktops, laptops, tablets, and mobile phones (such as iPhone and Android phones).
Browser Support
The numbers in the table specifies the first browser version that fully supports the @media rule.
Media Query Syntax
A media query consists of a media type and can contain one or more expressions, which resolve to either true or false.
The result of the query is true if the specified media type matches the type of device the document is being displayed on and all expressions in the media query are true. When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules.
Unless you use the not or only operators, the media type is optional and the all type will be implied.
You can also have different stylesheets for different media:
CSS3 Media Types
Value | Description |
---|---|
all | Used for all media type devices |
Used for printers | |
screen | Used for computer screens, tablets, smart-phones etc. |
speech | Used for screenreaders that «reads» the page out loud |
Media Queries Simple Examples
One way to use media queries is to have an alternate CSS section right inside your style sheet.
The following example changes the background-color to lightgreen if the viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the background-color will be pink):
Example
The following example shows a menu that will float to the left of the page if the viewport is 480 pixels wide or wider (if the viewport is less than 480 pixels, the menu will be on top of the content):
Example
More Media Query Examples
For much more examples on media queries, go to the next page: CSS MQ Examples.
CSS @media Reference
For a full overview of all the media types and features/expressions, please look at the @media rule in our CSS reference.
Responsive Web Design — Media Queries
It uses the @media rule to include a block of CSS properties only if a certain condition is true.
Example
If the browser window is 600px or smaller, the background color will be lightblue:
Add a Breakpoint
Earlier in this tutorial we made a web page with rows and columns, and it was responsive, but it did not look good on a small screen.
Media queries can help with that. We can add a breakpoint where certain parts of the design will behave differently on each side of the breakpoint.
Desktop
Phone
Use a media query to add a breakpoint at 768px:
Example
When the screen (browser window) gets smaller than 768px, each column should have a width of 100%:
@media only screen and (max-width: 768px) /* For mobile phones: */
[class*=»col-«] width: 100%;
>
>
Always Design for Mobile First
Mobile First means designing for mobile before designing for desktop or any other device (This will make the page display faster on smaller devices).
This means that we must make some changes in our CSS.
Instead of changing styles when the width gets smaller than 768px, we should change the design when the width gets larger than 768px. This will make our design Mobile First:
Example
/* For mobile phones: */
[class*=»col-«] width: 100%;
>
Another Breakpoint
You can add as many breakpoints as you like.
We will also insert a breakpoint between tablets and mobile phones.
Desktop
Tablet
Phone
We do this by adding one more media query (at 600px), and a set of new classes for devices larger than 600px (but smaller than 768px):
Example
Note that the two sets of classes are almost identical, the only difference is the name ( col- and col-s- ):
/* For mobile phones: */
[class*=»col-«] width: 100%;
>
It might seem odd that we have two sets of identical classes, but it gives us the opportunity in HTML, to decide what will happen with the columns at each breakpoint:
HTML Example
For desktop:
The first and the third section will both span 3 columns each. The middle section will span 6 columns.
For tablets:
The first section will span 3 columns, the second will span 9, and the third section will be displayed below the first two sections, and it will span 12 columns:
Typical Device Breakpoints
There are tons of screens and devices with different heights and widths, so it is hard to create an exact breakpoint for each device. To keep things simple you could target five groups:
Example
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px)
/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px)
/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px)
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px)
Orientation: Portrait / Landscape
Media queries can also be used to change layout of a page depending on the orientation of the browser.
You can have a set of CSS properties that will only apply when the browser window is wider than its height, a so called «Landscape» orientation:
Example
The web page will have a lightblue background if the orientation is in landscape mode:
Hide Elements With Media Queries
Another common use of media queries, is to hide elements on different screen sizes:
Example
/* If the screen size is 600px wide or less, hide the element */
@media only screen and (max-width: 600px) div.example display: none;
>
>
Change Font Size With Media Queries
You can also use media queries to change the font size of an element on different screen sizes:
Variable Font Size.
Example
/* If the screen size is 601px or more, set the font-size of
@media only screen and (min-width: 601px) div.example font-size: 80px;
>
>
/* If the screen size is 600px or less, set the font-size of to 30px */
@media only screen and (max-width: 600px) div.example font-size: 30px;
>
>
CSS @media Reference
For a full overview of all the media types and features/expressions, please look at the @media rule in our CSS reference.