GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

`overlay` CSS property

The overlay CSS property specifies whether an element appearing in the top layer (for example, a shown popover or modal dialog element) is actually rendered in the top layer. This property is only relevant within a list of transition-property values, and only if allow-discrete is set as the transition-behavior.

The **`overlay`** [CSS](/docs/Web/CSS) property specifies whether an element appearing in the [top layer](/docs/Glossary/Top_layer) (for example, a shown [popover](/docs/Web/API/Popover_API) or modal dialog element) is actually rendered in the top layer. This property is only relevant within a list of transition-property values, and only if `allow-discrete` is set as the transition-behavior.

It is important to note that `overlay` can _only_ be set by the browser — author styles cannot change the `overlay` value of any element. You can, however, add `overlay` to the [list of transition properties](/docs/Web/CSS/Reference/Properties/transition-property) set on an element. This causes its removal from the top layer to be deferred so it can be animated instead of disappearing immediately.

> [!NOTE]
> When transitioning `overlay`, you need to set [`transition-behavior: allow-discrete`](/docs/Web/CSS/Reference/Properties/transition-behavior) on the transition so that it will animate. `overlay` animations differ from normal [discrete animations](/docs/Web/CSS/Guides/Animations/Animatable_properties#discrete) in that the visible (i.e., `auto`) state will always be shown for the full duration of the transition, regardless of whether it is the start or end state.

## Syntax

```css
/* Keyword values */
overlay: auto;
overlay: none;

/* Global values */
overlay: inherit;
overlay: initial;
overlay: revert;
overlay: revert-layer;
overlay: unset;
```

### Values

This property is specified as one of the following keyword values:

- `auto`
- : The element is rendered in the top layer if it is promoted to the top layer.
- `none`
- : The element is not rendered in the top layer.

## Formal definition

## Formal syntax

## Examples

### Transitioning a popover

In this example, a [popover](/docs/Web/API/Popover_API) is animated as it [transitions](/docs/Web/CSS/Guides/Transitions) from hidden to shown and back again.

#### HTML

The HTML contains a div element declared as a popover using the [popover](/docs/Web/HTML/Reference/Global_attributes/popover) attribute, and a button element designated as the popover's display control using its [popovertarget](/docs/Web/HTML/Reference/Elements/button#popovertarget) attribute.

```html
<button popovertarget="mypopover">Show the popover</button>
<div popover="auto" id="mypopover">I'm a Popover! I should animate.</div>
```

#### CSS

The `overlay` property is only present in the list of transitioned properties. As `overlay` is a user-agent controlled property, it is not declared in the pre-transition or post-transition states.

```css
html {
font-family: "Helvetica", "Arial", sans-serif;
}

[popover]:popover-open {
opacity: 1;
transform: scaleX(1);
}

[popover] {
font-size: 1.2rem;
padding: 10px;

/* Final state of the exit animation */
opacity: 0;
transform: scaleX(0);

transition:
opacity 0.7s,
transform 0.7s,
overlay 0.7s allow-discrete,
display 0.7s allow-discrete;
/* Equivalent to
transition: all 0.7s allow-discrete; */
}

/* Needs to be included after the previous [popover]:popover-open
rule to take effect, as the specificity is the same */
@starting-style {
[popover]:popover-open {
opacity: 0;
transform: scaleX(0);
}
}

/* Transition for the popover's backdrop */

[popover]::backdrop {
background-color: transparent;
transition:
display 0.7s allow-discrete,
overlay 0.7s allow-discrete,
background-color 0.7s;
/* Equivalent to
transition: all 0.7s allow-discrete; */
}

[popover]:popover-open::backdrop {
background-color: rgb(0 0 0 / 25%);
}

/* Nesting selectors (&) cannot represent pseudo-elements, so this
starting-style rule cannot be nested. */

@starting-style {
[popover]:popover-open::backdrop {
background-color: transparent;
}
}
```

The two properties we want to animate are opacity and transform: we want the popover to fade in and out while growing and shrinking in the horizontal direction. We set a starting state for these properties on the default hidden state of the popover element (selected via `[popover]`), and an end state on the open state of the popover (selected via the :popover-open pseudo-class). We then set a transition property to animate between the two.

Because the animated element is being promoted to the [top layer](/docs/Glossary/Top_layer) when shown and removed from the top layer when hidden, `overlay` is added to the list of transitioned elements. This ensures that the removal of the element from the top layer is deferred until the animation has ended. This doesn't make a huge difference for basic animations such as this one, but in more complex cases not doing this can result in the element being removed from the overlay too quickly, meaning the animation is not smooth or effective. Note that the [`transition-behavior: allow-discrete`](/docs/Web/CSS/Reference/Properties/transition-behavior) value is also set in the shorthand to enable discrete transitions.

The following steps are also required to get the animation working in both directions:

- A starting state for the animation is set inside the @starting-style at-rule. This is needed to avoid unexpected behavior. By default transitions are not triggered on elements' first style updates, or when the `display` type changes from `none` to another type. `@starting-style` allows you to override that default in a specific controlled fashion. Without this, the entry animation would not occur and the popover would just appear.
- `display` is also added to the list of transitioned elements so that the animated element is visible (set to `display: block`) throughout both the entry and exit animation. Without this, the exit animation would not be visible; in effect, the popover would just disappear. Again, `transition-behavior: allow-discrete` is required in this case for the animation to occur.

You'll note that we've also included a transition on the ::backdrop that appears behind the popover when it opens, to provide a nice darkening animation. `[popover]:popover-open::backdrop` is needed to select the backdrop when the popover is open.

#### Result

The code renders as follows:

100%

> [!NOTE]
> Because popovers change from `display: none` to `display: block` each time they are shown, the popover transitions from its `@starting-style` styles to its `[popover]:popover-open` styles every time the entry transition occurs. When the popover closes, it transitions from its `[popover]:popover-open` state to the default `[popover]` state.
>
> It is possible for the style transition on entry and exit to be different in such cases. See our [Demonstration of when starting styles are used](/docs/Web/CSS/Reference/At-rules/@starting-style#demonstration_of_when_starting_styles_are_used) example for a proof of this.

## Specifications

## Browser compatibility

## See also

- [CSS transitions](/docs/Web/CSS/Guides/Transitions) module
- @starting-style
- transition-behavior
- [Four new CSS features for smooth entry and exit animations](https://developer.chrome.com/blog/entry-exit-animations/) on developer.chrome.com (2023)

source: MDN Web Docs mirror · page

Cite this

"`overlay` CSS property". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/overlay

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·