HTML & CSSEasy
What is justify-content?
justify-content distributes space between and around items along the main axis of a flex container (or aligns the whole grid inside a grid container along the inline axis). It only has a visible effect when there is free space left over, so it does nothing if items already fill the container, for example when an item has flex: 1.
Values
flex-start/start(default): pack items at the beginning.flex-end/end: pack at the end.center: pack in the middle.space-between: first and last items touch the edges; equal space between the others.space-around: equal space around each item (so half-size gaps at the edges).space-evenly: exactly equal gaps everywhere, including the edges.stretch: in grid, stretches auto-sized tracks.
Common uses
- Navbar with logo left and links right:
justify-content: space-between. - Centering a group of buttons:
justify-content: center. - Right-aligning dialog actions:
justify-content: flex-end.
Tips
- For a single item pushed to one side,
margin-left: autoon that item is often simpler. gaphandles fixed spacing between items;justify-contenthandles leftover space.- With wrapping,
space-betweencan leave the last row looking odd (items spread far apart); Grid withauto-fillhandles uniform grids better. safe centerprevents content from overflowing off the start edge when it is larger than the container.
Interview tip
Remember that it follows the main axis: in flex-direction: column it works vertically.
Code example
html
1<style>2 body { font-family: system-ui; }3 .row { display: flex; border: 2px solid #0b1020; margin: 6px 0; padding: 4px; background: #f8fafc; }4 .row span { background: #2b4bff; color: white; padding: 4px 10px; border-radius: 4px; }5 code { display: block; margin-top: 10px; }6</style>7<code>flex-start</code><div class="row" style="justify-content:flex-start"><span>A</span><span>B</span><span>C</span></div>8<code>center</code><div class="row" style="justify-content:center"><span>A</span><span>B</span><span>C</span></div>9<code>flex-end</code><div class="row" style="justify-content:flex-end"><span>A</span><span>B</span><span>C</span></div>10<code>space-between</code><div class="row" style="justify-content:space-between"><span>A</span><span>B</span><span>C</span></div>11<code>space-around</code><div class="row" style="justify-content:space-around"><span>A</span><span>B</span><span>C</span></div>12<code>space-evenly</code><div class="row" style="justify-content:space-evenly"><span>A</span><span>B</span><span>C</span></div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.