Justify content helps you control how items line up horizontally inside a container. It makes your page look neat and balanced.
Justify content in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
.container { display: flex; justify-content: value; }
You must set display: flex; or display: grid; on the container first.
The value controls how items align horizontally.
.container { display: flex; justify-content: flex-start; }
.container { display: flex; justify-content: center; }
.container { display: flex; justify-content: space-between; }
.container { display: flex; justify-content: space-around; }
This example shows three blue boxes spaced evenly across a gray container using justify-content: space-between;. The boxes appear at the left, center, and right with equal space between them.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Justify Content Example</title> <style> .container { display: flex; justify-content: space-between; background-color: #f0f0f0; padding: 1rem; border: 2px solid #ccc; max-width: 600px; margin: 2rem auto; } .box { background-color: #4a90e2; color: white; padding: 1rem 2rem; border-radius: 0.5rem; font-weight: bold; user-select: none; } </style> </head> <body> <section class="container" aria-label="Justify content example"> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box">Box 3</div> </section> </body> </html>
If you use justify-content without display: flex or display: grid, it won't work.
Try changing the justify-content value in the sample to see how the layout changes.
Use browser DevTools (right-click > Inspect) to experiment with justify-content live on any page.
Justify content controls horizontal alignment of items inside a flex or grid container.
Common values: flex-start, center, space-between, space-around.
It helps make layouts look balanced and organized.
Practice
justify-content control in a flex container?Solution
Step 1: Understand the role of
This property controls how items are aligned horizontally inside a flex or grid container.justify-contentStep 2: Differentiate from vertical alignment
Vertical alignment is controlled byalign-items, notjustify-content.Final Answer:
The horizontal alignment of items inside the container -> Option BQuick Check:
Justify content = horizontal alignment [OK]
- Confusing justify-content with align-items
- Thinking it changes item size
- Assuming it controls colors
justify-content in a flex container?Solution
Step 1: Recall valid values for
Common valid values includejustify-contentflex-start,center,space-between, andspace-around.Step 2: Identify the correct syntax for centering
The correct value to center items horizontally iscenter, so the syntax isjustify-content: center;.Final Answer:
justify-content: center; -> Option DQuick Check:
Centering uses 'center' value [OK]
- Using invalid values like 'middle' or 'align-center'
- Missing the colon or semicolon
- Confusing with align-items syntax
div.container {
display: flex;
justify-content: space-between;
width: 300px;
}
<div class="container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>Solution
Step 1: Understand
This value places the first item at the start, the last item at the end, and evenly distributes space between the items.justify-content: space-between;Step 2: Visualize the layout
With three items, the spaces between them are equal, but no extra space is added at the container edges.Final Answer:
Items are evenly spaced with equal space between them -> Option CQuick Check:
Space-between = equal gaps between items [OK]
- Thinking space-between adds space around edges
- Confusing with space-around or center
- Assuming items cluster on one side
justify-content from working:div.container {
display: block;
justify-content: center;
}Solution
Step 1: Check the display property
justify-contentonly works on flex or grid containers, but here display is set toblock.Step 2: Understand the requirement for flex/grid
Withoutdisplay: flex;ordisplay: grid;,justify-contenthas no effect.Final Answer:
The container must have display: flex; or display: grid; for justify-content to work -> Option AQuick Check:
Justify-content needs flex or grid [OK]
- Using justify-content on block containers
- Confusing justify-content with justify-items
- Thinking value 'center' is invalid
justify-content value should you use in your flex container?Solution
Step 1: Understand the difference between space-between and space-around
space-betweenputs equal space only between items, no space at edges.space-aroundadds equal space around each item, including edges.Step 2: Match requirement to property value
Since the requirement is equal space around each link including edges,space-aroundis the correct choice.Final Answer:
space-around -> Option AQuick Check:
Space-around = equal space around all items [OK]
- Choosing space-between which skips edges
- Using center which groups items
- Using flex-start which aligns left only
