CSS
Responsive Flexbox Navbar with Space Distribution
Craft a responsive navigation bar using CSS Flexbox, distributing items evenly and vertically centering them for a clean, modern look. Adapts to smaller screens.
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15px 20px;
background-color: #333;
color: white;
}
.navbar-logo a {
color: white;
text-decoration: none;
font-size: 1.5em;
}
.navbar-links {
list-style: none;
margin: 0;
padding: 0;
display: flex;
gap: 20px;
}
.navbar-links a {
color: white;
text-decoration: none;
padding: 5px 10px;
transition: background-color 0.3s ease;
}
.navbar-links a:hover {
background-color: #555;
border-radius: 4px;
}
@media (max-width: 768px) {
.navbar {
flex-direction: column;
align-items: flex-start;
}
.navbar-links {
flex-direction: column;
width: 100%;
margin-top: 10px;
gap: 5px;
}
.navbar-links a {
width: 100%;
text-align: center;
padding: 10px 0;
}
}
How it works: This CSS snippet demonstrates how to create a responsive navigation bar using Flexbox. The `.navbar` container uses `display: flex`, `justify-content: space-between` to push the logo and links to opposite ends, and `align-items: center` for vertical alignment. A media query is included to stack items vertically on smaller screens, making the navigation adaptable to various devices.