Lesson 6: Layout with Flexbox
For years, laying out a web page meant fighting with floats and hacks, and simple jobs such as centring a box or putting three cards in a row took surprising effort. Flexbox, short for Flexible Box Layout, changed that: it is a CSS layout mode designed for arranging items in a row or a column, and it handles the spacing, alignment and wrapping that used to be painful. You turn it on by setting display: flex on a container, and its children, called flex items, line up along a main axis and a cross axis, with simple properties to control how they spread out, line up and grow. This lesson covers the properties you will use every day, and by the end of it you will be able to build navigation bars, card rows and centred layouts with a handful of lines.
The container and its two axes
When you set display: flex on an element, it becomes a flex container and its direct children become flex items. The container has a main axis, which runs horizontally by default, and a cross axis, which runs vertically, and the flex-direction property lets you flip them: row is the default, row-reverse runs right to left, column stacks items vertically, and column-reverse stacks them bottom to top. The key idea is that you describe the layout once on the container, and the items follow, which is much easier than positioning each item by hand. A navigation bar, for example, is a flex container with row direction, and a sidebar layout is a flex container holding a nav and a main area side by side, with the main area told to grow and fill the remaining space.
Justifying and aligning
Two properties do most of the alignment work. justify-content controls spacing along the main axis: flex-start packs items to the start, center centres them, space-between pushes the first and last items to the edges and spreads the rest evenly, and space-around gives every item equal space on both sides. align-items controls the cross axis: stretch makes items fill the container's height, center centres them, flex-start lines them up at the top, and flex-end at the bottom. To centre something both ways, which is famously tricky in older CSS, you write just three lines: display: flex; justify-content: center; align-items: center; and the item sits in the middle of its container. That single pattern alone is worth learning Flexbox for, and you will use it in almost every project you build.
.card-row {
display: flex;
gap: 1rem;
flex-wrap: wrap;
}
.card-row .card {
flex: 1 1 200px;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
Growing, shrinking and wrapping
Flex items can grow and shrink to fill the space available, which is controlled by three properties that are usually written together as the flex shorthand: flex-grow says how much an item expands when there is spare room, flex-shrink says how much it shrinks when space is tight, and flex-basis says how big it starts before growing or shrinking. A value of flex: 1 on three cards makes them share the row equally, while flex: 2 on one card makes it take twice the space of its neighbours, and flex: 1 1 200px says start at 200 pixels, then grow or shrink as needed. By default, flex items try to fit on one line and will shrink to do so, which can squeeze them uncomfortably, so add flex-wrap: wrap to let items move onto a new line when they run out of room, and the gap property to put even space between them without fiddly margins. Together, these properties make layouts that adapt gracefully instead of breaking.
When Flexbox is the right tool
Flexbox is the right tool when you are arranging items in one direction, a row or a column, which covers navigation bars, button groups, card rows, form fields with labels, and centring. It is less suited to two-dimensional layouts, such as a full page grid with rows and columns at once, and that is the job of CSS Grid, which you will meet in the next lesson. A good rule is to reach for Flexbox first for components, and for Grid when the layout needs both rows and columns, and many real pages use both: a Grid page layout holding Flexbox navigation and card rows inside it. Practise by rebuilding a simple page header, a row of three equal cards, and a footer, using only the properties in this lesson, and notice how little code it takes compared with the old float-based approach.
Key Terms
- Flexbox
- A CSS layout mode for arranging items in a row or a column, with easy alignment and spacing.
- Flex container
- The element with display: flex, whose children become flex items.
- Flex item
- A direct child of a flex container, arranged by the container's flex properties.
- Main axis
- The direction items are laid out in, horizontal by default; controlled by flex-direction.
- Cross axis
- The axis at right angles to the main axis, controlled by align-items.
- justify-content
- A property that controls spacing along the main axis, such as center or space-between.
- flex
- A shorthand for flex-grow, flex-shrink and flex-basis, controlling how an item grows and shrinks.
Discussion Questions
- Why is centring an item both ways so much easier with Flexbox than with older CSS techniques?
- A row of cards looks good on a wide screen but squeezes badly on a phone. Which two properties would fix it, and how?
- When would you choose Flexbox over Grid, and when would you choose Grid instead?
Practice Questions
- What single declaration turns an element into a flex container?
- What is the difference between justify-content and align-items?
- How do you centre an item both horizontally and vertically with Flexbox?
- What does flex: 1 do when three cards in a row all have it?
- Why add flex-wrap: wrap to a row of cards?
Show answers
- display: flex; Its direct children then become flex items arranged along the main and cross axes.
- justify-content works along the main axis and align-items along the cross axis. With the default row direction, justify-content spaces items horizontally and align-items lines them up vertically.
- display: flex; justify-content: center; align-items: center; The container centres its items on both axes at once.
- It makes them share the available space equally. Each card grows by the same amount, so the row is split into three equal parts.
- So items can move onto a new line instead of squeezing. Without it, flex items shrink to fit on one line, which can make them too narrow on small screens.