Loading video player…

Find Breakpoints Where Content Breaks

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-content and align-items from the header rule in the layout layer. The header’s two children are blocks, so the name sits above the nav. Add flex-wrap: wrap to .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, and 52rem grows 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:

Language: 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.

00:00 The home page now stacks on a phone, and from 45rem, its sidebar comes in beside the welcome. Now the header, and this time you’ll find where its breakpoint belongs.

00:10 In device mode at 320 pixels, the garden’s name and the seven links still sit in one flex row. Here, the page is 579 pixels wide, so it scrolls sideways.

00:23 Start with the small screen. In the layout layer, cut the three flex lines from the header rule. The header’s two children are blocks, so the name now sits above the nav.

00:34 The nav’s list is still a flex row of seven links. Add flex-wrap: wrap to its rule, so the links that don’t fit carry on in a second row. Save. The page is 320 pixels wide again.

00:48 Now bring the row back on wider screens. But at which width? Plenty of stylesheets use device widths, such as 768 pixels for a tablet. Below the list’s rule, add a query for a width of at least 768 pixels.

01:03 Paste the three flex lines into a header rule inside it, and save. At 1280 pixels, the header is one row again. At 767, it’s stacked. Now set the width to 768.

01:17 The row is back, but it doesn’t fit. The garden’s name breaks over two lines, and About drops to a second row on its own.

01:27 Drag the page’s edge to make it wider, and watch the header. At 790 pixels, nothing has changed. At 809, it’s the same. At 810, the name and all seven links only just fit on one row.

01:42 So here, the row needs 810 pixels. That number comes from the garden’s name and seven links, in this recording’s font. Your font may move it a little, so note where your row fits.

01:54 A tablet’s width has nothing to do with the name and links, so it was a detour. Dragging is how you find where any part of a page breaks.

02:04 810 divided by 16 is about 51, so round up a little to 52rem. If your row fit at another width, divide yours by 16 and round up the same way. Swap 768 pixels for your value: 52rem here, 832 pixels at the default text size.

02:24 Save, and drag again. Up to 831 pixels, the header stays stacked, its seven links in one row under the name. At 832, it becomes a row. Drag your own page to check your value.

02:37 In rem, the breakpoint also moves with the visitor’s text size. With 20-pixel text, the bigger links need a wider row, and 52rem grows to 1040 pixels.

02:50 So 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.

03:02 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.

03:18 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.

Become a Member to join the conversation.