CSS
Create a Responsive Flexbox Navigation Bar
Design a flexible and responsive navigation bar using CSS Flexbox, incorporating `justify-content: space-between` and `flex-wrap: wrap` for optimal layout across devices.
.navbar {
display: flex;
justify-content: space-between; /* Pushes items to ends */
align-items: center; /* Vertically center items */
flex-wrap: wrap; /* Allow items to wrap to next line */
background-color: #333;
padding: 0.5rem 1rem;
color: white;
}
.navbar .logo {
font-size: 1.5rem;
font-weight: bold;
margin-right: 1rem; /* Space after logo */
}
.navbar ul {
display: flex;
list-style: none;
margin: 0;
padding: 0;
}
.navbar ul li {
margin-left: 1.5rem;
}
.navbar ul li a {
color: white;
text-decoration: none;
padding: 0.5rem 0;
display: block;
}
How it works: This snippet builds a responsive navigation bar using Flexbox. The `.navbar` is set to `display: flex`, with `justify-content: space-between` to push the logo and navigation links to opposite ends. `align-items: center` vertically centers the items. Crucially, `flex-wrap: wrap` allows the items to wrap onto the next line if the viewport becomes too narrow, ensuring responsiveness.