🎚️ align-items β€” Vertical Alignment in CSS Grid

🌐 What is align-items?

align-items is a CSS Grid (and Flexbox) property that controls thevertical alignment of grid items *within their grid cells*. It sets the default vertical alignment for **all items** unless overridden byalign-self on individual items.

>>β€œalign-items tells every grid item how to align vertically inside its cell.”

πŸ“Œ Where does align-items apply?

It applies to the **grid container**, not to grid items directly. Only works when the container has display: grid or inline-grid.

🎯 Available Values

ValueDescription
startAligns items to the top of their cell
centerCenters items vertically
endAligns items to the bottom
stretchStretches items to fill cell height (default)

πŸ§ͺ Basic Example

align-items: center

.container {
  display: grid;
  grid-template-rows: repeat(3, 100px);
  align-items: center;
}

All grid items will be vertically centered inside their cells.

πŸ“ Visual Alignment Example

Media content

🧱 Example Using All Values

align-items: start | center | end | stretch

.start   { align-items: start; }
.center  { align-items: center; }
.end     { align-items: end; }
.stretch { align-items: stretch; }

🧠 align-items vs align-content

PropertyAligns What?
align-itemsItems *inside each* grid cell (vertical)
align-contentThe **entire grid** inside the container (vertical)

Note

⚑ Tip:
align-items = item alignment
align-content = grid alignment

πŸ“¦ Override align-items Using align-self

If you want individual item-level control, use align-self.

Override Example

.container {
  display: grid;
  align-items: center; /* default for all items */
}

.item1 {
  align-self: start;
}

.item2 {
  align-self: end;
}

🧩 Real-World Use Cases

  • Vertically centering icons inside buttons or cards
  • Aligning form labels or inputs consistently
  • Creating visually balanced dashboard widgets
  • Ensuring card content stays aligned regardless of text length

πŸ“± Responsive Behavior

align-items adapts naturally across breakpoints. For custom behavior, override it in media queries:

Responsive align-items

.container {
  align-items: stretch;
}

@media (min-width: 768px) {
  .container {
    align-items: center;
  }
}

⚠️ Common Mistakes

  • Using align-items to try aligning the whole grid (use align-content instead)
  • Expecting vertical alignment when row height is fixed and equal to item height
  • Setting align-items on grid items instead of the container

Note

🧠 Reminder:justify-items = horizontal alignmentalign-items = vertical alignment

πŸ”₯ Summary

align-items sets the vertical alignment of all grid items inside their respective cells. By mastering this property, you gain complete control over internal item positioning β€” making UI grids clean, balanced, and visually polished.

>>β€œGreat grid design isn’t just layout β€” it’s alignment. align-items brings vertical harmony.”

Learn more atMDN Docs πŸ“˜