html – CSS list fades in with unknown value

Question:

I was trying to do a gradual fadein using plain CSS .
The lines in the list should appear one after the other. The solution should be without jquery

I know how to do this for a limited number of rows. But, how do I code the css so that no matter how many rows I have in the list, it would work.

Here is what I did:

.ladder {
  opacity: 0;
  -webkit-animation: fadeIn 0.9s 1;
  animation: fadeIn 0.9s 1;
  -webkit-animation-fill-mode: forwards;
  animation-fill-mode: forwards;
}

.ladder:nth-child(5n+1) {
  -webkit-animation-delay: 0.2s;
  animation-delay: 0.2s;
}

.ladder:nth-child(5n+2) {
  -webkit-animation-delay: 0.4s;
  animation-delay: 0.4s;
}

.ladder:nth-child(5n+3) {
  -webkit-animation-delay: 0.6s;
  animation-delay: 0.6s;
}

.ladder:nth-child(5n+4) {
  -webkit-animation-delay: 0.8s;
  animation-delay: 0.8s;
}

.ladder:nth-child(5n+5) {
  -webkit-animation-delay: 1.0s;
  animation-delay: 1.0s;
}

@-webkit-keyframes fadeIn {
  0% {
    opacity: 0.0;
  }
  100% {
    opacity: 1.0;
  }
}

@keyframes fadeIn {
  0% {
    opacity: 0.0;
  }
  100% {
    opacity: 1.0;
  }
}
<li class="ladder">A</li>
<li class="ladder">B</li>
<li class="ladder">C</li>
<li class="ladder">D</li>
<li class="ladder">E</li>

My question is how to make the css work no matter how many lines are in the list.

Answer:

Here's an idea using CSS variables that allows you to make your code smaller.

This isn't a general solution, but it's easier to add simple, inline CSS for each li than it is to write complex CSS :

.ladder {
  opacity: 0;
  animation: fadeIn 1s var(--d) forwards;
}

@keyframes fadeIn {
  100% {
    opacity: 1;
  }
}
<ul>
  <li style="--d:0s" class="ladder">A</li>
  <li style="--d:0.2s" class="ladder">B</li>
  <li style="--d:0.4s" class="ladder">C</li>
  <li style="--d:0.6s" class="ladder">D</li>
  <li style="--d:0.8s" class="ladder">E</li>
</ul>

Here is another idea where you can apply animation on ul:

ul {
  position:relative;
}
ul:before {
  content:"";
  position:absolute;
  top:-20px;
  bottom:0;
  left:0;
  right:0;
  background:linear-gradient(to bottom,transparent,#fff 20px);
  animation:fadeIn 2s forwards
}

@keyframes fadeIn {
  0% {
    top:-10px;
  }
  100% {
    top: 100%;
  }
}
<ul>
  <li>A</li>
  <li>B</li>
  <li>C</li>
  <li>D</li>
  <li>E</li>
</ul>
Scroll to Top