π Introduction
The align-content property in Flexbox controls the vertical spacing between rows (or horizontal spacing in column-direction layouts) when there is **extra space along the cross-axis**. It only works when:1) flex-wrap is enabled and 2) multiple rows or columns exist. This makes it different from align-items, which aligns items within a single row. π―
π― What align-content Controls
- Controls spacing between flex lines (rows or columns)
- Works only when flex items wrap
- Affects all lines as a group
- Does NOT align individual items β use
align-itemsfor that
β¨ Syntax
Syntax
container {
align-content: stretch | flex-start | flex-end | center | space-between | space-around | space-evenly;
}Note
align-content has no effect when the flex container has a single line. Set flex-wrap: wrap; to see changes.π§© Available Values
1. stretch (default)
Flex lines stretch to fill the container.
Code Snippet
align-content: stretch;2. flex-start
Pack all rows toward the start of the cross-axis.
Code Snippet
align-content: flex-start;3. flex-end
Pack rows toward the end of the cross-axis.
Code Snippet
align-content: flex-end;4. center
Center all rows in the containerβs cross-axis.
Code Snippet
align-content: center;5. space-between
Equal space *between* each row; no space at edges.
Code Snippet
align-content: space-between;6. space-around
Equal space around each row; outer rows get half spacing.
Code Snippet
align-content: space-around;7. space-evenly
Equal spacing between rows and edges.
Code Snippet
align-content: space-evenly;π₯ Practical Example
align-content Example
.container {
display: flex;
flex-wrap: wrap;
height: 300px;
gap: 10px;
align-content: space-between;
}
.item {
width: 100px;
height: 50px;
background: #60a5fa;
}π Visual Meaning of align-content
| Value | Effect on Flex Lines |
|---|---|
| stretch | Rows expand to fill space |
| flex-start | Rows move to top/left |
| flex-end | Rows move to bottom/right |
| center | Rows centered in container |
| space-between | Equal space between rows |
| space-around | Space on both sides of rows |
| space-evenly | Equal spacing everywhere |
π§ Common Mistakes
- β Using align-content without
flex-wrap: wrap - β Expecting align-content to align items (use align-items)
- β Using align-content with only one row (no effect)
π Conclusion
The align-content property gives you control over how multiple flex rows or columns are spaced inside a containerβs cross-axis. When combined with flex-wrap and gap, it becomes a powerful layout tool for responsive UI grids, galleries, cards, and flexible content blocks. π