CSS overlay Property
The CSS overlay property determines whether an element is rendered in the browser's top layer or in its normal document position.
This property is primarily used during transitions involving top-layer elements, such as modal dialogs and popovers. It allows an element to remain in the top layer until an exit transition finishes, preventing it from disappearing before its animation completes.
The overlay property is controlled by the browser. Authors cannot use it to place arbitrary elements into the top layer.
overlay Syntax
The overlay property accepts two keyword values. However, authors generally use it only within the transition-property or transition declarations.
Basic Syntax
selector {
overlay: none;
}
Keyword Values
.element {
overlay: none;
}
.element {
overlay: auto;
}
Using overlay in a Transition
dialog {
transition: opacity 0.3s, overlay 0.3s allow-discrete;
}
The allow-discrete keyword permits the discrete overlay property to participate in the transition.
CSS-Wide Keywords
.element {
overlay: initial;
}
.element {
overlay: inherit;
}
.element {
overlay: unset;
}
.element {
overlay: revert;
}
.element {
overlay: revert-layer;
}
Although these declarations are syntactically valid, the browser controls the actual overlay value for top-layer elements.
Property Values
| Value | Description |
|---|---|
none |
The element is not rendered in the top layer. |
auto |
The element is rendered in the top layer. |
initial |
Resets the property to its initial value of none, subject to browser control. |
inherit |
Requests the computed value of the parent element, although author declarations do not override browser-controlled top-layer placement. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | All elements |
| Computed value | Specified keyword |
| Animation type | Discrete |
| Author control | Browser-controlled; author declarations cannot change top-layer placement |
| Length values | Not accepted |
| Percentage values | Not accepted |
Important Distinction
The property has defined keyword values, but it is not intended for directly controlling top-layer membership through ordinary author styles.
Its practical purpose is to support transitions when the browser adds or removes an element from the top layer.
How It Works
When a modal dialog or popover enters the top layer, the browser manages its overlay value.
Normally, removing an element from the top layer happens immediately. If an exit transition is configured correctly, the browser can delay that removal until the transition completes.
Without an overlay Transition
dialog {
transition: opacity 0.3s;
}
When a modal dialog closes, it may leave the top layer before its opacity transition can finish.
With an overlay Transition
dialog {
transition: opacity 0.3s, overlay 0.3s allow-discrete;
}
Including overlay in the transition allows the browser to retain top-layer placement while the exit transition runs.
Using transition-behavior
dialog {
transition-property: opacity, overlay;
transition-duration: 0.3s;
transition-behavior: allow-discrete;
}
This is an alternative to placing allow-discrete directly in the transition shorthand.
Important Consideration
Including overlay alone does not guarantee a visible exit transition. The element's opacity, display behavior, and entry and exit styles must also be configured appropriately.
Understanding the Top Layer
The top layer is a browser-managed rendering layer that appears above ordinary document content.
Elements in the top layer are not limited by the normal stacking order of their ancestors.
Common Top-Layer Elements
- Modal dialogs opened with
showModal(). - Popovers displayed using the Popover API.
- Elements displayed in fullscreen mode.
Opening a Modal Dialog
const dialog = document.querySelector("dialog");
dialog.showModal();
The browser places the modal dialog in the top layer.
Opening a Popover
const popover = document.querySelector("[popover]");
popover.showPopover();
The browser displays the popover in the top layer.
Closing a Top-Layer Element
When a dialog closes or a popover is hidden, the browser normally removes it from the top layer. An overlay transition can defer that removal until the transition completes.
Important Consideration
Authors cannot move an ordinary element into the top layer by setting overlay: auto. Top-layer membership is established through browser-supported features and APIs.
overlay Transitions
The overlay property uses discrete animation behavior. It does not interpolate between intermediate values.
When transitioning between none and auto, the browser keeps the element in the top layer for the duration of the transition.
Discrete Transition
dialog {
transition: overlay 0.3s allow-discrete;
}
Combining Multiple Properties
dialog {
transition:
opacity 0.3s,
display 0.3s allow-discrete,
overlay 0.3s allow-discrete;
}
The display transition can keep the element rendered during its exit animation, while overlay keeps it in the top layer.
Using @starting-style
dialog {
opacity: 0;
transition:
opacity 0.3s,
display 0.3s allow-discrete,
overlay 0.3s allow-discrete;
}
dialog[open] {
opacity: 1;
}
@starting-style {
dialog[open] {
opacity: 0;
}
}
The @starting-style rule provides an initial opacity for the dialog's entry transition.
Why display Is Included
A closed dialog normally uses display: none. Transitioning display with allow-discrete can keep the dialog rendered while it fades out.
Important Consideration
For entry and exit transitions, the browser switches display between none and a visible value at the beginning or end of the transition so that content remains visible during the animation.
Dialog Transitions
Modal dialogs are a common use case for the overlay property because they are displayed in the top layer.
HTML Example
<button type="button" id="open-dialog">Open Dialog</button>
<dialog id="example-dialog">
<h2>Example Dialog</h2>
<p>This dialog uses an entry and exit transition.</p>
<button type="button" id="close-dialog">Close Dialog</button>
</dialog>
CSS Example
#example-dialog {
opacity: 0;
transform: translateY(20px);
transition:
opacity 0.3s,
transform 0.3s,
display 0.3s allow-discrete,
overlay 0.3s allow-discrete;
}
#example-dialog[open] {
opacity: 1;
transform: translateY(0);
}
@starting-style {
#example-dialog[open] {
opacity: 0;
transform: translateY(20px);
}
}
JavaScript Example
const dialog = document.querySelector("#example-dialog");
document.querySelector("#open-dialog").addEventListener("click", () => {
dialog.showModal();
});
document.querySelector("#close-dialog").addEventListener("click", () => {
dialog.close();
});
How It Works
Opening the dialog triggers its entry transition. Closing it changes the dialog's rendering state, allowing the opacity and transform transitions to run while display and overlay preserve the necessary rendering conditions.
Important Consideration
Use the native <dialog> element and showModal() when modal behavior is required. Styling alone does not provide modal focus management or the same accessibility behavior.
Popover Transitions
Popovers can also use overlay transitions to remain in the top layer while closing.
HTML Example
<button type="button" popovertarget="example-popover">Toggle Popover</button>
<div id="example-popover" popover>
<p>This popover fades in and out.</p>
</div>
CSS Example
#example-popover {
opacity: 0;
transform: translateY(10px);
transition:
opacity 0.3s,
transform 0.3s,
display 0.3s allow-discrete,
overlay 0.3s allow-discrete;
}
#example-popover:popover-open {
opacity: 1;
transform: translateY(0);
}
@starting-style {
#example-popover:popover-open {
opacity: 0;
transform: translateY(10px);
}
}
How It Works
The :popover-open pseudo-class matches the popover while it is displayed. When the popover closes, the browser can keep it rendered and in the top layer until the exit transition completes.
Important Consideration
The Popover API manages the popover's top-layer placement. The overlay transition does not replace the popover attribute or the API.
overlay vs. z-index
The overlay and z-index properties affect different aspects of element rendering.
| Feature | overlay | z-index |
|---|---|---|
| Primary purpose | Represents browser-controlled top-layer placement | Controls stacking order within applicable stacking contexts |
| Author-controlled placement | No | Yes |
| Can place an ordinary element in the top layer | No | No |
| Useful in transitions | Yes, for top-layer entry and exit | Yes, for stacking changes |
Using z-index
.panel {
position: absolute;
z-index: 1000;
}
The panel participates in normal document stacking rules. A high z-index does not automatically place it above every other element.
Using the Top Layer
document.querySelector("dialog").showModal();
The browser places the modal dialog in the top layer, above ordinary document stacking contexts.
Important Consideration
Use z-index for ordinary stacking requirements. Use native top-layer features when a dialog, popover, or fullscreen element is needed.
overlay Example
This example demonstrates an animated modal dialog. The overlay transition keeps the dialog in the top layer while its exit animation completes.
HTML Example
<button type="button" id="demo-open">Open Animated Dialog</button>
<dialog id="demo-dialog">
<h3>Animated Dialog</h3>
<p>The dialog fades and slides when opened or closed.</p>
<button type="button" id="demo-close">Close Dialog</button>
</dialog>
CSS Example
#demo-dialog {
width: min(320px, calc(100% - 32px));
padding: 24px;
border: 2px solid #64748b;
border-radius: 8px;
background-color: #fff;
color: #334155;
opacity: 0;
transform: translateY(20px);
transition:
opacity 0.3s ease,
transform 0.3s ease,
display 0.3s allow-discrete,
overlay 0.3s allow-discrete;
}
#demo-dialog[open] {
opacity: 1;
transform: translateY(0);
}
#demo-dialog::backdrop {
background-color: rgb(15 23 42 / 50%);
}
@starting-style {
#demo-dialog[open] {
opacity: 0;
transform: translateY(20px);
}
}
JavaScript Example
const demoDialog = document.querySelector("#demo-dialog");
document.querySelector("#demo-open").addEventListener("click", () => {
demoDialog.showModal();
});
document.querySelector("#demo-close").addEventListener("click", () => {
demoDialog.close();
});
Click Open Animated Dialog to display the modal. Click Close Dialog to trigger its exit transition. In supporting browsers, the dialog remains in the top layer until the transition finishes.
Popover Example
The same technique can animate a popover without requiring JavaScript.
<button type="button" popovertarget="info-popover">Show Information</button>
<div id="info-popover" popover>
<p>This popover uses an overlay transition.</p>
</div>
#info-popover {
opacity: 0;
transform: scale(0.95);
transition:
opacity 0.25s,
transform 0.25s,
display 0.25s allow-discrete,
overlay 0.25s allow-discrete;
}
#info-popover:popover-open {
opacity: 1;
transform: scale(1);
}
@starting-style {
#info-popover:popover-open {
opacity: 0;
transform: scale(0.95);
}
}
This popover uses an overlay transition.
Click Show Information to toggle the popover. The popover fades and scales while entering or leaving the top layer in supporting browsers.
Accessibility Considerations
Transitions involving top-layer elements should preserve accessibility and avoid interfering with keyboard navigation.
Use Native Dialog and Popover Features
Use native HTML features rather than attempting to recreate top-layer behavior through positioning and stacking rules.
Respect Reduced Motion Preferences
@media (prefers-reduced-motion: reduce) {
dialog {
transition: none;
}
}
Users who prefer reduced motion should not be required to experience unnecessary entrance or exit animations.
Maintain Keyboard Access
Dialog controls and popover content should remain accessible through keyboard navigation while the element is active.
Avoid Excessive Animation
Keep transitions brief and predictable. Long exit transitions can delay visual removal of an element.
Important Consideration
The overlay property affects rendering during transitions. It does not provide focus management, accessible names, or other accessibility behavior by itself.
Common Mistakes
Trying to Force an Element into the Top Layer
.ordinary-element {
overlay: auto;
}
Author styles cannot use overlay: auto to place arbitrary elements into the browser's top layer.
Forgetting allow-discrete
dialog {
transition: overlay 0.3s;
}
The overlay property is discrete. Use allow-discrete to permit its transition.
Omitting display from Exit Transitions
Closed dialogs and hidden popovers may stop rendering immediately unless display is also included in the transition.
Forgetting @starting-style
Without appropriate starting styles, entry transitions may not animate when an element first becomes rendered.
Confusing overlay with z-index
The z-index property controls stacking within ordinary stacking contexts. It does not create top-layer membership.
Expecting overlay to Animate Visually
The overlay property does not create a fade, slide, or scale effect. Animate properties such as opacity or transform for visible movement.
Ignoring Browser Compatibility
Older browsers may not support transitions involving overlay, display, or @starting-style.
Important Considerations
Keep the following points in mind when using overlay:
- The property represents whether an element is rendered in the top layer.
- Its initial value is
none. - The property is not inherited.
- It accepts
noneandauto. - The browser controls top-layer placement.
- Author declarations cannot force an ordinary element into the top layer.
- The property is primarily useful in CSS transitions.
- Its animation type is discrete.
- Use
allow-discreteto enable discrete transitions. - Transitions can delay removal from the top layer.
- Modal dialogs and popovers are common use cases.
- The
displayproperty often needs to participate in the same transition. @starting-stylecan establish entry transition styles.- Animate
opacityortransformfor visible effects. - The property does not replace native dialog or popover APIs.
- It is different from
z-index. - Top-layer elements appear above ordinary stacking contexts.
- Reduced-motion preferences should be respected.
- Browser support for related transition features should be checked.
- Test entry and exit behavior independently.
Browser Support
The CSS overlay property has limited availability compared with long-established CSS layout properties. It is supported in modern browsers that implement transitions for top-layer elements, but compatibility varies across browser versions.
Successful animated entry and exit behavior also depends on support for transition-behavior: allow-discrete, transitions involving display, and @starting-style.
Progressive Enhancement
dialog {
opacity: 0;
transition: opacity 0.3s;
}
dialog[open] {
opacity: 1;
}
@supports (transition-behavior: allow-discrete) {
dialog {
transition:
opacity 0.3s,
display 0.3s allow-discrete,
overlay 0.3s allow-discrete;
}
}
Browsers without support for discrete transitions can still open and close the dialog using its native functionality, even if the full exit animation is unavailable.
For current compatibility information, visit Can I Use? and search for overlay.
Specifications
The overlay property is defined in CSS Positioned Layout Module Level 4.
CSS Positioned Layout Module Level 4: overlay
The specification describes the property and its relationship to the browser-managed top layer.
For additional documentation and examples, consult MDN Web Docs.
