HTML & CSSEasy
What is align-items?
align-items aligns items along the cross axis of a flex container (vertically in a row) or along the block axis inside each grid area. The default is stretch, which is why flex items in a row become equal height; center is the most common override.
Values
stretch(default): items fill the cross axis (unless they have an explicit height or width on that axis).flex-start/start: align to the cross-start edge.flex-end/end: align to the cross-end edge.center: center on the cross axis.baseline: align the first text baselines; great for items with different font sizes, like a big price next to a small label.last baselinealso exists.
Related properties
align-selfon one item overridesalign-itemsfor that item.align-contentaligns lines of wrapped flex items (or the whole grid), only when there is extra cross-axis space.place-items: centerin Grid sets bothalign-itemsandjustify-items, the shortest way to center.
Common gotchas
- Using
centerremoves the stretch, so cards in a row no longer have equal heights. - In a
columndirection container,align-itemscontrols horizontal alignment, andstretchmakes children full-width;centershrinks them to their content width. - Vertical centering needs the container to have a height larger than its content.
Interview tip
Explain why flex rows give equal-height columns by default: that is align-items: stretch at work.
Code example
html
1<style>2 body { font-family: system-ui; }3 .row { display: flex; gap: 6px; height: 90px; border: 2px solid #0b1020; margin: 4px 0 12px; padding: 4px; background: #f8fafc; }4 .row span { background: #2b4bff; color: white; padding: 4px 10px; border-radius: 4px; }5 .big { font-size: 28px; }6 .self { align-self: flex-end; background: #e11d48 !important; }7</style>8<code>stretch (default)</code>9<div class="row"><span>A</span><span>B<br>two lines</span><span>C</span></div>10<code>center</code>11<div class="row" style="align-items:center"><span>A</span><span>B<br>two lines</span><span>C</span></div>12<code>baseline</code>13<div class="row" style="align-items:baseline"><span class="big">$49</span><span>/month</span><span>billed yearly</span></div>14<code>flex-start + align-self: flex-end on one item</code>15<div class="row" style="align-items:flex-start"><span>A</span><span class="self">self</span><span>C</span></div>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.