Flexbox is a powerful layout system that makes it easy to design flexible responsive layouts without floats or positioning.
Flex Container Basics
.container {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.item {
flex: 1; /* Equal width for all items */
padding: 1rem;
}
Flex Direction and Wrap
.container {
display: flex;
flex-direction: row; /* row, column, row-reverse, column-reverse */
flex-wrap: wrap; /* wrap, nowrap, wrap-reverse */
}
Justify Content and Align Items
.container {
/* Main axis alignment */
justify-content: flex-start; /* flex-end, center, space-between, space-around */
/* Cross axis alignment */
align-items: stretch; /* flex-start, flex-end, center, baseline */
}
Responsive Navigation
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
.nav-links {
display: flex;
gap: 2rem;
}
@media (max-width: 768px) {
.navbar {
flex-direction: column;
}
}
Centering Content
.center-box {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
Flexbox simplifies complex layouts. Practice these patterns for responsive design!