Forem

Cover image for Why display: none Can Break Your Animations
Pawar Shivam
Pawar Shivam

Posted on

Why display: none Can Break Your Animations

=> Why Your Animation Is Not Working

You write this:

.box {
  display: none;
  transition: all 0.3s ease;
}
Enter fullscreen mode Exit fullscreen mode

And expect animationโ€ฆ

๐Ÿ‘‰ But it disappears instantly.


=> The Real Problem

display: none removes the element from the DOM flow.

That means:

๐Ÿ‘‰ No rendering
๐Ÿ‘‰ No animation
๐Ÿ‘‰ No transition


=> Why Transition Fails

CSS transitions need:

  • a starting state
  • an ending state

But with display: none:

๐Ÿ‘‰ Element doesnโ€™t exist visually

So nothing animates.


=> The Correct Way

Use opacity + visibility:

.box {
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
}

.box.active {
  opacity: 1;
  visibility: visible;
}
Enter fullscreen mode Exit fullscreen mode

Now animation works smoothly.


=> Better Animation with Transform

.box {
  opacity: 0;
  transform: translateY(10px);
  transition: all 0.3s ease;
}

.box.active {
  opacity: 1;
  transform: translateY(0);
}
Enter fullscreen mode Exit fullscreen mode

=> When to Use display: none

Use it only when:

๐Ÿ‘‰ You donโ€™t need animation
๐Ÿ‘‰ You want to completely remove element


=> What Developers Often Miss

Animation is not just about transition.

It depends on how elements exist in layout.


=> Real UI Tip

For modals, dropdowns, tooltips:

๐Ÿ‘‰ Avoid display: none for animations


=> What Do You Think?

Have you ever tried animating an element and it just disappeared instantly?

Top comments (0)