ProgramGuru

CSS Media Queries and Breakpoints

CSS Media Queries and Breakpoints

Media queries apply CSS rules only when a media feature, such as viewport width, matches.

What you’ll learn

  • Write correct media query syntax.
  • Explain viewport conditions and breakpoints.
  • Choose breakpoints based on content fit.

Understand the concept

Use @media (min-width: 700px) { ... } to enable rules at 700 CSS pixels and above, or @media (max-width: 699px) for smaller widths. Modern range syntax such as @media (width >= 700px) is also available.

Pick breakpoints where a layout actually becomes cramped. A query can also test preferences such as prefers-reduced-motion; media queries are not limited to device widths.

What the browser evaluates

At matching viewport widths, declarations inside @media enter the cascade just like ordinary CSS rules.

Show a baseline alert

Start with a block that spans its parent.

HTML

<div class="notice-bar"><strong>New arrivals</strong><span>Explore the catalog</span></div>

CSS

.notice-bar { display: block; background: #eff6ff; border-radius: 10px; padding: 14px; } .notice-bar span { display: block; margin-top: 5px; }

Rendered output

New arrivalsExplore the catalog

Observe: The callout content is stacked.

Switch arrangement with @media

Use a width condition to align text in a row on larger viewports.

HTML

<div class="notice-bar"><strong>New arrivals</strong><span>Explore the catalog</span></div>

CSS

.notice-bar { display: block; background: #d1fae5; border-radius: 10px; padding: 14px; } .notice-bar span { display: block; margin-top: 5px; } @media (min-width: 650px) { .notice-bar { display: flex; justify-content: space-between; gap: 16px; } .notice-bar span { margin-top: 0; } }

Rendered output

New arrivalsExplore the catalog

Observe: Resize your browser through 650px to see the layout switch.

Explain the result

At matching viewport widths, declarations inside @media enter the cascade just like ordinary CSS rules.

Compare the two examples: identify the selector, each changed property, and which visible part of the output proves that the CSS took effect.

Common mistakes to avoid

  • Queries for viewport width do not respond to an individual component resizing.

  • Do not choose breakpoints solely because a device is described as tablet or desktop.

Try it yourself

Add a 900px breakpoint changing the background color; inspect the two independent conditions.

Before changing the code, predict what will change. Then verify by checking the rendered result.

Check your understanding

What does @media (min-width: 650px) test?
Choose one answer. You can try again.

Answer and explanation

The browser viewport is at least 650px wide

A width media query tests the layout viewport.

What you should remember

Use @media when a full-page viewing condition affects the layout.

Revisit the rendered examples if the rule still feels abstract. Change one declaration at a time so you can connect cause and effect.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Fluid Widths and Responsive Typography →