HTML & CSS Tip of the Week

Transitioning to and from display: none

Published on


Iā€™ve been making YouTube videos on CSS for over 8 years now, and one question that Iā€™ve got on a regular basis is how to transition to and from display: none.

For a long time, the answer was ā€œitā€™s complicatedā€, but now, with allow-discrete and @starting-style, itā€™s actually pretty easy!

Hereā€™s a quick example of how we can use them together for a dialog.

dialog {
  opacity: 0;
  display: none;

  transition: opacity 1s, display 1s, overlay 1s;
  transition-behavior: allow-discrete;
}

dialog[open] {
  opacity: 1;
  display: block;

  @starting-style {
    opacity: 0;
  }
}

In that code block, Iā€™m also transitioning overlay because dialogs are brought onto the top layer when they are opened, and transitioning it prevents strange z-index things popping up during the transition.

Browser support

As of the time of publishing this article, Firefox doesnā€™t support either one of them (it does support @starting-style, but not to and from display: none).

However, they can still be used as progressive enhancements.

Dive deeper

If youā€™d like to learn more about how itā€™s working, here are some great posts that include interactive examples:

Or you can watch this video where I break it down in a lot more detail.