What is Isolation in CSS: Explanation and Usage
isolation is a property that creates a new stacking context for an element, preventing its children from blending with elements outside it. This means the element and its descendants are visually isolated, avoiding unwanted style or effect overlaps.How It Works
Think of isolation like a glass box around an element and its children. Inside this box, styles like shadows, opacity, or blending effects stay contained and don’t mix with elements outside the box. This helps keep visual effects neat and predictable.
Normally, elements can blend or overlap in ways that cause unexpected results, especially with transparency or filters. When you set isolation: isolate;, the browser treats that element as a separate layer, so its children won’t visually interfere with anything outside.
Example
This example shows two overlapping boxes. The first uses isolation: isolate; to keep its shadow inside, while the second box’s shadow blends with the background.
body {
background: #eee;
padding: 2rem;
font-family: Arial, sans-serif;
}
.box {
width: 150px;
height: 150px;
margin-bottom: 2rem;
background: #4a90e2;
box-shadow: 0 0 20px rgba(0,0,0,0.5);
color: white;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
}
.isolated {
isolation: isolate;
}
.non-isolated {
/* no isolation here */
}
When to Use
Use isolation: isolate; when you want to keep an element’s visual effects separate from the rest of the page. This is helpful for:
- Preventing shadows or filters from blending with other elements.
- Fixing stacking order issues where child elements unexpectedly overlap outside their container.
- Creating clean layered designs where each section looks independent.
For example, if you have a card with a shadow and a transparent background, isolation keeps the shadow from mixing with elements behind the card.
Key Points
isolation: isolate;creates a new stacking context.- It prevents visual blending of child elements with outside elements.
- Helps control shadows, opacity, and filter effects.
- Useful for layered UI designs and fixing overlap issues.
Key Takeaways
isolation: isolate; to create a separate visual layer for an element and its children.