Skip to content
  • Official
  • Beginner
  • English
  • 10 steps · 2 quizzes

CSS Flexbox Exercises: Lay Out a Page Step by Step

CSS flexbox exercises on real page parts: write CSS in the lesson, match a target nav bar, card row and page, with checks as you go.

  • Free
  • No account needed
  • Stop any time, pick up where you left off
Workshopy.io

WorkshopyOfficial

78 lessons · used by 2 teachers

For teachers

Teaching this? Run it live with your class.

  • Students join with a code — no accounts
  • See who’s done and who’s stuck, step by step
  • It’s your copy — change anything before class

Sign in to copy it to your workspace. It’s free.

Your path

10 steps · 2 quizzes · open any step, or read it here

  1. Start here: why won't these boxes line up?Start hereThese CSS flexbox exercises start with a problem everyone who builds pages meets: you put a logo, three links and a button in a nav bar, and they stack one under another instead of sitting in a row.InteractiveHands-on
    Read this step

    These CSS flexbox exercises start with a problem everyone who builds pages meets: you put a logo, three links and a button in a nav bar, and they stack one under another instead of sitting in a row. Flexbox is the part of CSS that lines boxes up, shares out the space between them and lets them wrap, and you'll practise it here on real page parts.

    Interactive · From a stack of parts to a laid-out page — try it in the lesson

    You just watched five parts of a nav bar flow onto one line, cards grow to share a row and a button get pushed to the far end. Each of those moves is one or two lines of CSS, and by the end you'll write all of them yourself.

    What you'll do

    • Switch on flexbox and see what changes for the container and for its items.
    • Place items along the main axis and across it, on a nav bar, a dashboard header and a chat.
    • Size items with flex-grow, flex-shrink and flex-basis, and work out the widths by hand.
    • Build a whole page: a nav bar, a grid of cards and a footer.

    By the end you can

    • Read a flexbox layout and say which property puts each box where it is.
    • Lay out nav bars, card rows and headers without floats, tables or fixed positions.
    • Fix a layout that squeezes, overflows or sits in the wrong place.

    You need a browser and a little CSS: a selector, a property and a value.

  2. display: flex: one container, its items in a rowFlexbox starts with one line of CSS on a parent elementInteractiveHands-on
    Read this step

    Flexbox starts with one line of CSS on a parent element:

    .nav {
      display: flex;
    }
    

    That makes .nav a flex container, and its direct children become flex items. Only the children: a link inside a list inside the nav is not an item of the nav.

    What changes the moment you turn it on:

    • The items stop taking a line each (that's what block boxes do) and sit side by side, in HTML order, from the start of the row.
    • Each item is only as wide as its content needs, and every item stretches to the height of the row.
    • The container itself still behaves like a block on the page: it takes the full width and pushes the next element down.

    Every flexbox property belongs to one side. display, flex-direction, justify-content, align-items, flex-wrap and gap go on the container. flex-grow, flex-shrink, flex-basis, order and align-self go on an item.

    Try it: switch on display: flex on the nav, then flex: 1 on the cards and margin-left: auto on the button. Then turn them off one at a time and watch which part moves back.

    Interactive · Turn on flexbox and watch the page lay itself out — try it in the lesson

    The cards row in the picture is a second flex container: a page is usually many small flex containers, one inside another.

    Takeaway: display: flex goes on the parent; its direct children are the items you then arrange.

  3. Main axis and cross axis: flex-directionA flex container lays its items along a line called the main axis. The line across it is the cross axis. Every other flexbox property is defined by these two axes, so it pays to know where they point. flex-direction sets the main axisInteractiveHands-on
    Read this step

    A flex container lays its items along a line called the main axis. The line across it is the cross axis. Every other flexbox property is defined by these two axes, so it pays to know where they point.

    flex-direction sets the main axis:

    Value Main axis runs Cross axis runs
    row (the default) left to right top to bottom
    row-reverse right to left top to bottom
    column top to bottom left to right
    column-reverse bottom to top left to right

    In a right-to-left page row runs right to left: the main axis follows the writing direction.

    When you switch to column, the axes swap. The properties don't: justify-content still works along the main axis (now vertical) and align-items across it (now horizontal). That swap is the most common surprise in flexbox.

    The -reverse values change only where the items are drawn. The HTML order stays the same, so screen readers and the Tab key still follow the HTML.

    Try it: do the three tasks. The dashed outlines show where each part should land; your boxes move as you type.

    Interactive · Practice: flex-direction on a toolbar, a menu and a chat — try it in the lesson

    Takeaway: flex-direction picks the main axis; row is the default, column stacks, and the -reverse values flip the drawing order only.

  4. justify-content: share the space along the main axisWhen the items don't fill the main axis, there is free space left over. justify-content, on the container, decides where that space goesInteractiveHands-on
    Read this step

    When the items don't fill the main axis, there is free space left over. justify-content, on the container, decides where that space goes:

    Value What happens
    flex-start (the default behaviour) Items packed at the start, the free space at the end.
    flex-end Items packed at the end.
    center Items in the middle, equal free space on both sides.
    space-between First item at the start, last at the end, the rest of the space shared equally between items.
    space-around Each item gets equal space on both sides, so the ends get half a share.
    space-evenly Equal space between items and at both ends.

    justify-content only moves space that exists. If the items already fill the row (or overflow it), it has nothing to share and changes nothing you can see.

    In the nav bar below, the links are wrapped in one div, so the nav has three items: the logo, the group of links and the button. That's why space-between puts the links in the middle.

    Try it: three components, three values.

    Interactive · Practice: justify-content on a nav bar, a pager and a dialog — try it in the lesson

    Takeaway: justify-content moves the free space along the main axis; it does nothing when there is no free space.

  5. align-items: line items up across the axisalign-items, also on the container, places the items on the cross axis: in a row, that's up and down.InteractiveHands-on
    Read this step

    align-items, also on the container, places the items on the cross axis: in a row, that's up and down.

    Value What happens in a row
    stretch (the default behaviour) Each item without a fixed height is stretched to the height of the row.
    flex-start Items at the top, each at its own height.
    flex-end Items at the bottom.
    center Items centred vertically.
    baseline Items moved so the first line of text in each sits on one line.

    stretch being the default explains two things you'll meet often: a small badge that turns into a tall pill next to a big title, and an avatar that runs down the whole side of a long message. An item with a fixed height is not stretched.

    Centring something vertically, the old CSS headache, is two lines on the parent: display: flex; align-items: center; (add justify-content: center; to centre it the other way too).

    Try it: a dashboard header, a message in an inbox and a price tag.

    Interactive · Practice: align-items on a dashboard header, a message and a price — try it in the lesson

    Takeaway: align-items places items across the main axis; its default is stretch.

  6. Check: containers and axesFour questions on what you've used so far.Quiz · 4 questions
    Read the questions

    Four questions on what you've used so far.

    Question 1. A nav bar's HTML is `<nav class="nav">` with a logo, three links and a button inside it. Where does `display: flex` go to put them in a row?

    • On `.nav`
    • On each link and the button
    • On the `body`

    Question 2. A container has `flex-direction: column`. Which way does `justify-content: center` move its items?

    • Left to right, to the middle of the row
    • Up and down, to the middle of the column
    • It does nothing in a column

    Question 3. A flex container has no `align-items` set. What do its items do on the cross axis?

    • They stretch to fill the container's height (in a row)
    • They sit at the top at their own height
    • They are centred

    Question 4. Three items in a row with `justify-content: space-between`. Where are they?

    • All three in the middle, with equal space around the group
    • The first at the start, the last at the end, the middle one with equal space on both sides
    • Equal space before the first, between all, and after the last
  7. gap and flex-wrap: a row of cards that wrapsTwo container properties handle space between items and what happens when they don't fit. gap puts space between items, never before the first or after the lastInteractiveHands-on
    Read this step

    Two container properties handle space between items and what happens when they don't fit.

    gap puts space between items, never before the first or after the last:

    .plans {
      display: flex;
      gap: 16px;          /* rows and columns */
      /* gap: 24px 16px;     row gap, then column gap */
    }
    

    Before gap worked in flexbox, people put margins on items and then removed the margin from the last one. gap does it in one line.

    flex-wrap decides what happens when the items are wider than the container. The default is nowrap: all items stay on one line, and because flex-shrink is 1 by default, they shrink to fit, even below the width you gave them, until they reach their content's minimum width. After that they overflow the container. With flex-wrap: wrap, items that don't fit move to a new line instead, and each line is laid out on its own.

    This is the usual cause of "my 220 px cards are only 180 px wide": the row is nowrap and the cards were shrunk.

    Try it: avatars, pricing plans and skill tags.

    Interactive · Practice: gap and flex-wrap on avatars, pricing plans and tags — try it in the lesson

    Takeaway: gap spaces items without extra margins; flex-wrap: wrap lets items move to a new line instead of shrinking or overflowing.

  8. flex-grow, flex-shrink, flex-basis and the flex shorthandThree item properties decide how wide each item ends up along the main axis: flex-basis: the size the item starts from before free space is shared out. The default auto means "use the item's width, or its content's size if it has none".InteractiveHands-on
    Read this step

    Three item properties decide how wide each item ends up along the main axis:

    • flex-basis: the size the item starts from before free space is shared out. The default auto means "use the item's width, or its content's size if it has none".
    • flex-grow: how much of the free space the item takes, as a share. The default is 0: items don't grow. Two items with flex-grow: 1 and flex-grow: 2 split the free space 1 : 2.
    • flex-shrink: how much the item gives up when the items are too wide for the container. The default is 1: every item may shrink. Shrinking is weighted by the basis, so a bigger item gives up more pixels. 0 means it never shrinks.

    The flex shorthand sets all three, in the order grow, shrink, basis:

    You write It means
    flex: 1 flex: 1 1 0%: grow and shrink, start from 0, so the items share all the space by their grow values
    flex: auto flex: 1 1 auto: grow and shrink, starting from the item's own width
    flex: none flex: 0 0 auto: keep the item's own width, never grow or shrink
    flex: 0 0 160px exactly 160 px, never more, never less
    (nothing set) flex: 0 1 auto: don't grow, may shrink

    The difference between flex: 1 and flex: auto is the basis: with 0% the whole row is free space, so equal grow values give equal widths whatever the content; with auto each item starts from its content and only the leftovers are shared.

    One more rule: a flex item won't shrink below its content's minimum width (min-width: auto). A long unbroken word or URL can keep it from shrinking; min-width: 0 on the item lets it.

    Try it: a search bar, an app layout with a sidebar, and dashboard tiles.

    Interactive · Practice: flex-grow, flex-basis and the flex shorthand — try it in the lesson

    Takeaway: flex: 1 shares the space equally, flex: 0 0 <size> fixes an item's size, and grow values set the shares of the free space.

  9. How wide is each item?Work out each width by hand, then check. In every exercise the items have no padding, no border and no margin, and hold only a short word, so nothing stops them shrinking. A hint costs nothing.Practice · 7 exercises
    Read this step

    Work out each width by hand, then check. In every exercise the items have no padding, no border and no margin, and hold only a short word, so nothing stops them shrinking. A hint costs nothing.

    A container 600 px wide holds three items, each with flex-basis: 100px and flex-grow: 1, and no gap. How wide is each item, in px?

    Same container (600 px), three items with flex-basis: 100px, but flex-grow of 1, 1 and 2. How wide is the third item, in px?

    A card row 820 px wide has gap: 20px and three cards with flex: 1. How wide is each card, in px?

    An app is 1000 px wide with gap: 24px. The sidebar has flex: 0 0 240px and the main area flex: 1. How wide is the main area, in px?

    A container 500 px wide holds two items, each with flex-basis: 400px and the default flex-shrink: 1, no wrap. How wide is each item, in px?

    A container 500 px wide, no wrap: item A has flex-basis: 400px, item B has flex-basis: 200px, both with flex-shrink: 1. How wide is item A, in px (to two decimals)?

    What does flex: 1 mean in full?

    • flex: 1 1 0%
    • flex: 1 0 auto
    • flex: 1 1 auto
    • flex: 0 1 auto
  10. Push, reorder, align one: margin-left: auto, order and align-selfThree tools for when one item needs to break the pattern. An automatic margin takes all the free space on its side. margin-left: auto on the last item of a row pushes it to the far end: the classic "logo and links on the left, account menu on the right".InteractiveHands-on
    Read this step

    Three tools for when one item needs to break the pattern.

    An automatic margin takes all the free space on its side. margin-left: auto on the last item of a row pushes it to the far end: the classic "logo and links on the left, account menu on the right". It works on the main axis whatever justify-content says, and margin: auto on a lone item centres it both ways. In a right-to-left page, write margin-inline-start: auto so the push follows the writing direction.

    order changes where an item is drawn. Every item starts at order: 0; items are drawn from the lowest value to the highest, and equal values keep their HTML order. So order: -1 moves an item before all the others, order: 1 after them. It changes only the picture: screen readers and the Tab key still follow the HTML, so use it for small visual changes and change the HTML when the reading order should change too.

    align-self is align-items for one item. It overrides the container's value for that item only: align-self: flex-end drops one item to the bottom of a row whose items sit at the top.

    Try it: an admin header, a plans row and a code-review comment.

    Interactive · Practice: margin-left: auto, order and align-self — try it in the lesson

    Takeaway: margin-left: auto pushes, order reorders the picture (not the reading order), align-self aligns one item.

Comments

You’ll sign in with Google before it’s posted.Comments are public — don’t name your students.
0 / 1,500

No comments yet. Be the first to say how it went.

Using it

Workshopy shares this lesson under CC BY 4.0. Anyone may use and change it, with credit to you. Anyone can learn it here, free. A copy in your workspace is yours to change and run live; your students never need an account.

Versions

  1. 1

    Version 1Current

    · First published

Copies are made from the current version. A copy you already have doesn’t change.

More from Workshopy

All 78 lessons