0

I have elements with 2 classes (fade and hidden)

<div id="example" class="fade hidden"></div>

.fade {
    opacity: 0;
    -webkit-transition: opacity .15s linear;
    -o-transition: opacity .15s linear;
    transition: opacity .15s linear;
}

.hidden {display: none;}

When clicked, a class gets added (.in and .shown)

.fade.in {
    opacity: 1;
}

.shown {display: block !important;}

My issue is, by doing the display: block, there is no animation with opacity. Just the element showing in full.

Anyway to keep the animation fade with opacity?

user1702965
  • 379
  • 5
  • 16

1 Answers1

1

Instead of using display, one could use position: absolute. See the following example:

.fade {
  opacity: 0;
  transition: opacity 1s linear;
}

.hidden {
  position: absolute;
  pointer-events: none;
}

.fade.in {
  opacity: 1;
}
<button onclick="document.getElementById('example').className = 'fade in';">Click</button>
<div id="example" class="fade hidden">Example</div>
<p>Text...</p>
Anonymous
  • 902
  • 10
  • 23