Coding/Learn/css/media queries
CSS Beginner Course
01.Introduction to CSS
02.Selectors (Element, Class & ID)
03.3 Ways to Add CSS to HTML
04.Colors & Backgrounds (HEX, RGB, HSL)
05.Typography & Fonts (font-size, font-weight, line-height)
06.The Cascade & Specificity Wars
07.The Sacred CSS Box Model
08.Padding vs Margin & Shorthand Notation
09.Borders & Border Radius (Rounded Corners & Circles)
10.The Magic of box-sizing: border-box
11.Sizing & Units (px, rem, em, %, vh, vw)
12.The Display Property (block, inline, inline-block, none)
13.Flexbox Part 1: Container Powers (justify, align, gap)
14.Flexbox Part 2: Direction, Wrapping & Item Powers
15.Positioning (relative, absolute, fixed, sticky)
16.Z-Index & The 3D Stacking Context
17.Pseudo-Classes (:hover, :active, :focus, :first-child)
18.Smooth Transitions & Box Shadows
19.Responsive Design & Media Queries (@media)
20.Capstone: Crafting a Modern UI Profile Card
Course Certificate0%

Complete all lessons to earn

LearnResponsive Design & Media Queries (@media)
19 / 20 0%
Lesson 19 of 20
5 mins read

Responsive Design & Media Queries (@media)

PreviousNext Lesson
In Plain English

Over 60% of all web traffic comes from mobile devices. Media queries (@media) allow you to apply specific CSS rules conditionally based on the screen width of the device (e.g. flipping a 3-column desktop grid into a single vertical column on mobile).

The Mental Model

"A media query is like a responsive wardrobe. In winter (screen < 768px), you wear a single compact heavy jacket (1 column). In summer on the beach (screen > 1200px), you spread out a wide 4-person picnic blanket (4 columns)."

Deep Dive: How It Works

@media (max-width: 768px): Applies styles only when screen width is 768px or smaller (tablets & phones).

Mobile-First Approach: Write default styles for phones first, then use @media (min-width: 768px) to add multi-column grids for desktops.

Common Breakpoints: Mobile (< 640px), Tablet (641px - 1024px), Desktop (> 1024px).

Responsive Flexbox: Flipping flex-direction: row on desktop to flex-direction: column on mobile is the universal responsive layout trick.

Syntax Blueprint

/* Desktop Default: 3 Columns */
.grid {
  display: flex;
  gap: 16px;
}

/* Mobile: 1 Column */
@media (max-width: 768px) {
  .grid {
    flex-direction: column;
  }
}

Flip horizontal flex rows into vertical columns on small screens.

PointCore Rules to Remember

Point star
The Viewport Meta Tag: Always ensure <meta name="viewport" content="width=device-width, initial-scale=1.0"> is in your HTML.
Point star
Relative Typography on Mobile: Scale down hero headlines from 3rem on desktop to 1.75rem on phones.
Point star
Test in DevTools: Press Cmd+Option+I (or F12) and click the Device Toolbar icon to test every phone screen.

Common Beginner Traps & How to Fix Them

Mistake:Writing hardcoded fixed widths inside media queries

Why it happens: Using width: 400px inside a 360px wide phone view causes horizontal scrolling.

How to fix: Use width: 100%; or max-width: 100%; on mobile screens.

Pro Tip: Never hide content on mobile just because it doesn't fit—re-architect it with clean vertical scrolling!

Live Interactive Example

Hit Run Code to see it live
Responsive Grid to Column Flip
CSS3
1
2
3
4
5
6
7
8
9
10
11
Live Browser Preview
Click "Preview / Apply" to view the rendered output.
How it works: The media query listens for screens narrower than 640px and rotates the flex axis.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Write a mobile media query

  • PointWrite a media query @media (max-width: 768px) containing a rule for .sidebar setting display: none;
1
2
Browser Preview & Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates