Master the align-items property to control how flex or grid children are positioned along the cross axis, ensuring consistent vertical (or horizontal) alignment within a container.
What it is
In CSS Flexbox and Grid layouts, items have two axes: the main axis (defined by flex-direction) and the cross axis (perpendicular to the main axis). The align-items property determines how child elements align themselves along this cross axis. Think of it as setting the "vertical center" for a row of items, or the "horizontal center" for a column of items, depending on your layout direction.
Related terms include justify-content (which controls alignment on the main axis), align-self (which overrides align-items for individual items), and align-content (which affects multiple lines in a wrapped flex container).
Why it matters
- Visual Balance: It prevents awkward gaps at the top or bottom of containers when items have different heights.
- Responsive Design: It allows components like cards or buttons to remain centered regardless of screen size changes that affect item dimensions.
- Cleaner Code: It eliminates the need for manual margin adjustments or absolute positioning hacks to achieve vertical centering.
- Consistency: It ensures all sibling elements share the same alignment behavior without needing individual rules.
Syntax or steps
To use align-items, you must first establish a flex or grid context on the parent container using display: flex; or display: grid;. Then, apply the property with one of these common values:
stretch: Default value; items expand to fill the container's height/width.center: Items are centered along the cross axis.flex-start: Items align to the start of the cross axis (top for rows, left for columns).flex-end: Items align to the end of the cross axis (bottom for rows, right for columns).baseline: Items align their text baselines.
Example
<div class="container">
<div class="item short">Short</div>
<div class="item tall">Tall Content<br>More Lines</div>
<div class="item medium">Medium</div>
</div>
<style>
.container {
display: flex;
height: 200px;
background-color: #f0f0f0;
/* Aligns items vertically in the center */
align-items: center;
}
.item {
padding: 10px;
background-color: #3498db;
color: white;
margin: 5px;
}
.short { height: 40px; }
.tall { height: 120px; }
.medium { height: 60px; }
</style>
Explanation: The .container uses display: flex to create a row-based layout. Because the default flex-direction is row, the main axis is horizontal, making the vertical axis the cross axis. By setting align-items: center, all three .item divs—regardless of their specific heights—are vertically centered within the 200px tall container. Without this rule, they would stretch to fill the full height due to the default stretch behavior.
Common mistakes
- Forgetting the Parent Context: Applying
align-itemsto a standard block element does nothing. You must setdisplay: flexordisplay: gridon the parent. - Confusing Axes: Users often try to use
align-itemsto space items horizontally in a row. Remember:align-itemshandles the cross axis (vertical in a row); usejustify-contentfor the main axis (horizontal in a row). - Overriding with Height: If an item has a fixed
heightequal to the container,align-itemswill appear ineffective because there is no extra space to align within. - Using on Single Items: To align just one item differently, use
align-selfon the child, notalign-itemson the parent.
When to use it
| Property | Controls | Best For |
|---|---|---|
align-items | Cross Axis (All Children) | Centering a group of items vertically in a navbar or card list. |
justify-content | Main Axis (All Children) | Spacing items horizontally or pushing them to edges. |
align-self | Cross Axis (Single Child) | Pinning one button to the bottom while others stay centered. |
Practice
Guided Exercise: Create a flex container with three boxes of varying widths. Set align-items: flex-end. Observe how the bottoms of the boxes align, even if their tops differ.
Challenge: Modify the previous example so that only the middle box is aligned to the top (flex-start) while the others remain at the bottom. Hint: Use align-self on the middle box.
Quick check
Q: In a default flex row, which axis does align-items control?
A: The vertical axis (the cross axis).
Summary
The align-items property is essential for managing cross-axis alignment in Flexbox and Grid layouts. By understanding the distinction between main and cross axes, you can efficiently center, stretch, or pin content without complex positioning hacks.