liveBook · Manning

Media query

As explained in Build a Website with ChatGPT

A media query is a CSS conditional expression that applies styles according to characteristics such as viewport width or device capabilities.

A media query is a CSS condition that checks a screen feature, such as viewport width. When the condition is true, the browser applies its style rules; otherwise, it ignores them. section 12.2.9

Media queries help create adaptive layouts. A narrow viewport can switch a multi-column page to one column, making text and page regions more comfortable to use on a smartphone. section 12.2.9

The basic pattern uses a condition followed by CSS rules:

  • Use max-width when styles should apply at or below a breakpoint. section 12.2.9
  • Use min-width when styles should apply at or above a breakpoint. section 12.2.9
  • Put the styles inside the media query’s code block so the browser can apply them conditionally. section 12.2.9

Switch the main area to one column

Use @media (max-width: 500px) { main { display: grid; grid-template-columns: 1fr; } } to make the main element a one-column grid on screens no wider than 500 pixels. A larger-screen rule can instead set h1 { font-size: 72px; } at min-width: 1024px.

To check that a responsive rule works, load the page in a smartphone browser and confirm that the header, logo, text sizes, and main area adapt as intended. section 12.2.10

How Build a Website with ChatGPT covers it

How deep it goes
Defines conditional syntax and explains max-width and min-width with examples, stopping short of broader screen-feature queries.
Examples
Uses 500px one-column layouts, 1024px heading sizing, and smartphone verification of an article page.
What it assumes
Starts with viewport width and CSS query syntax, while demonstrating how to request responsive CSS without assuming coding expertise.
What you can do after
Apply CSS media queries to change layouts and typography at specified viewport widths, then verify the result on a smartphone.
How it connects
Connects media queries to adaptive layout, viewport width, CSS Grid, and responsive web-page 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 Build a Website with ChatGPT. The section links point to the parts of the book it draws on.