⬅ Back

MEDIA QUERIES

A responsive web page is like clothing that should fit different people:

In web development, that means:

A single layout must adapt to many screen conditions.

Developers solve this with media queries.

1. What media queries are

Media queries are a CSS tool that allows us to apply different styles depending on device or viewport conditions.

They help create adaptive and responsive pages that look good on different screens.

Main idea: a media query asks a question like:

Then the browser applies the matching CSS rules.

Viewport / device condition
            down
      media query checks it
            down
matching CSS rules are applied
Browser
|
|- checks screen width
|- checks device type
|- checks other conditions
+- chooses matching styles

2. What media queries depend on

Media queries can depend on:

But in everyday responsive design, the most common condition is viewport width.

Common media query targets
|
|- width
|- height
|- orientation
|- resolution
+- media type (screen / print)

3. How media queries work

The algorithm is simple:

  1. The developer writes media queries and CSS rules inside them.
  2. The browser watches the viewport and environment.
  3. The browser applies the rules whose conditions are true.
Step 1: developer writes rules
        down
Step 2: browser checks conditions
        down
Step 3: matching rules become active

Example idea:

If width is small -> mobile styles
If width is medium -> tablet styles
If width is large -> desktop styles

4. Media query as a sentence

A media query can be understood like an instruction.

For example: “When the viewport width is 900px or more, make the background orange.”

So media queries are like conditional CSS.

IF condition is true
THEN apply these CSS rules
Condition:
viewport >= 900px

Action:
body background becomes orange

5. Anatomy of a media query

A media query starts with @media.

Then it may contain:

General structure:

@media media-type and (media-feature) {
  /*
   * A set of CSS rules to apply
   * if the media type and feature condition is true
   */
}
@media
  |- media type
  |- and
  +- media feature

Example:

@media screen and (min-width: 900px) {
  body {
    background-color: orange;
  }
}

How to read it:

If this is a screen
and the width is at least 900px,
make the body background orange.

6. Media type and media feature

A media type describes the general output environment.

Examples:

A media feature describes a specific condition.

Examples:

Media query
|
|- media type    -> what kind of output?
+- media feature -> what exact condition?

Example:

@media screen and (min-width: 900px)

Breakdown:

screen         -> media type
(min-width...) -> media feature

7. The min-width feature

min-width means: apply styles when the viewport width is greater than or equal to a value.

Example:

/* The CSS rules below apply when the viewport width is greater than or equal to 800px */
@media (min-width: 800px) {
  body {
    background-color: orange;
  }
}

How to read it:

If width >= 800px,
apply these styles.

Range diagram:

0 ----------- 799 | 800 ------------------------------>
                    styles start here

Another visual:

Viewport widths:

500px  -> no
700px  -> no
800px  -> yes
1000px -> yes

8. Example of min-width

Code:

@media (min-width: 550px) {
  body {
    background-color: blue;
  }
}

Meaning:

Width line

0 -------- 549 | 550 ------------------------------>
                blue starts here

Examples:

400px -> no blue
549px -> no blue
550px -> blue
700px -> blue

9. The max-width feature

max-width means: apply styles when the viewport width is less than or equal to a value.

Example:

/* CSS rules apply when the viewport width is less than or equal to 600px */
@media (max-width: 600px) {
  body {
    background-color: green;
  }
}

How to read it:

If width <= 600px,
apply these styles.

Range diagram:

<---------------- 600 | 601 --------------------------->
styles active here

Examples:

400px -> green
600px -> green
601px -> not green
900px -> not green

10. min-width vs max-width

Comparison:

min-width: 800px
-> 800 and above

max-width: 600px
-> 600 and below

Visual comparison:

min-width
0 -------- 799 | 800 ------------------------------>
                 active area

max-width
<---------------- 600 | 601 ----------------------->
 active area

Short memory line:

min-width = from this point upward
max-width = up to this point

11. Multiple media queries

A stylesheet can contain many media queries.

The browser checks each one and applies the ones that match.

Example:

body {
  background-color: white;
}

/* Applies when the viewport width is less than or equal to 600px */
@media (max-width: 600px) {
  body {
    background-color: green;
  }
}

/* Applies when the viewport width is greater than or equal to 800px */
@media (min-width: 800px) {
  body {
    background-color: orange;
  }
}

12. How the previous example behaves

This code gives three visual zones.

0 ----------- 600 | 601 ------- 799 | 800 -------------------->
green              white             orange

So:

Table:

Viewport width     Background
--------------------------------
500px              green
600px              green
700px              white
799px              white
800px              orange
1200px             orange

13. Gaps between media queries

Sometimes no media query matches.

Then the browser uses the base style.

In the previous example:

Base style = white

[ green zone ] [ white gap ] [ orange zone ]

This is very normal.

14. Another multiple-query example

Code:

@media (max-width: 600px) {
  body {
    color: blue;
  }
}

@media (min-width: 700px) {
  body {
    color: green;
  }
}
0 ----------- 600 | 601 --- 699 | 700 ------------------------>
blue               default       green

Meaning:

15. Overriding styles in media queries

Media queries do not automatically “win” because they are media queries.

Their rules still follow the normal CSS cascade.

Important:

Example:

/* Base style */
body {
  background-color: #fff;
}

