CSS
Distributing Navigation Items Evenly with Flexbox
Learn to create responsive navigation menus where items are evenly spaced across the available width, using Flexbox `justify-content` property for clean layouts.
.nav-menu {
display: flex;
justify-content: space-around; /* Distributes items with space around them */
list-style: none;
padding: 0;
margin: 0;
background-color: #0056b3;
}
.nav-menu li a {
display: block;
padding: 15px 20px;
color: white;
text-decoration: none;
white-space: nowrap;
}
/* For 'space-between' variation */
.nav-menu.space-between {
justify-content: space-between; /* Distributes items with space between them */
}
How it works: This snippet demonstrates how to evenly distribute navigation items within a flex container. By setting `display: flex` on the parent `ul` (`.nav-menu`), `justify-content: space-around` ensures that items have equal space on both sides. Alternatively, `justify-content: space-between` places the first item at the start, the last at the end, and distributes the remaining space equally between them, making it perfect for navigation bars.