The align-items property
align-items is set on a flex or grid container and aligns all its items along the cross axis: it stretches them to full height, centers them or packs them at the start or end.
Syntax
align-items: normal | stretch | flex-start | center | flex-end | baseline; - Initial value
normal- Inherited
- no
- Applies to
- all elements; the effect is visible in flex and grid containers
Values
| Value | What it does |
|---|---|
normal | the initial value; in flex and grid containers it behaves as stretch |
stretch | items stretch to the full height of the line (unless they have a set height) |
flex-start | items at the start of the cross axis — the top for a row |
center | in the center of the cross axis |
flex-end | at the end of the cross axis — the bottom for a row |
baseline | along the baseline of the first line of text — handy when font sizes differ |
Example
.row {
display: flex;
gap: 8px;
height: 90px;
padding: 8px;
margin-bottom: 8px;
background: #f1f5f9;
}
.row span {
padding: 6px 10px;
background: #0284c7;
color: #fff;
}
.row span:nth-child(2) { font-size: 24px; }
.stretch { align-items: stretch; }
.center { align-items: center; }
.end { align-items: flex-end; }
.baseline { align-items: baseline; } <p>stretch</p><div class="row stretch"><span>1</span><span>2</span><span>3</span></div>
<p>center</p><div class="row center"><span>1</span><span>2</span><span>3</span></div>
<p>flex-end</p><div class="row end"><span>1</span><span>2</span><span>3</span></div>
<p>baseline</p><div class="row baseline"><span>1</span><span>2</span><span>3</span></div> Result
Good to know
- The cross axis is perpendicular to the main axis: with flex-direction: row it is vertical, with column it is horizontal.
- stretch does not stretch an item that has a height set (or a width, in a column).
- To center on both axes: justify-content: center and align-items: center on the container.
- A single item can be aligned differently with align-self; in a grid container align-items aligns items inside their cells.
Sources and specification
- MDN: the align-items property — MDN Web Docs
- CSS Box Alignment Module Level 3 (W3C) — W3C
See also
The justify-content property
CSS justify-content distributes space between items along the main axis in flexbox and grid: flex-start, center, space-between, space-evenly.
The flex property
The CSS flex property is a shorthand for flex-grow, flex-shrink and flex-basis: what flex: 1, flex: auto and flex: none mean and how items share space.
The display property
The CSS display property sets how an element takes part in layout: as a block, inline with text, as a flex or grid container, or not at all. Values, examples.
CSS Flexbox Playground
Change flex-direction, justify-content, align-items, flex-wrap and gap and see how flexbox works right away. Copy the ready CSS code in one click.
CSS Flexbox for beginners
A beginner's guide to CSS Flexbox: display: flex, axes, justify-content, align-items, flex-wrap, gap and flex: 1, with examples of a menu, cards and centering.
Center an element horizontally and vertically
The simplest modern way to center a block using flexbox.