How can I keep grid items aligned when one item is taller?
Quick answer
To align the contents of grid items at the top of their areas, set align-items: start on the grid container, or use align-self: start on one item. This prevents items from stretching to fill their grid areas. The row can still be as tall as its tallest item. Use align-content for positioning or distributing the grid tracks when the container has extra space, not for aligning an item inside its area. [S1]
The question, the details, the next step.
A practical checklist
Identify whether the misalignment is inside an item’s grid area or involves the tracks as a group.
Try
align-items: startwhen all grid items should align to the start of their areas.Use
align-self: startwhen only one item needs an override.Check that the taller item still has enough room and that the shorter item ends where its content ends.
Consider
align-contentonly when extra container space should affect track positioning or distribution.
A few more useful answers
Does `align-items: start` make every row the height of its shortest item?
No. It changes item alignment within grid areas; it does not mean that the row shrinks to fit the shortest item. In a shared row, the taller content can still require more row height. [S1]
Can I top-align just one grid item?
Yes. Set align-self: start on that item to override the grid container’s item alignment. [S1]
When is `align-content` the right property?
Use it when you want to position or distribute the grid tracks within the container and there is extra space. It does not align an item inside its grid area. [S2]
Sources
References used for this guide.
- [S1] Aligning items in CSS grid layout developer.mozilla.org · Checked
- [S2] Box alignment in grid layout developer.mozilla.org · Checked
