Flexbox is a powerful CSS layout system for arranging items in rows or columns.
The parent element becomes a flex container:
.container {
display: flex;
flex-direction: row; /* row or column */
justify-content: center; /* Horizontal alignment */
align-items: center; /* Vertical alignment */
gap: 10px; /* Space between items */
}On container:
flex-direction: row | column | row-reverse | column-reversejustify-content: flex-start | center | flex-end | space-between | space-aroundalign-items: flex-start | center | flex-end | stretchflex-wrap: wrap | nowrapOn items:
flex: Shorthand for grow, shrink, basisflex-grow: How much to grow (0-1)flex-basis: Initial sizenav {
display: flex;
justify-content: center;
gap: 20px;
}
// ...<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>Centering an item:
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
}Two-column layout:
.container {
display: flex;
gap: 20px;
}
.sidebar { flex: 0 0 200px; } /* Fixed width */
.content { flex: 1; } /* Takes remaining space */Flexbox is a powerful CSS layout system for arranging items in rows or columns.
The parent element becomes a flex container:
.container {
display: flex;
flex-direction: row; /* row or column */
justify-content: center; /* Horizontal alignment */
align-items: center; /* Vertical alignment */
gap: 10px; /* Space between items */
}On container:
flex-direction: row | column | row-reverse | column-reversejustify-content: flex-start | center | flex-end | space-between | space-aroundalign-items: flex-start | center | flex-end | stretchflex-wrap: wrap | nowrapOn items:
flex: Shorthand for grow, shrink, basisflex-grow: How much to grow (0-1)flex-basis: Initial sizenav {
display: flex;
justify-content: center;
gap: 20px;
}
// ...<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>Centering an item:
.center {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
}Two-column layout:
.container {
display: flex;
gap: 20px;
}
.sidebar { flex: 0 0 200px; } /* Fixed width */
.content { flex: 1; } /* Takes remaining space */