Responsive Web Design - Media Queries
Media queries were introduced in CSS3:CSS3 @media query。
Using @media queries, you can define different styles for different media types.
Example
If the browser window is smaller than 500px, the background will become light blue:
@media only screen and (max-width: 500px)
{
body
{
background-color:
lightblue;
}
}
Try it »
Add a breakpoint
In earlier tutorials we used rows and columns to make web pages; it was responsive but did not display well on small screens.
Media queries can help us solve this problem. We can add breakpoints in the middle of the design, with different effects at different breakpoints.
Desktop devices
Mobile devices
Use a media query to add a breakpoint at 768px:
Example
When the screen (browser window) is smaller than 768px, the width of each column is 100%:
/* For desktop: */
.col-1
{
width:
8.33%;
}
.col-2
{
width:
16.66%;
}
.col-3
{
width:
25%;
}
.col-4
{
width:
33.33%;
}
.col-5
{
width:
41.66%;
}
.col-6
{
width:
50%;
}
.col-7
{
width:
58.33%;
}
.col-8
{
width:
66.66%;
}
.col-9
{
width:
75%;
}
.col-10
{
width:
83.33%;
}
.col-11
{
width:
91.66%;
}
.col-12
{
width:
100%;
}
@media only screen and (max-width: 768px)
{
/* For mobile phones: */
[class*="col-"] {
width: 100%;
}
}
Try it »
Designing for mobile first
Mobile first means prioritizing the mobile design when designing for desktop and other devices.
This means we must make some changes to the CSS.
We make style modifications when the screen is smaller than 768px, and also when the screen width is greater than 768px. The following is a mobile-first example:
/* Designed for mobile: */
[class*="col-"]
{
width:
100%;
}
@media only screen and (min-width: 768px)
{
/* For desktop: */
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
}
Other breakpoints
You can add breakpoints according to your own needs.
We can also set breakpoints for tablet and mobile phone devices.
Desktop devices
Tablet devices
Mobile devices
Add a media query at a screen size of 600px and set new styles (for screens larger than 600px but smaller than 768px):
Example
Note that the two sets of class styles are the same but have different names (col- and col-m-):
/* For mobile phones: */
[class*="col-"]
{
width:
100%;
}
@media only screen and (min-width: 600px)
{
/* For tablets: /
.col-m-1 {width: 8.33%;}
.col-m-2 {width: 16.66%;}
.col-m-3 {width: 25%;}
.col-m-4 {width: 33.33%;}
.col-m-5 {width: 41.66%;}
.col-m-6 {width: 50%;}
.col-m-7 {width: 58.33%;}
.col-m-8 {width: 66.66%;}
.col-m-9 {width: 75%;}
.col-m-10 {width: 83.33%;}
.col-m-11 {width: 91.66%;}
.col-m-12 {width: 100%;}
}
@media only screen and (min-width: 768px) {
/ For desktop: */
.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}
}
Try it »
The code above may look redundant, but it can automatically set different styles according to the screen size, so it is still very necessary.
HTML Example
For desktop devices:
The first and third parts span 3 columns. The middle part spans 6 columns.
For tablet devices:
The first spans 3 columns, the second spans 9 columns, and the third spans 12 columns:
<
div
class=
"row"
<
div
class="col-3 col-m-3"
...
<
/div
<
div
class="col-6 col-m-9"
...
<
/div
<
div
class="col-3 col-m-12"
...
<
/div
<
/divCombined with CSS media queries, you can create layouts that adapt to different device orientations (landscape, portrait, etc.).
Syntax:
orientation:portrait | landscape
- ** portrait:** specifies that the height of the visible page area on the output device is greater than or equal to the width
- ** landscape:** all cases other than the portrait value are landscape
Example
If it is landscape, the background will be light blue:
@media only screen and (orientation: landscape)
{
body
{
background-color:
lightblue;
}
}
Try it »