A responsive home page
Make a page with a header (logo + menu), three cards and a footer. On a phone: the menu below the logo, the cards one below the other. From 700px: the menu to the right of the logo, the cards in a row.
More than half of all visitors open websites on a phone. A responsive site adapts itself to the width of the screen: on a phone everything is in one column, on a computer in several. This lesson covers the viewport, flexible sizes, media queries and the "phone first" approach.
Without this line a phone shows the page as if the screen were 980px wide and shrinks everything – the text becomes tiny:
<meta name="viewport" content="width=device-width, initial-scale=1">
It must be in the head of every page.
| Instead of | Use | Why |
|---|---|---|
width: 900px | max-width: 900px; width: 100% | it shrinks on a narrower screen |
| images with a fixed width | img { max-width: 100%; height: auto; } | an image never goes off the screen |
| font sizes in px everywhere | rem, and for headings clamp(1.5rem, 4vw, 2.5rem) | the text adapts |
| a fixed number of columns | flex-wrap or repeat(auto-fill, minmax(...)) | the columns rearrange themselves |
A media query is a set of CSS rules that apply only under a certain condition, most often for a certain screen width:
<style>
.columns { display: flex; flex-direction: column; gap: 8px; } /* phone: one column */
.columns div { background: #eef6fc; padding: 14px; border-radius: 8px; }
.message::after { content: "a narrow screen (phone)"; }
@media (min-width: 600px) { /* applies when the width is 600px or more */
.columns { flex-direction: row; } /* tablet and computer: columns in a row */
.columns div { flex: 1; }
.message::after { content: "a wide screen (≥ 600px)"; }
}
</style>
<p class="message">Right now it is: </p>
<div class="columns">
<div>First column</div>
<div>Second column</div>
<div>Third column</div>
</div>
Result:
The result frame is narrower than 600px on a phone and wider on a computer – open this page on both devices or make the window narrower. In F12 there is a Toggle device toolbar button (Ctrl+Shift+M) that shows the page as it looks on a chosen phone.
| Common breakpoints | Device |
|---|---|
| up to 599px | phone |
| 600px – 1023px | tablet, small laptop |
| 1024px and more | computer |
The breakpoints are not a law – they are placed where the layout "breaks" as you make the window narrower.
The recommended way of writing: the basic rules are for the phone (simple, one column), and @media (min-width: ...) adds a more complex layout for larger screens. That way the CSS is shorter and the page is faster on a phone.
/* the basics: phone */
.page { display: grid; gap: 12px; }
nav ul { display: flex; flex-direction: column; }
/* tablet and larger */
@media (min-width: 700px) {
nav ul { flex-direction: row; gap: 16px; }
}
/* computer */
@media (min-width: 1024px) {
.page { grid-template-columns: 220px 1fr; } /* menu on the left, content on the right */
}
main { max-width: 70ch; }.@media (prefers-color-scheme: dark) { ... } respects the device's setting.div with overflow-x: auto so that they scroll horizontally instead of breaking the page.Ready-made libraries such as Bootstrap have ready classes for a responsive grid and components. They are used in the Logo creator project, but it is important that you first understand how all of this works with plain CSS.
Each task has an example, a hint and a solution. The solutions have been checked in the browser.
Make a page with a header (logo + menu), three cards and a footer. On a phone: the menu below the logo, the cards one below the other. From 700px: the menu to the right of the logo, the cards in a row.
meta viewport, media queries do not work as you expect on a phone.max-width: 100% go off the screen and make the whole page scroll horizontally.1. What does @media (min-width: 600px) { ... } do?
2. Which line must be in the head for responsive design to work on a phone?
3. What does "mobile first" mean?
4. How do you stop an image from being wider than the screen?