liveBook · Manning

Justify-content

As explained in Web Design Playground

A CSS alignment property that distributes and aligns items along the main axis of a flex container or grid container.

Think of a flex container as a shelf. Once flex-direction chooses which way the shelf runs, justify-content decides where its items sit along that main axis: at the start, end, center, or spread through the available room. It belongs on the container, not on each individual item. section 12.2

Choose a value based on the arrangement you want:

  • flex-start places items at the beginning of the container. It is the default, so writing it explicitly is optional. section 12.2
  • flex-end places items at the end, and center places them in the middle. section 12.2
  • space-between puts the first item at the beginning, the last at the end, and distributes the remaining items evenly between them. section 12.2
  • space-around gives every item equal space on both sides. Interior gaps look larger because neighboring items contribute space to the same gap. section 12.2
container {
    display: flex;
    justify-content: flex-start|flex-end|center|space-between|space-around;
}

Start by making the element a flex container, then select one value. With the usual horizontal main axis, these choices move items left-to-right; change flex-direction and the same property follows the new main axis. section 12.2

Center a heading in a window

Make a div fill the browser height and turn it into a flex container. Set justify-content: center to center its h1 along the main axis, then pair it with align-items: center to center it along the cross axis too. section 12.5

For a larger layout, justify-content can center a row of page content in the browser window. A nested column can instead use flex-start so its content begins at the top. Try changing one value at a time and watch the available space move. section 20.10 section 12.2

How Web Design Playground covers it

How deep it goes
Defines five flexbox values and explains the uneven outer spacing produced by space-around.
Examples
Uses a centered h1 and a portfolio layout with a centered row and top-aligned column.
What it assumes
Teaches placement after flex-direction sets the main axis and a container becomes a flex container.
What you can do after
Apply it via CSS Flexbox to place items at an axis start, end, center, or distributed spacing.
How it connects
Connects flex-direction, align-items, and flex containers when positioning items in two axes.

Also explained in

Compare how 4 books explain justify-content →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground. The section links point to the parts of the book it draws on.