0
0
Tailwindmarkup~8 mins

Object-fit for images and media in Tailwind - Performance & Optimization

Choose your learning style9 modes available
Performance: Object-fit for images and media
MEDIUM IMPACT
This affects how images and media scale and fit within their containers, impacting layout stability and paint performance.
Fitting an image inside a fixed-size container without layout shifts
Tailwind
<img src="photo.jpg" class="w-64 h-64 object-cover" alt="Photo">
Using object-cover ensures the image fills the container without distortion or layout shifts.
📈 Performance GainPrevents layout shifts, reducing CLS and repaint costs.
Fitting an image inside a fixed-size container without layout shifts
Tailwind
<img src="photo.jpg" class="w-64 h-64" style="width:16rem; height:16rem;" alt="Photo">
Without object-fit, the image may stretch or overflow, causing layout shifts or overflow issues.
📉 Performance CostTriggers layout shifts causing CLS issues and multiple repaints.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Image without object-fit in fixed containerMinimalMultiple on load if image size differsHigh due to repaints from layout shifts[X] Bad
Image with object-fit (e.g., object-cover)MinimalSingle initial reflowLower paint cost due to stable layout[OK] Good
Rendering Pipeline
The browser calculates styles including object-fit during Style Calculation, then Layout determines the image box size. Paint renders the image respecting object-fit rules, and Composite layers the final output.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout and Paint stages are most impacted if object-fit is not used properly, causing layout shifts and repaints.
Core Web Vital Affected
CLS
This affects how images and media scale and fit within their containers, impacting layout stability and paint performance.
Optimization Tips
1Always use object-fit to control how images fill their containers to avoid layout shifts.
2Use Tailwind's object-cover or object-contain classes for consistent image scaling.
3Avoid fixed image sizes without object-fit to prevent costly reflows and repaints.
Performance Quiz - 3 Questions
Test your performance knowledge
How does using object-fit on images affect layout shifts?
AIt has no effect on layout shifts.
BIt prevents layout shifts by fitting images properly inside containers.
CIt increases layout shifts by resizing images dynamically.
DIt causes images to load slower.
DevTools: Performance
How to check: Record a page load and look for layout shifts in the experience timeline. Also check Paint events for repaints caused by image loading.
What to look for: Look for CLS score spikes and repeated layout or paint events related to images without object-fit.