Performance: Align items
Align items affects the layout and paint stages by controlling how flex or grid children align along the cross axis, impacting rendering speed and visual stability.
display: flex; height: 200px; align-items: center;
display: flex; height: 200px; /* No align-items set, using margin-top on children */ .child { margin-top: 50px; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using margin-top on each child for alignment | Multiple style changes on children | Multiple reflows per child | Higher paint cost due to layout thrashing | [X] Bad |
| Using align-items: center on flex container | Single style change on container | Single reflow for alignment | Lower paint cost with stable layout | [OK] Good |
align-items do in a flex container?align-itemsalign-items property is used inside flex or grid containers to control how items align vertically.align-items?align-items valuescenter, flex-start, flex-end, and stretch. center centers items vertically.align-items.div.container {
display: flex;
height: 200px;
align-items: flex-end;
}
<div class="container"> <div>Item 1</div> <div>Item 2</div> </div>
align-items: flex-end;div.container {
display: flex;
align-items: center
height: 150px;
}align-items: center is missing a semicolon.display: flex; is correct, height is valid, and align-items works without justify-content.div.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
align-items: stretch;
}
div.grid > div.special {
/* What to add here? */
}align-items: stretch; to stretch all items vertically by default.align-self. The correct value for top alignment is start, not flex-start (which is for flexbox).Pass the test and this pattern moves unknown