Tuesday, December 5, 2023
HomeVideo EditingThe best way to Construct a Pure CSS Loading Animation (With Keyframes)

The best way to Construct a Pure CSS Loading Animation (With Keyframes)


Animations are an effective way to make a web site extra attention-grabbing; let me present you ways.

Most animations rely on JavaScript in a single kind or one other, particularly once they cope with sophisticated sequences or happen after a consumer interplay corresponding to scrolling or clicking.

Nonetheless, if we’re constructing a easy animation that doesn’t require any consumer interplay in any respect, then it may be carried out purely with CSS.

1. Markup With HTML

For the construction of our webpage, we’ll break down the banner into the person components we’ll be animating.

1
<div class="container">
2
  <div class="split-screen"></div>
3
  <div class="background-image"></div>
4
  <div class="content material">
5
    <p>Good day <span class="comma">,</span></p>
6
    <p>World</p>
7
  </div>
8
</div>

2. Styling With CSS

Now we will type the format of our web page. Let’s start with the static components i.e. the weather that don’t want any animation.

1
physique {
2
  margin: 0;
3
  background-color: #b1bcbf;
4
}
5
6
.container {
7
  width: 100%;
8
  peak: 100vh;
9
  place: relative;
10
}
11
12
.background-image {
13
  width: 50%;
14
  place: absolute;
15
  prime: 0;
16
  backside: 0;
17
  left: 0;
18
  margin: auto;
19
  background-image: url("https://photographs.pexels.com/pictures/1261728/pexels-photo-1261728.jpeg");
20
  background-size: cowl;
21
}

Right here’s what our web page appears to be like like at the moment:

A webpage with half an imageA webpage with half an imageA webpage with half an image

3. Animating With CSS

As soon as now we have our static styling carried out, we will transfer on to the animated components. The principle factor to bear in mind when working with CSS animations is monitoring the timing of every animated component to ensure they movement easily collectively. Fortunately, you may view the main points of any animation on a webpage utilizing the Chrome DevTools Animation Inspector. This chart provides us a transparent thought of the beginning level and through of each animated component in our webpage.

Let’s check out the timeline for our demo:

A chart showing the different timings of the element animationsA chart showing the different timings of the element animationsA chart showing the different timings of the element animations

Now now we have an thought of what the timeline appears to be like like, let’s recreate it beginning with the cut up display screen animation. 

The preliminary styling for the component is about to fully cowl the web page:

1
.split-screen {
2
  width: 100%;
3
  background-color: #2c2d2b;
4
  place: absolute;
5
  proper: 0;
6
  prime: 0;
7
  backside: 0;
8
  z-index: 2;
9
}

Then we’ll outline a brand new animation utilizing the @keyframes property.

Keyframes are used to find out the timeline of every animation.

For instance, if now we have an animation lasting 2 seconds and we set a keyframe property opacity: 1 at 50%, this implies the component will attain an opacity of 1 at 1s. We will additionally use the from and to key phrases to specify the beginning and finish keyframes.

We need to set the split-screen component to go from 100% to 50% of the web page width.

1
@keyframes reduceSize {
2
  from {
3
    width: 100%;
4
  }
5
  to {
6
    width: 50%;
7
  }
8
}

Assign the Animation Keyframes to an Component

Now we will assign this animation to the split-screen component utilizing the animation-name property. We will additionally management the timing of the animation utilizing the animation-duration and animation-delay properties. Based mostly on animation chart above, we wish the split-screen animation to begin 500ms after the web page has loaded so we’ll give it a delay of 0.5s:

1
animation-name: reduceSize;
2
animation-duration: 1.5s;
3
animation-delay: 0.5s;

That is what our animation appears to be like like now (once more, hit Rerun to see the animation):

Animation Fill Mode

From the pen above, we’ll discover that the component reverts again to its authentic state after animation. We will forestall this from taking place through the use of the animation-fill-mode property. This property permits us to manage the state of an animated component earlier than and after the animation is carried out. 

1
animation-name: reduceSize;
2
animation-duration: 1.5s;
3
animation-delay: 0.5s;
4
animation-fill-mode: forwards;

