Sub topic 1
The Viewport and CSS Pixels
Prerequisites
This chapter assumes basic HTML and CSS, including selectors and the box model. You should also know px, rem, percentages, and the basics of a frame-based design tool such as Figma.
The viewport and CSS pixels
The viewport is the part of a page currently visible in the browser. CSS pixels are the units CSS uses to describe that space. A CSS pixel is a logical unit, not necessarily one physical screen pixel. High-density screens can use several physical pixels to display one CSS pixel.
On small screens, add this tag inside the page head:
<meta name="viewport" content="width=device-width, initial-scale=1">It tells the browser to use the device's CSS width instead of laying out the page on a wider virtual canvas. Without it, mobile browsers may shrink a desktop-width layout until its text becomes difficult to read.
What a breakpoint is and why layouts need them
A breakpoint is a width at which the layout changes because the current arrangement no longer works. For example, a navigation row may fit at 900 CSS pixels but wrap awkwardly at 500. The breakpoint belongs where the content breaks, not automatically at the width of a named device.
Think of responsive design as a set of rules applied to a continuous range of widths:
Fluid layouts: let the content flex before you add breakpoints
Use fluid values first. Percentages, fr, minmax(), and clamp() let a layout adapt between breakpoints.
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}
h1 {
font-size: clamp(1.75rem, 1.25rem + 2vw, 3rem);
}This grid creates as many columns as fit while keeping each card at least 16rem wide. Add a breakpoint only when the fluid behavior no longer preserves readable text, usable controls, or clear hierarchy.
Writing media queries
A media query applies CSS when a condition matches. A mobile-first stylesheet defines the narrow layout first, then adds changes with min-width.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 48em) {
.layout {
grid-template-columns: 1fr 1fr;
}
}Here, the base rule supports narrow screens. At 48em and above, the layout gains two columns. Test widths between the base state and the breakpoint because wrapping can fail before the breakpoint itself.
Exercise
Why should a breakpoint be chosen from content behavior rather than a device name?
Show answer
Content behavior shows the width where the current layout becomes unusable, regardless of the device displaying it.