Skip to content
BENIBIX Learn

Flexbox, Grid and Position

Level 4 · Page layout

Three ways CSS places boxes on a page. When Flexbox fits a row, when CSS Grid fits rows and columns, and when position pins one box.

Course contentsLesson 9 of 14

After this lesson you can

  • Lay out a row with Flexbox
  • Build a grid of cards with CSS Grid
  • Pin one box in place with position

14 mincovers 3 glossary entries

Worth reading first: The CSS Box Model

Which layout tool fits which job

CSS has three main tools for placing boxes. Flexbox lines boxes up in one direction, a row or a column. CSS Grid places them in rows and columns at once. The position property takes one box and pins it to a spot. It has 5 values, and static, the normal flow, is the default.

Flexbox starts with display: flex on the parent box. Its children then sit in a row. justify-content spreads them along the row, and align-items lines them up across it. The gap property sets the space between them. A menu bar, a row of buttons or a card's top line is a Flexbox job.

Flexbox shines when the boxes decide the size. Each item takes the room its content needs, and flex: 1 tells one item to take whatever space is left. If the row is too narrow, flex-wrap: wrap lets items drop to a new line.

CSS Grid starts with display: grid. You then draw the columns, and the children fill them in order. grid-template-columns: repeat(3, 1fr) makes three equal columns, where fr means a share of the free space. A page frame, a photo wall or a price table is a job for CSS Grid.

CSS Grid shines when the layout decides the size. The columns are set first, and the content fits into them. Items can also span, so a large photo can take two columns while the rest take one. Rows line up across the whole grid, which Flexbox cannot do.

Position is for the odd box out. Relative nudges a box from where it would be. Absolute lifts it out of the flow and places it inside the nearest box that has a position set. Fixed pins it to the window, and sticky lets it scroll until it reaches an edge, then holds it there.

People often think CSS Grid has replaced Flexbox. Most pages use both: CSS Grid for the big frame, and Flexbox inside each part. And position: absolute is not a layout tool. A page built from it falls apart on the first screen size the author did not test.

The three layout tools side by side

ToolPlaces boxes inGood forStarts with
FlexboxOne row or one columnMenus, button rowsdisplay: flex
CSS GridRows and columns at oncePage frames, card wallsdisplay: grid
PositionOne spot, out of the rowSticky headers, badgesposition: sticky

Which one to pick

  1. 1

    Flexbox

    Pick it when a few items share one line and each should take the room its content needs.

  2. 2

    CSS Grid

    Pick it when you know the columns first and want the items to line up in rows and columns.

  3. 3

    Position

    Pick it when one box must stay in a set spot, such as a header that sticks while you scroll.

css
.menu  { display: flex; justify-content: space-between; gap: 1rem; }

.cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }

.top   { position: sticky; top: 0; }
A menu row, a wall of three equal columns, and a top bar that stays put as you scroll.
Flexbox works along one line. Grid works in rows and columns at the same time.

Worked example

Lucy lays out a shop page

Lucy's shop page shows 12 products. She uses CSS Grid with 3 columns, so they fall into 4 neat rows.

Inside each card, Flexbox puts the price on the left and the buy button on the right. The top bar uses position: sticky, so it stays in view as the 12 cards scroll past.

The words for this lesson3

Check yourself

When should you use CSS Grid instead of Flexbox?

Finished this lesson?

Marking it complete also marks its 3 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.