liveBook · Manning

Media query

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A media query is a CSS condition that applies styles according to properties of the user agent or viewport, such as screen width or resolution.

A media query makes CSS responsive by applying styles when a viewport condition is true. In a MyBlog banner, the default rule hides the image, while a minimum width of 768 pixels restores it for larger screens. This preserves small-screen space for content without requiring separate page layouts. section 7.2.1

The technique relies on CSS’s cascade: the later rule overrides the earlier one when its condition matches. Keeping the base rule and conditional override in base.css makes the behavior reusable across templates that inherit from base.html, including Bootstrap-styled pages. section 7.2.1

Hide a banner on narrow screens

Define `.banner { display: none; }`, then add `@media (min-width: 768px) { .banner { display: block; background: var(--background-url) no-repeat center center / cover; } }`. Resizing the browser activates the query and reveals or removes the banner.

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains conditional CSS, cascading overrides, and a 768-pixel breakpoint through one banner example.
Examples
Demonstrates a responsive banner that hides below 768 pixels and appears above that breakpoint.
What you can do after
Apply it by combining a default CSS rule with a breakpoint-specific override in a shared stylesheet.
How it connects
Connects CSS cascade, Bootstrap, CSS variables, templates, and responsive web 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 The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.