2

I'm trying to make the divs appear one after another on page load. Problem is this setup only works IF I add a visibility: hidden property to the div selector, which in turn, reverses the animation. What am I missing?

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73, 73, 73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53, 53, 53);
}

.box1 {
  animation: test 1s;
}

.box2 {
  animation: test 1.1s;
}

.box3 {
  animation: test 1.2s;
}

.box4 {
  animation: test 1.3s;
}

.box5 {
  animation: test 1.4s;
}

@keyframes test {
  from {
    visibility: hidden;
  }
  to {
    visibility: visible;
  }
}
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<div class="box4"></div>
<div class="box5"></div>
Pete
  • 57,112
  • 28
  • 117
  • 166
David Skx
  • 131
  • 8
  • `visibility` can not be animated. Change to `opacity: 0` and `opacity: 1` and your animation will work fine. – Turnip Sep 03 '20 at 13:17
  • @Turnip it's not the way I intended it to work though. I don't want it to fade in, but rather pop in. – David Skx Sep 03 '20 at 13:20
  • As @Turnip said, it's impossible to animate it this way using `visibility`. See https://stackoverflow.com/questions/27900053/css-transition-with-visibility-not-working – johannchopin Sep 03 '20 at 13:28

4 Answers4

1

You need to animate the opacity to make the item appear - you can set the length to 1% so it just "pops" in rather than fades in.

I have also set the animation-fill-mode to forwards so the final state is maintained and used animation-delay to set the time between each box popping in

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73, 73, 73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53, 53, 53);
}

.box {
  opacity: 0;
  animation: test 0.1s forwards;
}

.box1 {
  animation-delay: 1s;
}

.box2 {
  animation-delay: 1.1s;
}

.box3 {
  animation-delay: 1.2s;
}

.box4 {
  animation-delay: 1.3s;
}

.box5 {
  animation-delay: 1.4s;
}

@keyframes test {
  0% {
    opacity: 0;
  }
  99% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
<div class="box box5"></div>
Pete
  • 57,112
  • 28
  • 117
  • 166
0

please add visibility: hidden; to every boxes

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73,73,73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53,53,53);
}

.box1{
  visibility: hidden;
  animation: test 1s;
}

.box2{
  visibility: hidden;
  animation: test 1.1s;
}

.box3{
  visibility: hidden;
  animation: test 1.2s;
}

.box4{
  visibility: hidden;
  animation: test 1.3s;
}

.box5{
  visibility: hidden;
  animation: test 1.4s;
}

@keyframes test {
  from {
    visibility: hidden;
  }

  to {
    visibility: visible;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./style.css">
  <title>Document</title>
</head>
<body>

  <div class="box1"></div>
  <div class="box2"></div>
  <div class="box3"></div>
  <div class="box4"></div>
  <div class="box5"></div>


</body>
</html>
Rayees AC
  • 4,426
  • 3
  • 8
  • 31
0

As it is a loader, Here is another I can propose you with height and width, infinite loader:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73,73,73);
}

div {
  width: 15vh;
  height: 15vh;
  background-color: rgb(53,53,53);
}

.box1{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0s infinite;
}

.box2{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.2s infinite;
}

.box3{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.4s infinite;
}

.box4{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.6s infinite;
}

.box5{
  width : 15vh;
  height: 15vh;
  animation: test 1s ease 0.8s infinite;
}

@keyframes test {
  from {
    height: 15vh;
    width: 15vh;
  }

  to {
     height: 0;
    width: 0;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./style.css">
  <title>Document</title>
</head>
<body>

  <div class="box1"></div>
  <div class="box2"></div>
  <div class="box3"></div>
  <div class="box4"></div>
  <div class="box5"></div>


</body>
</html>
MaxiGui
  • 6,190
  • 4
  • 16
  • 33
0

@Pete Excellent work. I used 'scale' instead of opacity. Thanks!

    * {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 100vh;
  background-color: rgb(73,73,73);
}

div {
  transform: scale(0);
  width: 15vh;
  height: 15vh;
  background-color: rgb(53,53,53);
}

.box {
  animation: test 0.4s forwards;
}

.box1{
  animation-delay: 0.2s;
}

.box2{
  animation-delay: 0.4s;
}

.box3{
  animation-delay: 0.6s;
}

.box4{
  animation-delay: 0.8s;
}

.box5{
  animation-delay: 1s;
}

@keyframes test {
  from {
    transform: scale(0);
  }

  to {
    transform: scale(1, 1);
  }
}
<div class="box box1"></div>
<div class="box box2"></div>
<div class="box box3"></div>
<div class="box box4"></div>
<div class="box box5"></div>
David Skx
  • 131
  • 8