CSS Flexbox Layout for Beginners
CSS Flexbox is a layout method for arranging items in a row or column. It is useful for navigation bars, button groups and aligning content.
Make a flex container
Apply display: flex to the parent element. Its direct children become flex items.
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
}
Choose the main axis
Flexbox lays items out in a row by default. Set flex-direction: column to stack them vertically. The main axis follows this direction, while the cross axis runs perpendicular to it.
.menu {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
Align and wrap items
Use justify-content to distribute items along the main axis and align-items to align them across the cross axis. Add flex-wrap: wrap when items should move onto another line on a narrow screen.
Flexbox works best for one-dimensional layout. For a two-dimensional grid of rows and columns, explore CSS Grid.