By setting the property to forwards, we’re specifying that the animation ought to preserve the state of its final keyframe. That is our up to date animation:

Utilizing Shorthand Property is Even Higher

We will use the animation shorthand property to use all of the animation styling thus far. Now our cut up display screen styling appears to be like like this:

1
.split-screen {
2
  width: 100%;
3
  background-color: #2c2d2b;
4
  place: absolute;
5
  left: 0;
6
  prime: 0;
7
  backside: 0;
8
  z-index: 2;
9
  animation: reduceSize 1.5s 0.5s forwards;
10
}

The animation-duration worth ought to be positioned earlier than the animation-delay worth.

Subsequent Animation

The subsequent animation we apply to the cut up display screen banner is shifting it to the fitting of the display screen. We’ll outline a keyframe property to deal with this:

1
@keyframes moveRight {
2
  from {
3
    left: 0%;
4
  }
5
  to {
6
    left: 50%;
7
  }
8
}

We’ll set the timing of the animation to begin 3s after the web page masses and final for a complete of 1.5s. We will additionally mix animations in CSS:

1
.split-screen {
2
  width: 100%;
3
  background-color: #2c2d2b;
4
  place: absolute;
5
  left: 0;
6
  prime: 0;
7
  backside: 0;
8
  z-index: 2;
9
  animation: 
10
    reduceSize 1.5s 0.5s forwards,
11
    moveRight 1.5s 3s forwards;
12
}

Animation on Web page Content material

Subsequent, we’ll animate the content material on the web page. 

There are three animations to be set for the content material:

  1.  Content material fades in whereas sliding up from the underside of the web page
  2. Content material slides to the fitting 
  3. Content material adjustments textual content color from white to a background picture

Let’s outline the keyframes for every. For sliding the content material to the fitting, we will reuse the already outlined moveRight animation.

1
@keyframes fadeInUp {
2
  from {
3
    remodel: translateY(100vh);
4
    opacity: 0;
5
  }
6
  to {
7
    remodel: translateY(0);
8
    opacity: 1;
9
  }
10
}
11
12
@keyframes changeBackground {
13
  to {
14
    background-image: url("https://photographs.pexels.com/pictures/1261728/pexels-photo-1261728.jpeg");
15
    background-size: 200%;
16
    background-position: heart;
17
    background-clip: textual content;
18
    -webkit-background-clip: textual content;
19
    coloration: clear;
20
  }
21
}

For the changeBackground keyframe, we use the background-clip property to set the textual content color as a background picture as a substitute of a single color.

Now let’s type the content material. Since we’re working with animation delays, we’ll want to use the beginning keyframe of the animation on to the content material or this may trigger a jar when the animation begins. Check out what occurs if we don’t have the preliminary content material styling matching the keyframe begin type:

The content material is seen on web page load earlier than the opacity and remodel properties within the fadeInUp animation are utilized, so we’ll want to incorporate these properties within the content material styling:

1
.content material {
2
  coloration: white;
3
  font-size: 10vw;
4
  text-transform: uppercase;
5
  place: absolute;
6
  width: fit-content;
7
  peak: fit-content;
8
  prime: 0;
9
  left: 0;
10
  proper: 0;
11
  backside: 0;
12
  margin: auto;
13
  z-index: 4;
14
  opacity: 0;
15
  remodel: translateY(100vh);
16
  animation: 
17
    fadeInUp 2s 0.5s, 
18
    moveRight 1.5s 3s,
19
    changeBackground 1.5s 3s;
20
  animation-fill-mode: forwards;
21
}

One Final Element

Lastly, we apply one final animation to the span containing a comma only for enjoyable:

1
.content material .comma {
2
  coloration: #2c2d2b;
3
  opacity: 1;
4
  animation: fadeOut 0.5s forwards 2.5s;
5
}
6
7
@keyframes fadeOut {
8
  from {
9
    opacity: 1;
10
  }
11
  to {
12
    opacity: 0;
13
  }
14
}

We’re Accomplished!

And that’s that—we’re carried out with our CSS animated banner!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments