DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
The align-items
property is related to CSS layout. It effects how elements are aligned both in Flexbox and Grid layouts.
.container {
display: flex;
align-items: flex-start;
}
Syntax
align-items: flex-start | flex-end | center | baseline | stretch
The align-items
property defines the default behavior for how items are laid out along the cross axis (perpendicular to the main axis).
Imagine a horizontal flexbox layout. That horizontal flow is the main axis, so align-items is the alignment opposite that, on the vertical axis. Bear in mind that changes when the main axis changes, and the cross axis changes with it.
You can think of align-items
as the justify-content
version for the cross-axis (perpendicular to the main-axis).
The rest of this article is fairly focused on flexbox rather than grid. The concepts are still very similar, but there are some differences. For example, in flexbox, the axises can change, where in grid they cannot. That effects things like flexbox having values like flex-start
where in grid it is just start
.
The align-items
property accepts 5 different values:
flex-start
: cross-start margin edge of the items is placed on the cross-start lineflex-end
: cross-end margin edge of the items is placed on the cross-end linecenter
: items are centered in the cross-axisbaseline
: items are aligned such as their baselines alignstretch
(default): stretch to fill the container (still respect min-width/max-width)
The following figure helps understand how flex items are layed out depending on the align-items
value.
Demo
The following demo shows how flex items are layed out depending on the align-items
value:
- The red list is set to
flex-start
- The yellow list is set to
flex-end
- The blue list is set to
center
- The green list is set to
baseline
- The pink list is set to
stretch
Browser support
Browser support for align-items
varies in its usage with flexbox and CSS Grid.
Flex layout
IE | Edge | Chrome | Firefox | Safari | Opera |
---|---|---|---|---|---|
11 | All | 21-51 1 51+ | 20+ | 7-8 1 9+ | 12.1+ |
iOS Safari | Android Chrome | Android Firefox | Android Browser | Opera Mobile |
---|---|---|---|---|
7-8.4 1 9+ | All | All | 92+ | 12.1+ |
1 Supported with prefix
-webkit-
Grid layout
IE | Edge | Firefox | Chrome | Safari | Opera |
---|---|---|---|---|---|
All | 16+ | 52+ | 57+ | 10.1+ | 44+ |
iOS Safari | Android Chrome | Android Firefox | Android Browser | Opera Mobile |
---|---|---|---|---|
10.3+ | All | All | All | 64+ |
More information
- CSS Flexible Box Layout Module Level 1
- MDN documentation
- Advanced cross-browser flexbox
- CSS-Tricks Flexbox Guide
- Using Flexbox
- Old Flexbox and new Flexbox
- How To Use CSS Grid Properties to Justify and Align Content and Items (DigitalOcean)
- Centering Things in CSS Using Flexbox (DigitalOcean)
There is an edge case bug with Firefox and align-items: baseline.
https://bugzilla.mozilla.org/show_bug.cgi?id=1036404
Thank you very much
This training was very useful for me.
Question for you, on the codepen demo ‘baseline’ case, let’s say I wanted that 3rd green box to align to the “5” instead of the 4. How would I do that?
It looks like
flex-end
is now justend
I’m pretty sure that change is made in the CSS Box Alignment Module Level 3 Working draft and isn’t a candidate recommendation yet.
IMHO, they aren’t the same.