You can justify the content of your page or content within the layout. The justify-content property aligns the flexible container's items when the items do not use all available space on the main-axis (horizontally).
The numbers in the table specify the first browser version that fully supports the property.
-webkit-justify-content: center; /* Safari 6.1+ */
justify-content: center;
You can see the full example for the justify-content In Div:
Understanding the Complete Div Example
Give the parent usable space
The parent needs available width before alignment can be visible. A block element normally fills its row, but the child widths, borders, padding, and box sizing still affect how much free space remains. If every child grows to fill the parent, center and space-distribution values may appear ineffective.
Use box-sizing: border-box when fixed dimensions include padding and borders. This makes the calculation easier to reason about and reduces unexpected overflow.
* {
box-sizing: border-box;
}
#main {
display: flex;
justify-content: center;
gap: 1rem;
padding: 1rem;
max-width: 900px;
margin-inline: auto;
}
Use gap for predictable spacing
The original example places the children together and centers the group. Adding gap creates a consistent distance without adding different margins to the first and last boxes. This is easier to maintain when the number of items changes.

Compare the alignment values visually
Change center to flex-start, flex-end, space-between, and space-evenly one at a time. Viewing the same boxes with each value is often the fastest way to understand how free space is distributed.
Remember that justify-content does not change the order of the elements. If the order is important, write the HTML in the correct reading order and use alignment only to control available space.
<!DOCTYPE html>
<html>
<head>
<style>
#main div {
width: 100px;
height: 100px;
}
#main {
width: 100%;
border: 2px solid #f2f2f2;
display: -webkit-flex; /* Safari */
-webkit-justify-content: center; /* Safari 6.1+ */
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<h1>The justify-content In Div</h1>
<p>The "justify-content: center;" aligns the flex items at the center of the container:</p>
<div id="main">
<div style="background-color:red;">Rd</div>
<div style="background-color:lightblue;">Lightblue</div>
<div style="background-color:green;">Green</div>
<div style="background-color:yellow;">Yellow</div>
</div>
<p><b>Note:</b> Internet Explorer 10 and earlier versions do not support the justify-content property.</p>
<p><b>Note:</b> Safari 6.1 (and newer) supports an alternative, the -webkit-justify-content property.</p>
</body>
</html>
Must Read These:
- How To Create a Snackbar / Toast Bootstrap
- How to use a google font on your website
- Types of CSS Selectors
How CSS justify-content Aligns Flex Items
Main axis and cross axis
CSS justify-content works on the main axis of a flex container. With the default row direction, that axis is horizontal. When flex-direction is column, it becomes vertical. The parent must be a flex or grid container; placing the declaration on a child will not move the child.
.container {
display: flex;
justify-content: center;
gap: 16px;
}
Centering a group of div elements
The center value groups the direct children in the middle of the available space. The children keep their own sizes, so the result is different from giving every child a percentage width.
<div class="boxes">
<div class="box red">Red</div>
<div class="box blue">Blue</div>
<div class="box green">Green</div>
</div>
.boxes {
display: flex;
justify-content: center;
gap: 16px;
padding: 16px;
border: 2px solid #ddd;
}
CSS justify-content Values
Start, end, and center
flex-start places items at the beginning of the main axis, while flex-end places them at the opposite edge. center places the group in the middle. The logical start and end values are useful when a layout must respect writing direction.
- flex-start: beginning of the flex direction.
- flex-end: end of the flex direction.
- center: equal remaining space around the group.
- start and end: logical writing-direction edges.
Space distribution
space-between places the first item at the beginning and the last item at the end, with remaining space between them. space-around adds space around each item. space-evenly makes the spaces between the container edges and items equal.
.navigation {
display: flex;
justify-content: space-between;
align-items: center;
}
These values need free space. If the children already fill the row, the visual result may look like start alignment because there is no unused space to distribute.
Flex Direction and Wrapped Layouts
Rows and columns
The default flex direction is row, so justify-content normally controls horizontal placement. With column, the same declaration controls vertical placement.
.panel {
min-height: 300px;
display: flex;
flex-direction: column;
justify-content: center;
}
Multiple lines
When flex-wrap is enabled, justify-content distributes items on the main axis of each line. It does not control the space between lines. Use align-content for multiple flex lines and gap for consistent row and column spacing.
justify-contentcontrols the main axis.align-itemscontrols items within a flex line.align-contentdistributes multiple lines.gapcreates regular spacing between items.
Responsive justify-content Examples
Changing a row on small screens
A toolbar can use space-between on a wide screen and become a vertical stack on a narrow screen. This is usually more reliable than forcing fixed widths.
.actions {
display: flex;
justify-content: space-between;
gap: 12px;
}
@media (max-width: 600px) {
.actions {
flex-direction: column;
justify-content: center;
}
.actions button {
width: 100%;
}
}
Why alignment appears not to work
Check that the parent has display:flex or display:grid. Then check the computed width, child widths, flex direction, wrapping, and possible overriding selectors. If children consume the entire container, justify-content has no free space to distribute.
Related Flexbox Properties
justify-content and align-items
Use justify-content for the main axis and align-items for the cross axis. With a row, this commonly means horizontal distribution and vertical centering. With a column, the directions switch.
gap and auto margins
Use gap for predictable spacing between neighboring items. An auto margin can push one item or group away from the others, which is useful for a header with navigation on one side and an account control on the other.
.header {
display: flex;
align-items: center;
}
.account {
margin-inline-start: auto;
}
Accessibility and Testing
Keep the document order meaningful
Visual alignment does not replace a logical HTML order. Keep headings, links, and buttons in an order that makes sense for keyboard and assistive technology users. Test focus visibility after changing spacing or direction.
Test real viewport sizes
Check wide and narrow screens, long labels, translated text, keyboard navigation, and zoom. A layout that works only with short English labels can overflow when content changes.
FAQ
How do I center a div with justify-content?
Set the parent to display:flex and use justify-content:center. Add align-items:center if the child also needs cross-axis centering.
Why does justify-content not work?
The parent may not be a flex or grid container, the declaration may be overridden, or the children may use all available space.
What is the difference between justify-content and align-items?
justify-content works on the main axis, while align-items works on the cross axis. Flex direction determines which physical direction those axes use.
Should I use gap or margins?
Use gap for regular spacing between flex items. Use margins when one item needs special positioning, such as pushing a group to the opposite side.
Conclusion
CSS justify-content is a parent-level alignment tool. Identify the main axis, confirm that free space exists, and choose a value that matches the intended relationship between the items.
Combine it with flexible sizing, gap, wrapping, media queries, and accessible focus states. Test the layout with realistic content instead of relying on one fixed screenshot.




Write a comment