SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
FlexboxDirection and WrappingFlex Item SizingMain-Axis AlignmentCross-Axis AlignmentFlexbox PatternsDebugging Flexbox Sizing and aligning a flex layout
Flexbox Layouts

Main-Axis Alignment

Distribute flex items along the main axis.

justify-content places the items along the main axis using any leftover space. It works on each flex line after the items have grown or shrunk.

Start with a container that has spare space

.actions {
  display: flex;
  justify-content: center;
  gap: 1rem;
}

Assume the direct children are three buttons. In the initial row direction, they form a centered group, with a 1rem gap between neighbors.

With a 600-pixel content width, three 100-pixel items, and two 16-pixel gaps, 268 pixels remain. Centering puts 134 pixels before the group and 134 pixels after it. This example assumes no margins, borders, or padding affect those measurements.

Choose a distribution

ValueEffect along the main axis
flex-startPacks items toward main-start.
flex-endPacks items toward main-end.
centerCenters the group.
space-betweenPlaces remaining space between items, with none added at the outer edges.
space-aroundAdds an equal share on each side of an item. At an outer edge this contributes one share, between items it contributes two.
space-evenlyDistributes equal shares of spare space before, between, and after the items. A declared gap still adds extra room between neighbors.

gap still applies. With distributed alignment, the visible space between items can be larger than the declared gap.

In a 600px row, centering three 100px items with a 16px gap leaves 134px at each edge. Space-between moves the outer items to the edges and leaves 150px between neighbors.

Follow the main axis when direction changes

.stack {
  display: flex;
  flex-direction: column;
  min-height: 20rem;
  justify-content: space-between;
}

In horizontal text, the main axis now runs vertically. The extra height provides room for the first and last items to move apart. Without extra main-axis space, changing the distribution may have little visible effect.

For a wrapped container, each line performs its own main-axis distribution. A line containing one item will not reproduce the spacing of a line containing three.

Push one group away with an auto margin

.toolbar {
  display: flex;
  gap: 1rem;
}

.toolbar > .account-link {
  margin-inline-start: auto;
}

In a normal row, the auto margin takes the spare space before the account link. The earlier items stay together and the account link moves to the far end.

justify-self does not align individual flex items on the main axis. Use an auto margin for this layout.

Predict the distribution

In the 600px example, what happens to the 268px of spare space if center becomes space-between? Keep the two 16px gaps.

Check your reasoning

The 268px is divided between the two spaces: 134px is added to each existing 16px gap, for 150px between neighbors. No extra space is added at the edges.

If the buttons grow until they fill the row, why might changing justify-content make no visible difference?

Check your reasoning

There is no positive free space left to distribute after sizing and gaps.

Key Takeaways

  • Main axis: justify-content follows the chosen flex direction.
  • Distribution: Alignment uses space left after sizing, gaps, and auto margins.
  • Groups: An auto margin can separate one group from another.
  • Wrapped lines: Each line distributes its own available space.

Try it yourself

Try it yourself

Distribute a row

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

How is this lesson?

Previous lessonFlex Item SizingNext lessonCross-Axis Alignment
Edit on GitHubReport on GitHub
On this page
%

On this page

Start with a container that has spare spaceChoose a distributionFollow the main axis when direction changesPush one group away with an auto marginPredict the distributionKey TakeawaysTry it yourself