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

Align Items on Both Axes

In this lesson, you gave the events banner room with min-height: 20rem and moved its heading and paragraph with flexbox’s alignment properties, watching them in DevTools’ flex overlay.

Key Points

  • justify-content moves flex items along the main axis. It shares out the space left over on that axis. When the items already fill the axis, as the heading and the paragraph fill the banner’s row, there’s nothing to share, and nothing moves. Where the row has room, as in the menu, center puts half the leftover space before the items and half after.
  • align-items moves flex items across, on the cross axis. By default, it stretches every item to fill the container across: in a row, as tall as the container, margins included. (The Computed pane calls that default normal, which stretches flex items.) With center, the items shrink to their content’s height and sit in the middle.
  • align-self on one item takes the same values as align-items and wins over it for that item. flex-end means the end of the axis: across a row, the bottom; across a column, the right side. The parent still does the arranging: align-self asks the parent’s flex layout to place that one item differently, so without display: flex on the parent, it does nothing.
  • A min-height of 20rem (320 pixels) gives the banner height to move items in (the cross axis in a row, the main axis once it’s a column), and the banner can still grow if its text needs more space.
  • flex-direction: column turns the axes, so the two properties swap which way they move on screen. Watch out: justify-content isn’t “horizontal” and align-items isn’t “vertical”. They follow the axes.

Final Code

The end of events/layout.css:

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 Your menu is a row now. Next comes the banner at the top of the events page: the heading Upcoming Events and a one-line paragraph under it. Rosa wants it to stand out as a tall welcome block, with the heading and the paragraph in the middle.

00:15 At the end of layout.css, add a rule for the events banner. Give it display: flex, so it can arrange the heading and the paragraph. Then give it a min-height of 20rem, which is 320 pixels, so there’s room to move them.

00:29 Save, and in DevTools, click the banner’s flex badge to turn on its overlay. The heading and the paragraph are now flex items, side by side in a row. Each one, margins included, stretches from the banner’s top to its bottom.

00:47 Two properties move flex items. The first, justify-content, works along the main axis, which in a row runs left to right. Add justify-content: center to the banner’s rule, and save.

01:02 Nothing moves. You can see why in the overlay. The heading and the paragraph already fill the row, and each one wraps onto two lines to fit. Justify-content shares out the space left over on the main axis, and here there’s none to share.

01:18 How wide each item ends up is the next lesson.

01:22 Where a row has room, center shares it. If your menu had justify-content: center, it would put half the leftover space before the items and half after.

01:33 The second, align-items, works across, on the cross axis, which in a row runs top to bottom. By default, it stretches flex items, which is why both fill the banner’s height.

01:47 Add align-items: center to the rule, and save. Both items shrink to the height of their content and move to the middle of the banner, top to bottom.

01:58 To move one item on its own, set align-self on that item. It takes the same values as align-items, and it wins over align-items for that one item. The banner still does the arranging: align-self only asks the banner’s flex layout to place this one item differently.

02:16 Without display: flex on the banner, it does nothing. Add a new rule for the banner’s paragraph, set align-self to flex-end, and save. Flex-end means the end of the axis, and across a row that’s the bottom.

02:31 The paragraph drops to the bottom of the banner. The heading stays in the middle.

02:37 Now turn the direction: add flex-direction: column to the banner’s rule. The main axis now runs down the page, and the cross axis runs left to right. Don’t save yet.

02:48 Your turn: the paragraph still has its align-self rule. Pause the video and predict where it ends up when you save.

03:01 Save, and the paragraph sits at the right edge of the banner. Across a column means left to right, so the end is the right side.

03:13 And justify-content: center, the declaration that did nothing in the row, now centers both items as a group, top to bottom. And align-items: center now centers the heading left to right.

03:27 Each item has the banner’s full width to itself, so neither wraps. Watch out: it’s tempting to learn justify-content as horizontal and align-items as vertical.

03:39 They follow the axes instead, so in a column, they swap.

03:44 Delete the paragraph’s rule, and save. With align-items: center, the paragraph joins the heading in the middle. That’s Rosa’s welcome block.

03:55 So justify-content moves items along the main axis, into the space left over. Align-items moves them across it, and align-self moves one item. And flex-direction decides which way that is on screen.

04:11 Next, in Let Items Grow and Shrink, you’ll find out how wide each flex item ends up, and why the heading and the paragraph filled the row.

Become a Member to join the conversation.