Sub topic 1
What Mobile-First Means and Why It Works
Prerequisites
Assume you know basic HTML structure, CSS selectors, the box model, the cascade, and basic Flexbox or Grid. You should also be able to use browser developer tools.
What mobile-first means and why it works
Mobile-first CSS starts with styles for narrow screens. You then add improvements for wider screens with min-width media queries. The small-screen rules are the default, so they remain useful when a query does not apply.
Desktop-first CSS starts with a wide layout and uses max-width queries to remove or override features. This often creates more overrides and makes the stylesheet harder to reason about. Mobile-first also makes limited space and slow connections part of the initial design.
Setting the viewport
Mobile browsers may otherwise use a wide virtual viewport and shrink the page to fit. Put this element in the document’s head:
<meta name="viewport" content="width=device-width, initial-scale=1">It tells the browser to use the device’s CSS width. Without it, media queries can appear to fail on phones.
> callout note > The viewport tag does not make a layout responsive by itself. It makes the browser report the screen width that your responsive CSS expects.
Thinking in relative units
Relative units adapt to context or screen size. Use rem for spacing and text tied to the root font size, % for proportions, and vw sparingly for viewport-based scaling. An em unit depends on the current element’s font size, so nested values can compound.
Avoid choosing breakpoints from device names. Choose one when the content becomes crowded, such as when navigation wraps or a card becomes too narrow.
Writing the small-screen base styles
Write the simplest useful layout first. A single column usually gives readable content on a narrow screen. Add columns only when the available width supports them.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}The base rule works everywhere. The media query adds, rather than undoes, the wider layout.
Exercise
Why is min-width usually the natural query for mobile-first CSS?
Show answer
Narrow-screen rules are the default, and min-width adds layout enhancements only when the viewport has enough space.