Skip to content

Intermediate · CSS layout and responsive design · topic 27 of 64 · 30 min

Responsive design and media queries

Explanation

A responsive page adapts to any screen, from a small phone to a big monitor. Start with the viewport meta tag and a mobile layout, use flexible widths, and add media queries with min-width to improve the layout as the screen grows.

Why it matters

Most people in India open websites on a phone. A page made only for laptops forces them to pinch and zoom. Responsive design means one page that rearranges itself to fit every screen.

Think of it like this: Think of water poured into different glasses. It takes the shape of a tall glass, a small cup or a wide bowl. A responsive page flows into the shape of each screen in the same way.

Step 1: the viewport meta tag

html
<meta name="viewport" content="width=device-width, initial-scale=1">
Without this line, phones pretend to be a 980px-wide desktop and shrink everything

Step 2: flexible sizes

  • Use %, fr, rem and max-width instead of fixed pixel widths.
  • Make images shrink: img { max-width: 100%; height: auto; }
  • Let grids decide columns with auto-fit and minmax.

Step 3: media queries

A media query applies CSS only when a condition is true, usually the width of the viewport. Mobile-first means: write the phone styles as the base, then add min-width queries that enhance the layout on bigger screens.

css
/* Base: phones. One column. */
.layout { display: grid; gap: 16px; }

/* Tablets and up */
@media (min-width: 600px) {
  .layout { grid-template-columns: 1fr 1fr; }
}

/* Laptops and up */
@media (min-width: 1024px) {
  .layout { grid-template-columns: 250px 1fr 1fr; }
}
Mobile-first: start simple, add columns as space grows

A breakpoint is a width where the layout changes. Do not pick widths of famous phones; widen your browser slowly and add a breakpoint where the design starts to look stretched or cramped.

Touch and preferences

  • Tap targets (buttons, links) should be at least about 44px by 44px with space between them.
  • Hover does not exist on touch screens: never hide important actions behind hover only.
  • @media (prefers-color-scheme: dark) lets you offer a dark theme that follows the phone setting.
  • @media (prefers-reduced-motion: reduce) lets you calm animations for people who get dizzy.
Tip: Test in DevTools device mode, then on a real low-cost Android phone if you can. Real phones show problems with slow loading and small text that a laptop hides.
Careful: Never add maximum-scale=1 or user-scalable=no to the viewport tag. Blocking zoom hurts people with low vision.

Watch the lesson

Responsive design and media queries · 1/6

Phone - tablet - laptop

Responsive design means one page that fits every screen.

Prefer a video by a teacher? Find free videos on “responsive web design media queries mobile first tutorial” on YouTube ↗

In real life: An exam results portal

On results day, lakhs of students open the board website on their phones. The mobile-first page shows one column: the roll number form, then the marks table that scrolls sideways inside its own box. On a laptop, a min-width: 1024px media query places the notices in a sidebar next to the form.

Practice (0/3 done)

Task 1 · Code · HTML

Make this page responsive: add the viewport meta tag (width=device-width, initial-scale=1) and make images shrink with img { max-width: 100%; height: auto; }.

Task 2 · Code · HTML

Mobile-first layout: .cards is a grid with one column by default (display: grid; gap: 16px). Add a media query @media (min-width: 600px) that sets .cards to grid-template-columns: 1fr 1fr.

Task 3 · Practice

Open a page you built in DevTools device mode. Slowly drag the width from 320px to 1400px and note where the layout looks wrong. Add or move a breakpoint there and fix it.

Quick quiz

Question 1

1.What happens on a phone without the viewport meta tag?

Question 2

2.In mobile-first CSS, which media queries do you mostly write?

Question 3

3.How should you choose breakpoints?

Question 4

4.Why not hide a menu that only opens on hover?

Question 5

5.Which CSS makes images shrink to fit small screens?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: Positioning and stacking