KodingKaro
CSS · Intermediate · Media Queries and Breakpoints

Media query syntax and viewport conditions

Media queries apply styles only when a condition is true. They are the standard way to adjust a layout for viewport characteristics.

✓ What you will learn in this topic

  • Explain the key idea: Media queries conditionally apply styles; organize them around content needs.
  • Apply the concept: The demo changes to a row at wider viewports; the card grid gains columns when the chosen condition matches.
  • Avoid this common mistake:

Concept explanation

A media query begins with @media and a condition such as (min-width: 48rem). Styles inside apply when the condition matches. You can combine conditions and use features like orientation, hover capability, and color scheme.

Use a consistent mobile-first progression where the base rules handle the narrow layout and minimum-width queries add roomier arrangements. This is a strategy, not a requirement; choose the organization that keeps your cascade clear.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo {
  display: block;
}
@media (min-width: 48rem) {
  .demo {
    display: flex;
    gap: 1rem;
  }
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo {
  display: block;
}
@media (min-width: 48rem) {
  .demo {
    display: flex;
    gap: 1rem;
  }
}

Expected result: The demo changes to a row at wider viewports; the card grid gains columns when the chosen condition matches.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card-grid {
  display: grid;
  grid-template-columns: 1fr;
}
@media (min-width: 52rem) {
  .card-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Adding many overlapping media queries without a clear progression makes the cascade difficult to predict.

Best practice

Organize conditions consistently, use content-led thresholds, and include reduced-motion or contrast considerations where appropriate.

Practice exercise

Add one breakpoint for a layout change and a reduced-motion query that removes nonessential animation. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Media queries conditionally apply styles; organize them around content needs.
  • Organize conditions consistently, use content-led thresholds, and include reduced-motion or contrast considerations where appropriate.

Knowledge check

When do styles inside a min-width media query apply?

Reveal the explanation

min-width sets a lower bound for the condition.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo {
  display: block;
}
@media (min-width: 48rem) {
  .demo {
    display: flex;
    gap: 1rem;
  }
}

Expected result:

The demo changes to a row at wider viewports; the card grid gains columns when the chosen condition matches.

Hands-on practice task

Add one breakpoint for a layout change and a reduced-motion query that removes nonessential animation.

In this section

Other lessons in Media Queries and Breakpoints

Broaden your knowledge

Related learning topics