Seedling
Last updated:
Flexbox Safe and Unsafe Values: A Complete Guide
Understanding safe and unsafe values in CSS flexbox properties and their impact on layout behavior.
This is a Seedling
The idea is here, the rest of the words aren't yet. Check back soon, or don't, and catch it when it's done.
Understanding Safe vs Unsafe Values
In CSS flexbox, certain properties accept “safe” and “unsafe” keywords that control how the browser handles content that might overflow or cause layout issues.
The align-self Property
The align-self property controls how a flex item aligns along the cross axis and supports safe/unsafe variants.
Safe Values
safe center- Centers the item but prevents it from overflowing the containersafe start- Aligns to the start but prevents overflowsafe end- Aligns to the end but prevents overflowsafe flex-start- Aligns to flex-start but prevents overflowsafe flex-end- Aligns to flex-end but prevents overflow
Unsafe Values
unsafe center- Centers the item even if it causes overflowunsafe start- Aligns to start even if it causes overflowunsafe end- Aligns to end even if it causes overflowunsafe flex-start- Aligns to flex-start even if it causes overflowunsafe flex-end- Aligns to flex-end even if it causes overflow
The justify-self Property
Similar to align-self, justify-self also supports safe/unsafe variants for controlling alignment along the main axis.
Safe Values
safe center- Centers without causing overflowsafe start- Aligns to start without overflowsafe end- Aligns to end without overflowsafe left- Aligns to left without overflowsafe right- Aligns to right without overflow
Unsafe Values
unsafe center- Centers even if it causes overflowunsafe start- Aligns to start even if it causes overflowunsafe end- Aligns to end even if it causes overflowunsafe left- Aligns to left even if it causes overflowunsafe right- Aligns to right even if it causes overflow
Practical Examples
Safe Alignment
.flex-item {
align-self: safe center;
justify-self: safe start;
}
Unsafe Alignment
.flex-item {
align-self: unsafe center;
justify-self: unsafe end;
}
When to Use Safe vs Unsafe
Use Safe When:
- Content might be dynamic or user-generated
- You want to prevent layout breaking
- Accessibility is a priority
- Working with responsive designs
- Content length is unpredictable
Use Unsafe When:
- You have complete control over content
- You want precise positioning
- Content is guaranteed to fit
- You need specific visual alignment
- Working with fixed-size content
Browser Support
Safe and unsafe values are supported in modern browsers, but it’s important to test across different browsers and devices to ensure consistent behavior.
Common Gotchas
- Safe values might cause unexpected centering when content is smaller than expected
- Unsafe values can cause content to overflow and break layouts
- The behavior can vary between different flexbox implementations
- Always test with various content lengths and container sizes
Best Practices
- Default to safe for most use cases
- Test thoroughly with different content lengths
- Use unsafe sparingly and only when you have control over content
- Consider fallbacks for older browsers
- Document your choices for team members