Hermit Notebook

Memo on CSS3 media queries

Memo on CSS3 media queries

Image by Clovis Cheminot from Pixabay

Reminder: you can left-scroll and right-scroll the long code blocks.

Intro

Media queries allow the HTML UI to adapt to the user’s device. This post focuses on a short
reminder of common media queries used on responsive designs (2022).
See a more detailed presentation here.

Media query syntax

1
2
3
4
5
6
7
8
9
10
11
@media [not|only] mediatype and (condition: breakpoint) {

/* CSS rules */

}

@media mediatype and (orientation: portrait|landscape) {

/* CSS rules */

}

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@media print {
body {
color: black;
}
}

@media screen and (max-width: 576px) {

.responsive {
width: 100%;
}

}

@media only screen and (orientation: landscape) {
.responsive {
width: 20%;
}
}

@media screen and (max-width: 900px) and (min-width: 600px), (min-width: 1100px) {
p.responsive {
font-size: 2em;
padding: 2em;
border: 0.5em solid black;
background: white;
}
}

Reference breakpoints

We can take inspiration from the famous Boostrap library.

Name Short name Min width Max width (excluded)
Extra small xs 0 576px
Small sm 576px 768px
Medium md 768px 992px
Large lg 992px 1200px
Extra large xl 1200px 1400px
Extra extra large xxl 1400px

Reference media queries

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* ****************** xs : extra small ****************** */
@media screen and (max-width: 575px) {

/* CSS rules */

}

/* ****************** sm : small ****************** */
@media screen and (min-width: 576px) and (max-width: 767px) {

/* CSS rules */

}

/* ****************** md : medium ****************** */
@media screen and (min-width: 768px) and (max-width: 991px) {

/* CSS rules */

}

/* ****************** lg : large ****************** */
@media screen and (min-width: 992px) and (max-width: 1199px) {

/* CSS rules */

}

/* ****************** xl : extra large ****************** */
@media screen and (min-width: 1200px) and (max-width: 1399px) {

/* CSS rules */

}

/* ****************** xxl : extra extra large ****************** */
@media screen and (min-width: 1400px) {

/* CSS rules */

}

That’s all for this memo. Leave a comment ✍️ and a “Like” ❤️ if you found this post useful 😉

Thanks for reading !

See you soon !

Keep learning !

Contents

  1. 1. Intro
  2. 2. Media query syntax
  3. 3. Reference breakpoints
  4. 4. Reference media queries