In this lesson, you made the garden’s header mobile-first and found where its breakpoint belongs by widening the window until the header fit.
Key Points
- The need. At 320 pixels, the header’s flex row of the garden’s name and seven links makes the home page 579 pixels wide in the recording, so it scrolls sideways.
- Stack the header first. Cut
display: flex,justify-contentandalign-itemsfrom theheaderrule in thelayoutlayer. The header’s two children are blocks, so the name sits above the nav. Addflex-wrap: wrapto.site-nav ul, so the links that don’t fit carry on in a second row. At 320 pixels, the page is 320 pixels wide again. - A tablet width doesn’t fit this header. With the flex lines in
@media (width >= 768px), the header is one row at 1280 pixels and stacked at 767. At 768, the row is back, but the garden’s name breaks over two lines and About drops to a second row on its own. - Drag until it fits. Widen the page in device mode, and in the recording nothing changes at 790 or 809 pixels. At 810, the name and all seven links barely fit on one row. That width comes from what’s in the row, the name and seven links in the recording’s font, not from any device. Your font may move it a little, so note where your row fits. Dragging is how you find where any part of a page breaks.
- Set the query a little above, in rem. 810 divided by 16 is about 51, so round up a little to
52rem, which is 832 pixels at the default text size. If your row fit at another width, divide yours by 16 and round up the same way. In the recording, the header stays stacked up to 831 pixels, with all seven links in one row under the name, and at 832 it becomes a row. Drag your own page to check your value. In rem, the breakpoint also moves with the visitor’s text size. With 20-pixel text, the bigger links need a wider row, and52remgrows to 1040 pixels.
The Rule
- Put each breakpoint where your content breaks, not where a device starts.
- Widen the window until the content fits. Then set the query in rem, a little above that width.
Final Code
The header’s rules in the layout layer of garden-practice/style.css:
header {
position: sticky;
top: 0;
background-color: var(--page);
}
.site-nav ul {
display: flex;
flex-wrap: wrap;
gap: var(--space-m);
list-style: none;
padding: 0;
}
@media (width >= 52rem) {
header {
display: flex;
justify-content: space-between;
align-items: center;
}
}
Next is Exercise: Make the Home Page Mobile-First, where you’ll turn a desktop-first copy of the garden’s home page mobile-first, on your own. It’s the course’s first exercise, with its own editor and starting CSS on the page, so you won’t need VS Code. Its tests check your CSS for you. Run them early and often, read each failing test for clues, and use Full screen for more room.