0
0
CSSmarkup~8 mins

Align content in CSS - Performance & Optimization

Choose your learning style9 modes available
Performance: Align content
MEDIUM IMPACT
Align content affects how extra space is distributed in a container with multiple lines, impacting layout calculation and paint.
Aligning multiple lines of content inside a flex or grid container
CSS
.container {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  height: 500px;
}
.item {
  flex: 1 0 100px;
}
Aligning content to flex-start avoids stretching and reduces layout recalculations.
📈 Performance Gainsingle reflow regardless of line count
Aligning multiple lines of content inside a flex or grid container
CSS
.container {
  display: flex;
  flex-wrap: wrap;
  align-content: stretch;
  height: 500px;
}
.item {
  flex: 1 0 100px;
}
Using align-content: stretch causes the browser to recalculate layout for each line to stretch items, triggering multiple reflows.
📉 Performance Costtriggers multiple reflows proportional to number of lines
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
align-content: stretchmedium (multiple lines)multiple reflowsmedium[X] Bad
align-content: flex-startmedium (multiple lines)single reflowlow[OK] Good
Rendering Pipeline
Align-content affects the Layout stage by determining how extra space is distributed among wrapped lines, which influences Paint and Composite stages.
Layout
Paint
Composite
⚠️ BottleneckLayout
Core Web Vital Affected
CLS
Align content affects how extra space is distributed in a container with multiple lines, impacting layout calculation and paint.
Optimization Tips
1Avoid align-content: stretch on containers with many wrapped lines to reduce reflows.
2Prefer align-content values like flex-start or center for better layout performance.
3Use browser DevTools Performance panel to monitor layout events when adjusting align-content.
Performance Quiz - 3 Questions
Test your performance knowledge
Which align-content value generally causes fewer layout recalculations in a multi-line flex container?
Astretch
Bflex-start
Cspace-between
Dspace-around
DevTools: Performance
How to check: Record a performance profile while resizing or changing align-content. Look for Layout events and their duration.
What to look for: High number or long duration of Layout events indicates costly reflows caused by align-content usage.