HTML and CSS · Lesson 10 of 12
Responsive design
Most of your users are on phones. Responsive design means one page that works at every width: start with the phone, then add rules as the screen grows.
Mobile first, with media queries
Write the CSS for small screens first, then add @media (min-width: 640px) { … } blocks for bigger ones. A media query applies its rules only when its condition is true, so the phone gets the simple layout and wider screens get more columns.
Units that scale
- rem: relative to the root font size (16px by default). Use it for font sizes and spacing so everything scales when a user enlarges text.
- %: relative to the parent. vw/vh: 1% of the screen's width or height.
max-width: 100%on images stops them overflowing small screens.clamp(1.5rem, 4vw, 2.5rem): a size that grows with the screen but stays between a minimum and a maximum.
Your turn
Make the navigation links stack vertically on phones and sit in a row from 600px up.
Show a solution
<style>
nav a { display: block; padding: 8px; background: #f1f3f5; margin: 4px 0; }
@media (min-width: 600px) {
nav { display: flex; gap: 8px; }
nav a { margin: 0; }
}
</style>
<nav><a href="#">Home</a><a href="#">Courses</a><a href="#">Practice</a></nav>In the interview
What is mobile-first CSS?
Writing the base styles for small screens and adding min-width media queries for larger ones. Phones get only the CSS they need, and the layout gains complexity as space allows.
rem vs em vs px?
px is fixed. em is relative to the element's own font size, so nested ems compound. rem is relative to the root font size, so it's predictable and respects the user's text size setting: the usual choice for fonts and spacing.
How do you make images responsive?
max-width: 100% and height: auto so they never overflow their container, width and height attributes to reserve space, and srcset/sizes (or the picture element) so phones download smaller files.