July 8, 2025
O. Wolfson
Modern web design demands responsive, easy-to-maintain layouts. Flexbox (Flexible Box Layout) is a powerful CSS module that makes arranging elements in one dimension—either row (horizontal) or column (vertical)—simple and intuitive.
This article will cover the key properties of Flexbox, and how you can use them to create flexible, responsive layouts.
Let’s look at the properties every developer should know:
display: flexThis activates Flexbox on a container element.
css.container {
display: flex;
}
All direct children of .container become flex items.
justify-content — Horizontal AlignmentControls how items are distributed along the main axis (row by default).
Options include:
flex-start (default): items start at the leftflex-end: items align to the rightcenter: items are centeredspace-between: equal space between itemsspace-around: equal space around each itemspace-evenly: equal space between and at endscss.container {
justify-content: space-between;
}
align-items — Vertical AlignmentAligns items along the cross axis (column by default).
Options:
stretch (default): items stretch to fill the containerflex-start: align to the topflex-end: align to the bottomcenter: vertically centeredbaseline: align to the text baselinecss.container {
align-items: center;
}
flex-grow and flex-shrink — Flexibility & ResponsivenessControl how individual flex items grow or shrink to fill space.
flex-grow: How much an item grows compared to others (0 by default).flex-shrink: How much an item shrinks if needed (1 by default).Example: Make the first item grow to take up extra space:
css.item:first-child {
flex-grow: 1;
}
To prevent an item from shrinking:
css.item.no-shrink {
flex-shrink: 0;
}
html<nav class="navbar">
<div class="logo">Logo</div>
<div class="nav-links">Links</div>
<div class="profile">Profile</div>
</nav>
css.navbar {
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
flex-shrink: 0;
}
.nav-links {
flex-grow: 1;
text-align: center;
}
.profile {
flex-shrink: 0;
}
This creates a navbar where the logo and profile stay at the edges, and the links expand in the center.
Note: For complex, two-dimensional layouts, CSS Grid is usually a better fit.
Flexbox is an essential tool for modern web layouts, letting you build responsive, flexible, and well-aligned components with minimal code. Mastering these few properties will unlock a new level of control and productivity in your CSS workflow.
Further Reading: