liveBook · Manning

Media query

As explained in Web Design Playground, Second Edition

An @media rule is a CSS at-rule that conditionally groups stylesheet rules according to a media type or media feature.

A media query lets CSS ask a question about the screen, then apply a group of style rules only when the answer is true. It is the little traffic cop that helps a layout behave on different devices. section 14.4

The question usually checks viewport width with max-width or min-width. A max-width query handles screens no wider than a value; a min-width query handles screens at least that wide. The @media rule can omit the media type because screen is the web default. section 14.4

Shrink the title on small screens

This rule changes the h1 size when the viewport is 350 pixels wide or less: @media (max-width: 350px) { h1 { font-size: 24px; } } For a larger layout, a min-width query can display the sidebar as an inline block at 1024 pixels and above. section 14.4

.container {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-areas:
        "header header header"
        "article article sidebar"
        "footer footer footer";
}
h1 {
   font-size: 32px;
}
@media (max-width: 450px) {             #1
    .container {
        grid-template-columns: 1fr;     #2
        grid-template-areas:            #2
            "header"                    #2
            "article"                   #2
            "sidebar"                   #2
            "footer";                   #2
    }
    h1 {
        font-size: 24px;                #3
    }
}

See a complete adaptive grid: wider screens use three columns, while screens under 450 pixels switch to one stacked column and reduce the title size. section 14.4

Use media queries to:

  • Change element sizes when the viewport becomes too narrow. section 14.4
  • Change a grid from columns to a stacked layout. section 14.4
  • Hide or display elements at suitable screen sizes. section 14.12
  • Support a mobile-first layout by adding features for larger screens. section 14.12

How Web Design Playground, Second Edition covers it

How deep it goes
Explains conditional syntax, width features, and grid changes, but stops short of broader media-feature coverage.
Examples
Demonstrates title resizing, sidebar display, and a three-column grid becoming a stacked mobile layout.
What it assumes
Assumes basic CSS selectors and declarations while teaching @media syntax and width conditions directly.
What you can do after
Apply it via CSS to resize elements, rearrange layouts, and control visibility at chosen viewport widths.
How it connects
Connects media queries with adaptive layout, responsive web design, viewport width, CSS Grid, liquid layout, and mobile-first design.

Also explained in

Compare how 7 books explain media query →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.