Where we learn to give our flex items the personal space they deserve!
TrashyMcTweak
CodyMcTweak
FattyMcTweak
AllyMcTweak
GrumpyMcTweak
AshleyMcTweak
GarbageMcTweak
SnowzieMcTweak
Everyone
The justify-content property aligns flex items along the main axis of the flex container. It helps control the spacing between items when they don't use all available space in the main axis.
Think of it as deciding how to distribute extra space when flex items don't fill the container completely.
.navigation { display: flex; justify-content: space-between; /* This is where the magic happens */ }
Key Point: The main axis depends on your flex-direction. If it's row (default), justify-content works horizontally. If it's column, justify-content works vertically.
Items are packed toward the start of the flex container. This is the default value.
justify-content: flex-start;
Items are packed toward the end of the flex container.
justify-content: flex-end;
Items are centered along the line.
justify-content: center;
Items are evenly distributed. First item at the start, last item at the end.
justify-content: space-between;
Items are evenly distributed with equal space around them. Note that visually the spaces aren't equal, since all items have equal space on both sides.
justify-content: space-around;
Items are distributed so that the spacing between any two items (and the space to the edges) is equal.
justify-content: space-evenly;
Select a justify-content value to see how it affects the navigation items:
.navigation { display: flex; justify-content: flex-start; }
Let's create a responsive navigation menu with evenly spaced items using justify-content.
Create a navigation bar with these requirements:
<nav class="navigation-container"> <div class="nav-item">Home</div> <div class="nav-item">Products</div> <div class="nav-item">Services</div> <div class="nav-item">About</div> <div class="nav-item">Contact</div> </nav>
Select the justify-content value for different screen sizes:
.navigation-container { display: flex; flex-wrap: wrap; justify-content: center; /* For mobile */ } /* For desktop */ @media (min-width: 640px) { .navigation-container { justify-content: space-between; } }
Pro Tip: Combine justify-content with align-items to control spacing in both directions, which we'll explore in our next lesson!