/* At 900px and wider, override the background color */
@media (min-width: 900px) {
  body {
    background-color: #388e3c;
  }
}
Base:
body background = white

If width >= 900px:
body background = green

So at large widths, the media query rule overrides the base background color.

16. Only identical properties are overridden

This is very important.

If a media query changes only background-color, it does not erase all other body styles.

Only the matching property changes.

Example:

/* Base styles */
body {
  color: #2a2a2a;
  background-color: #fff;
  font-size: 16px;
  line-height: 1.5;
  font-family: sans-serif;
}

/* From 600px and up, change background color, font size, and text color */
@media (min-width: 600px) {
  body {
    background-color: #2196f3;
    font-size: 20px;
    color: #fff;
  }
}

/* From 900px and up, change background color */
@media (min-width: 900px) {
  body {
    background-color: #4caf50;
  }
}

17. How layered styles work

Think of CSS like layers of a cake.

Base layer:

At 600px and up:

At 900px and up:

Layer 1: base styles
   down
Layer 2: 600px+ overrides some properties
   down
Layer 3: 900px+ overrides one property again

18. Result at widths above 900px

At width > 900px, the final body styles are:

Final style at >900px

background-color -> from 900px query
color            -> from 600px query
font-size        -> from 600px query
line-height      -> from base
font-family      -> from base

This shows: only identical properties get replaced.

19. Media types

A media type describes what kind of output device the styles are for.

Modern common media types:

Media types
|
|- screen -> displays with screens
|- print  -> printed output / print preview
+- all    -> any device

20. The print media type

print is for printing or print preview.

Example:

@media print {
  body {
    color: green;
  }
}

Meaning:

Screen view  -> normal styles
Print view   -> print styles

Use cases:

21. The screen media type

screen is for devices with physical screens.

That includes:

Example:

@media screen and (min-width: 400px) {
  /* ... */
}

Meaning:

screen
|- phone
|- tablet
|- monitor
+- TV

22. Omitting the media type

Often developers omit screen.

Example:

@media (min-width: 400px) {
  /* ... */
}

Why is this allowed?

Because if no media type is written, the default is:

all

Meaning: apply to any device type that satisfies the condition.

Common rule:

Comparison:

@media screen and (min-width: 400px)
@media (min-width: 400px)

In many normal responsive screen-only projects, the second form is common.

23. Simple and complex conditions

A media query can be:

Simple example:

@media (min-width: 768px) {
  /* styles */
}

Complex example:

@media screen and (min-width: 768px) {
  /* styles */
}

Most often, developers use:

Simple:
@media (min-width: ...)

Complex:
@media screen and (min-width: ...)

24. Practical responsive example

Imagine this CSS:

body {
  background-color: white;
}

@media (min-width: 600px) {
  body {
    background-color: blue;
  }
}

@media (min-width: 900px) {
  body {
    background-color: green;
  }
}
0 ----------- 599 | 600 --------- 899 | 900 ------------------->
white              blue                green

This is a very common pattern:

25. Mobile-first thinking with min-width

A very common approach is:

Base styles      -> mobile
min-width: 600   -> tablet+
min-width: 900   -> desktop+

This builds styles in layers.

Start small
   down
add more styles at bigger widths
   down
page grows in complexity with screen size

26. Print example situation

Situation: a developer created a page with an application form for a social assistance program.

Later it turned out many users prefer filling it out offline.

Designers then created a separate print version.

That is exactly a case for:

@media print

Possible idea:

Screen version:
navigation + colors + buttons + UI elements

Print version:
clean layout + readable spacing + print-friendly text

27. Developer tools and testing media queries

To test media queries:

Change viewport width
        down
browser re-checks media queries
        down
new styles may activate

Example:

590px -> mobile styles
650px -> tablet-like styles
920px -> desktop-like styles

28. Big summary diagram

Media queries
|
|- @media
|    |- media type
|    +- media feature
|
|- Common media features
|    |- min-width
|    +- max-width
|
|- Common media types
|    |- screen
|    |- print
|    +- all
|
|- Browser behavior
|    |- checks conditions
|    |- activates matching rules
|    +- uses CSS cascade
|
+- Responsive use
     |- mobile styles
     |- tablet styles
     +- desktop styles

29. Mini cheat sheet

@media               -> starts a media query
screen               -> devices with screens
print                -> print / print preview
all                  -> any device
min-width            -> width is at least this value
max-width            -> width is at most this value
breakpoint           -> width where styles change
base styles          -> default styles outside media queries
override             -> only matching properties are replaced

30. Common mistakes

Mistake 1: Thinking media queries replace all previous styles.

Problem: they only override matching properties.

Mistake 2: Forgetting the base style.

Problem: some width ranges may have no special query, so base style shows there.

Mistake 3: Misreading min-width and max-width.

Remember:

min-width -> from this width upward
max-width -> up to this width

Mistake 4: Testing only one screen size.

Problem: responsive bugs stay hidden.

Mistake 5: Writing print styles inside screen rules.

Problem: printed output will not get the intended design.

31. Final summary

Media queries are a CSS tool for applying styles depending on conditions such as viewport width or media type.

Important things to remember:

Super short memory line:

Media query = "if this condition is true, use these CSS rules."
⬅ Back