CSS Reference ยท Flexbox
The align-items property
Reference for the CSS align-items property: stretch, center, flex-start, flex-end and baseline on the cross axis of flex and grid containers, with examples.
The align-items property sets the default alignment of all items on the cross axis of a flex container. In a row container the cross axis is vertical, so it controls how items line up top to bottom; in a column container it is horizontal.
The default behaves like stretch: items without a set height grow to fill the line. That is why flex items in a row are usually all as tall as the tallest one.
.bar { display: flex; align-items: center; }
Try it
Syntax
align-items: normal | stretch | <baseline-position> | [ <overflow-position>? <self-position> ]; /* <self-position>: center | start | end | self-start | self-end | flex-start | flex-end */ /* <baseline-position>: [ first | last ]? baseline */
- Initial value:
normal(behaves likestretchfor flex items) - Applies to: flex containers and grid containers; current browsers also apply it to block containers
- Inherited: no
- Animatable: no
- Computed value: as specified
Values
| Value | Description |
|---|---|
normal / stretch |
Items with height: auto (in a row) are stretched to fill the line. Items with a set height stay that size and sit at the start. |
flex-start |
Items sit at the cross-start edge. |
flex-end |
Items sit at the cross-end edge. |
start, end |
Like the flex versions, but follow the writing mode and also work in grid. |
self-start, self-end |
Based on the item's own writing mode rather than the container's. |
center |
Items are centered on the cross axis. |
baseline |
The first text baselines of all items line up. first baseline is the same; last baseline aligns the last lines. |
safe / unsafe |
Optional prefix: safe center avoids pushing an item off the start edge when it is bigger than the line. |
Usage notes
- Cross axis, not main axis.
justify-contentworks along the main axis. After you setflex-direction: column, the two swap roles. - Stretch only works with an
autosize. A fixedheight(in a row) stops an item from stretching. Margins set toautoon the cross axis also overridealign-items. - Override for one item with
align-self. - Wrapped containers.
align-itemsaligns an item inside its own line. Usealign-contentto place the lines inside the container. - A container needs height to show the effect. If the container is only as tall as its tallest item,
centerandflex-endseem to do nothing for that item. - Baseline for text. Use
baselinewhen items have different font sizes and their text should read as one line (a big number beside a small label).
Browser support
Supported in every browser for flexbox and grid. Applying it to plain block containers is available in current browsers only.