Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Wrap Items and Keep Their Order

In this lesson, you laid out the seed swap page’s seed tags as a row that wraps onto new lines, and moved the Donate seeds link to the front in a way that keyboard users follow too.

The Seed Tags

To follow along, paste this block into events/seed-swap.html, after the How to swap block, at the end of <main>:

Language: HTML
<!-- New in this lesson: the seed tags -->
<h2>On the Seed List</h2>
<ul class="seed-tags">
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#tomatoes">Tomatoes</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#runner-beans">Runner beans</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#squash">Squash</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#lettuce">Lettuce</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#carrots">Carrots</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#sweet-peas">Sweet peas</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#basil">Basil</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#sunflowers">Sunflowers</a></li>
  <li><a href="https://elmstreetgarden.org/seedswap/seedlist#marigolds">Marigolds</a></li>
  <li class="donate"><a href="https://elmstreetgarden.org/seedswap/donate">Donate seeds</a></li>
</ul>

And paste this <style> element into the page’s <head>, after the two stylesheet links. It gives each tag its rounded border:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 The seed swap page needs a list of seed tags, each a link, under a new heading, On the Seed List. Donate seeds comes last. A style element in the head gives each tag its rounded border.

00:12 Stop here, paste both blocks from the text below, and save. Priya, who collects the swap’s seeds, wants the tags in a row. At the end of layout.css, add a seed tags rule with display: flex and a gap of the small spacing token, 8 pixels.

00:30 Save. The tags sit in one row, but it doesn’t fit. The two-word tags shrink to their longest word, and the row still runs 308 pixels past the column’s edge.

00:42 The page scrolls sideways.

00:46 A flex row stays on one line by default. To let it break, add flex-wrap: wrap to the rule, and save. Now the tags fill the first line from left to right. Sweet peas is the last one that fits, so Basil starts a second line.

01:02 The gap spaces the lines too, 8 pixels apart. Drag the window narrower. The last tag on each line moves down to the start of the next. At 640 pixels, Donate seeds sits on a third line alone.

01:18 It works like words wrapping in a paragraph: the tags keep their HTML order, left to right, then line by line. Unlike a paragraph, each line is a flex line of its own, with its own alignment.

01:31 Widen the window again.

01:34 Inside each line, align-items lines the tags up. To move the lines themselves, use align-content.

01:42 A row that doesn’t wrap, like the events banner, is one line as tall as its container, so there’s nothing for align-content to move. It works across, but only when the row wraps and has height to spare.

01:56 To see it, give the list a min-height of 12rem for a moment, and save. By default, the lines stretch to share the spare height, so the second line drops down.

02:07 Add align-content: flex-start, and save. The lines pack together at the top, 8 pixels apart again. Then delete both new lines.

02:19 Priya wants Donate seeds first. Flexbox can move a box on screen with the order property, and you’ll check the result with the keyboard. Every item’s order is zero by default, and lower numbers come first.

02:32 The Donate seeds list item has the class donate. Add a rule for it with order: -1, and save. Donate seeds jumps to the start of the first line, and the other tags move along one place.

02:46 Now the keyboard. Click the On the Seed List heading, above the tags, so Tab starts there. Press Tab. Focus lands on Tomatoes, the second tag on screen. Keep pressing.

03:01 Only after Marigolds does focus jump back to Donate seeds, at the top left. Watch out: order moves only the box on screen, while Tab and screen readers still follow the HTML, so a keyboard user’s focus skips the first tag they see and reaches it last.

03:20 Change the order in the HTML instead. Delete the donate rule from layout.css. In seed-swap.html, move the Donate seeds list item to the top of the list, and save both files.

03:35 The page looks exactly as it did with order. Click the heading again and press Tab: now Donate seeds comes first, and focus follows the tags line by line, the way you read them.

03:48 So a flex row stays on one line until you add flex-wrap: wrap. Then the items fill each line in the HTML’s order. Align-content moves wrapped lines when there’s room.

03:59 And order changes only the screen, so keep it for items whose reading order doesn’t matter. Otherwise, change the HTML.

04:08 Next comes Exercise: Line Up the Seed Swap Page, where you’ll lay out a seed swap page from its menu to its tags.

Become a Member to join the conversation.