liveBook · Manning

Justify-content

As explained in Tiny CSS Projects

The CSS justify-content property controls how free space is distributed along the main axis of a flex or grid layout.

Use `justify-content` on a flex container to position its children along the axis Flexbox is using. It distributes leftover space inside that container. Put the declaration on the parent, not on the item you want to move. section 6.4 section 6.9

Choose a value to match the layout you want:

  • `center` centers a profile card when its parent, `body`, has `display: flex`. Pair it with `align-items: center` to center the card vertically too. section 6.4
  • `space-around` spreads profile statistics across a row, leaving equal space between groups and half as much at each edge. section 6.7.2
  • `space-between` places credit-card details at opposite edges, with space between them. `flex-end` keeps text at the end of a security-code band. section 9.2.2 section 9.2.3

Center two actions without crowding them

Set `.actions { display: flex; gap: 16px; justify-content: center; }`. `justify-content` centers the link and button as a group; `gap` preserves space between them. The same pairing works for profile statistics, where a `12px` gap supplies minimum separation while `space-around` distributes the remaining room. section 6.8 section 6.7.2

Watch the widths of your items. With `space-around`, evenly distributed free space does not guarantee that the middle item sits at the container’s exact center: a wider group on one side can shift it. section 6.7.2

Listing 6.5 Centering the card
body {
  ...
  display: flex;
  justify-content: center;    #1
  align-items: center;        #2
}

The body rule shows `display: flex` on the parent, then `justify-content: center` and `align-items: center` positioning its card. section 6.4

How Tiny CSS Projects covers it

How deep it goes
Explains four values through layout rules and shows why unequal item widths can make a space-around row look off-center.
Examples
Shows a centered profile card and actions, spaced profile statistics, and details arranged across a virtual credit card.
What it assumes
Introduces `display: flex` on a parent before positioning its child; assumes familiarity with basic CSS rules and HTML elements.
What you can do after
Apply it via CSS Flexbox to center children or distribute them across a container, while checking gaps and unequal widths.
How it connects
Connects `justify-content` to Flexbox, `display: flex`, `align-items`, `gap`, and item widths.

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 Tiny CSS Projects. The section links point to the parts of the book it draws on.