Flexbox
Align and distribute elements side-by-side or in columns using CSS Flexbox.
Learning Goals
The Core Concept
Before Flexbox, aligning elements horizontally was a nightmare. Flexbox (Flexible Box Layout) makes it easy to arrange items in rows or columns. By simply setting `display: flex;` on a parent container, its direct children become 'flex items'. You can then align them horizontally with `justify-content` (e.g. centering or separating with spaces) and vertically with `align-items`.
Visual guide
Css concept flow
A simple original diagram to connect the lesson idea with real project flow.
Code & Implementation
.nav-container {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
background-color: #f8fafc;
}Expected Output
Menu logo on the far left, menu links on the far right, aligned perfectly in the vertical center.
Create a Responsive Gallery Grid
Hands-on practice task
The Challenge
Write CSS for a container .gallery that aligns its items horizontally, allows wrapping when space runs out, sets a 16px gap between pictures, and centers the items on the screen.
Helpful Hints
- •Use display: flex; on the container.
- •Set flex-wrap: wrap; and justify-content: center;.
Quick Knowledge Check
What is the difference between justify-content and align-items?
How do I make items wrap to the next line if they run out of room?
Continue Learning
Next steps after this lesson
Write CSS for a container .gallery that aligns its items horizontally, allows wrapping when space runs out, sets a 16px gap between pictures, and centers the items on the screen.
Ready to put your coding skills to the test?
Don't just read—write code! Use our free Try-Code Playground to experiment with real-time preview, or search utilities on our Developer Tools List.