how do i make a button fade in with css keyframes?

2.2k Views Asked by At

I'm trying to make my button fade in with keyframes but it isn't working and I know I'm doing something wrong. I ended up getting the animated text to work but this is giving me a headache and I cannot find info anywhere that has helped me... I'm new to web development so don't be too harsh on me or my (probably ugly and unorganized) code lol. I'm trying to learn as much as I can, so if you can please explain why it isn't working and why something else might? Thanks guys <3

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300&display=swap" rel="stylesheet">

    <title>Welcome! | Video Editing by Scottis</title>

</head>
<body>
    <!-- Header -->
<div class="container">
    <span class="text1">Video Editing by Scottis</span>
</div>


    <!-- Buttons -->
    <style>
        .btn {
           background-color: white;
           color: rgb(133, 4, 255);
           text-align: center;
           font-size: 15px;
           font-weight: bold;
           margin-right: -250px;
           margin-top: 600px;
           margin-left: 515px;
           padding: 30px;
        }




     </style>
  </head>
  <body>
    <button class="btn">Portfolio</button>
    <button class = "btn">Pricing</button>
    <button class="btn">Contact</button>


</body>
</html>

My CSS:

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

body {
  margin: 0;
  font-family: 'Roboto', sans-serif;
}

body {
    background-image: url("./assets/background.png");
    background-color: #cccccc;
    background-repeat: no-repeat;
  }

  /* Create three equal columns that floats next to each other */
.col-md-3 {
    float: left;
    width: 15%;
    padding: 15px;
    padding: 10px;
    margin: auto;
    display: block;
  }

  /* Clear floats after the columns */
  .row:after {
    content: "";
    display: table;
    clear: both;
  }

  /* Responsive layout - makes the three columns stack on top of each other instead of next to each other */
  @media screen and (max-width:768px) {
    .col-md-3 {
      width: 100% auto;
    }
  }

.col-md-12 {
  text-align: center;
}

.card-title {
  text-align: center;
}

.container{
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
}

.container span{
  color: white;
  text-transform: uppercase;
  display: block;

}

.text1{
  color: white;
  font-size: 60px;
  font-weight: 700;
  letter-spacing: 8px;
  margin-bottom: 20px;
  position: relative;
  animation: text 3s 1;
}


@keyframes text {
  0%{
    margin-bottom: -20%;
  }
  30%{
    letter-spacing: 25px;
    margin-bottom: -40px;
  }
  85%{
    letter-spacing: 15px;
    margin-bottom: -40px;
  }

  }
}

@keyframes button {
  0%{
    opacity: 0%;
  }
  0%{
    opacity: 0%;
  }
  25%{
    opacity: 25%;
  }
  50%{
    opacity: 50%;
  }
  75%{
    opacity: 75%;
  }
  100%{
    opacity: 100%;
  }
}

1

There are 1 best solutions below

0
dale landry On

You have some issues with your code first off. You are repeating both the head and body tags out of proper valid html sequence. Have a close look at the following page and resource from MDN: https://developer.mozilla.org/en-US/docs/Web/HTML/Element

As to your issue with keyframes, define the class you wish to control with the sequence in your css and add the animation with a unique call name, I used fadeIn. Below I have added Mozilla, webkit, opera and ms @keyframe animations for opacity. I am defining the animation to start the timer (3 seconds) @keyframe 0% { opacity: 0; } , then end at 100% { opacity: 1; }. I add multiple kits for different browsers.

.btn {
  animation: fadeIn ease 3s;
  -webkit-animation: fadeIn ease 3s;
  -moz-animation: fadeIn ease 3s;
  -o-animation: fadeIn ease 3s;
  -ms-animation: fadeIn ease 3s;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@-moz-keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

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

@-o-keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  font-family: sans-serif;
}

body {
  margin: 0;
  font-family: 'Roboto', sans-serif;
}

body {
  background-image: url("./assets/background.png");
  background-color: #cccccc;
  background-repeat: no-repeat;
}

/* Start class for buttons animation fadeIn ease */
.btn {
  animation: fadeIn ease 3s;
  -webkit-animation: fadeIn ease 3s;
  -moz-animation: fadeIn ease 3s;
  -o-animation: fadeIn ease 3s;
  -ms-animation: fadeIn ease 3s;
}

@keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@-moz-keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

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

@-o-keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@-ms-keyframes fadeIn {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}
/*  End of animation keyframes  */

/* Create three equal columns that floats next to each other */

.col-md-3 {
  float: left;
  width: 15%;
  padding: 15px;
  padding: 10px;
  margin: auto;
  display: block;
}


/* Clear floats after the columns */

.row:after {
  content: "";
  display: table;
  clear: both;
}


/* Responsive layout - makes the three columns stack on top of each other instead of next to each other */

@media screen and (max-width:768px) {
  .col-md-3 {
    width: 100% auto;
  }
}

.col-md-12 {
  text-align: center;
}

.card-title {
  text-align: center;
}

.container {
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
}

.container span {
  color: white;
  text-transform: uppercase;
  display: block;
}

.text1 {
  color: white;
  font-size: 60px;
  font-weight: 700;
  letter-spacing: 8px;
  margin-bottom: 20px;
  position: relative;
  animation: text 3s 1;
}

@keyframes text {
  0% {
    margin-bottom: -20%;
  }
  30% {
    letter-spacing: 25px;
    margin-bottom: -40px;
  }
  85% {
    letter-spacing: 15px;
    margin-bottom: -40px;
  }
}


}
<div class="container">
  <span class="text1">Video Editing by Scottis</span>
</div>

<button class="btn">Portfolio</button>
<button class="btn">Pricing</button>
<button class="btn">Contact</button>