How to Create CSS Animations for Website.

How to Create CSS Animations for Website.

how-to-create-CSS-animation
{tocify} $title={Table of Contents}

CSS Animations:

CSS Animation animate HTML elements transition to one style to another.

CSS animations we can animate elements without having to use JavaScript.

You can change as many CSS properties you would like, as many times you want.

Animates have two components:

  1. A style describing the CSS animation.
  2. A set of Keyframes that indicate the start, intermediate waypoints and and states of the animations style.

CSS Animation Properties.

  • @keyframes
  • animation-name
  • animation-delay
  • animation-iteration-count
  • animation direction
  • animation-fill-mode
  • animation

The @keyframes Rule.

The @keyframes rule specifies the animation code.

The animation is made one by one changing starting with one bunch of CSS style then onto the next.

Each @keyframes rule contains a style list of keyframe selectors, which indicate rates along the animation when the keyframe happens, and a square containing the styles for that keyframe 0% indicates the starting point of the animation sequence, while 100% indicates the final state animation.

Because 0% and 100% are very important, they have special aliases from and to.

@keyframes CSS Syntax:


@keyframes animationame{
keyframes-slecter {
css-style;
}
}

Example:


@keyframes anim {
0% {
transform: rotate(30deg);
}
@keyframes anim2 {
0% {
transform: rotate(45deg);
}
25% {
transform: rotate(90deg);
}
50% {
transform: rotate(120deg);
}
75% {
transform: rotate(180deg);
}
100% {
transform: rotate(360deg);
}
}

CSS3 Animation Timing Function

The Animation-Timing-Function CSS property sets how the animation move during each cycle period.

  • ease: default, the animation effect starts slowly, then fast, then slowly.
  • linear: the animation effect has the same speed from start to end.
  • ease-in: the animation effect start slowly.
  • ease-out: the animation effect end slowly.
  • ease-in-out: the animation effect both starts and and slowly.

Example:

ease
ease-in
ease-out
ease-in-out
linear

CSS Code:


.test-box1{
width:100%;
height:450px;
display: flex;
justify-content:center;
flex-direction: column;
}
.test-bol{
border-radius: 50%;
color:grey;
width:80px;
height:80px;
background-color:lightblue;
margin:5px 0;
position: relative;
animation:slide;
animation-duration:5s;
animation-iteration-count: infinite;
left:80%;
display:grid;
place-items: center;;
}
.bol1{
animation-timing-function:ease;
}
.bol2{
animation-timing-function:ease-in;
}
.bol3{
animation-timing-function:ease-out;
}
.bol4{
animation-timing-function:ease-in-out;
}
.bol5{
animation-timing-function:linear;
}
@keyframes slide {
from {
left:80%;
}
50%{
left:0%;
}
to{
left:80%
}
}

HTML Code:


<div class="test-box1">
<div> class="test-bol bol1">ease</div>
<div> class="test-bol bol2">ease-in</div>
<div> class="test-bol bol3">ease-out</div>
<div> class="test-bol bol4">ease-in-out</div>
<div> class="test-bol bol5">linear</div>
</div>

CSS3 Animation shorthand.

The animation CSS  property is a shorthand for the following:
  • animation-name
  • animation-duration
  • animation-timing-function
  • animation-delay
  • animation-iteration-count
  • animation-direecton
  • animation-fill-mode
  • animation-play-state

#selector{
animation-name:anim;
animation-duration:5s;
animation-timing-function: linear;
animation-delay:2s;
animation-iteration-count:infinite;
animation-direction:alternate;
}
/* One line CSS3 Animation */
#selector{
animation:anim 5s linear 2s infinite alternate;
}

CSS3 Animation Direction

The animation-direction CSS property sets whether the animation should run reverse or change front and rear.

  • normal: default, the animation plays forwards each cycle.
  • reverse: the animation plays backwards each cycle.
  • alternate: the animation reverses direction each cycle, with the fist iteration being played forwards.
  • alternate-reverse: the animation reverses direction each cycle, with the first iteration being played backwards.

alternate-reverse Example:

PoorWebDev

CSS Code:


*{
margin:0;padding:0;
box-sizing: border-box;
}
:root{
--bgcolor:#e0e0e0;
--rights:#bababa;
--left:#ffffff
}
.anim{
background-color:var(--bgcolor);
padding:20px;
display: grid;
place-items: center;
height:100vh;
}
.box{
padding:20px;
height:400px;
width:400px;
border-radius: 10px;
background: var(--bgcolor);
box-shadow: 25px 25px 50px var(--rights),-25px -25px 50px var(--left);
}
.social-box{
margin-top:20px;
height:40px;
display: flex;
justify-content:center;
align-items:center;
}
.ilink i:hover{
box-shadow: 3px 3px 6px rgb(0, 145, 96),-3px -3px 6px var(--left);
margin-bottom:8px;
}
.ilink{
margin:0 auto;
}
.ilink i{
font-size:42px;
padding:6px 8px;
border-radius: 2px;
background: var(--bgcolor);
box-shadow: 3px 3px 6px var(--rights),-3px -3px 6px var(--left),inset 3px 3px 3px var(--left),-3px -3px 10px var(--rights);
color:rgb(0, 145, 96);
}
.box1{
width:200px;height:200px;
margin:0 auto;
padding:20px;
border-radius:50%;
background: var(--bgcolor);
box-shadow: 2px 2px 5px var(--rights),-2px -2px 5px var(--left),inset 2px 2px 5px var(--rights),-2px -2px 5px var(--left);}
.box2{
display:grid;
place-items: center;
width:160px;height:160px;
border-radius:50%;
background:linear-gradient(#ffffff,#e0e0e0e0);
box-shadow: 2px 2px 5px var(--rights),-2px -2px 5px var(--left),inset 2px 2px 5px var(--left),-2px -2px 5px var(--rights);animation: anim 4s ease-in-out 2s infinite alternate-reverse;}
.anim-title{
color:rgb(0, 151, 101);
text-shadow: 2px 2px 5px var(--rights),-2px -2px 5px var(--left);
animation: anim 4s ease-in-out 2s infinite alternate-reverse;
}
@keyframes anim {
0% {transform: rotate(360deg);
}
}

HTML Code:


<div class="anim">
<div class="box">
<div class="box1">
<div class="box2">
<h2 class="anim-title">PoorWebDev</h2>
</div>
</div>
<div class="social-box">
<a class="ilink" href="https://www.facebook.com/PoorWebDev"><i class="fab fa-facebook"></i></a>
<a class="ilink" href="https://twitter.com/kanaiya1220"><i class="fab fa-twitter"></i></a>
<a class="ilink" href="https://www.instagram.com/kanaiya_valvi/"><i class="fab fa-instagram"></i></a>
<a class="ilink" href="https://pin.it/2wcWexx"><i class="fab fa-pinterest"></i></a>
</div>
</div>
</div>





style="display:block"
data-ad-client="ca-pub-1810152667600509"
data-ad-slot="1171232430"
data-ad-format="auto"
data-full-width-responsive="true">



Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *