Create Beautiful Scrolling Animations With the CSS Clip-Path Property

In a previous tutorial, we learned how to create a grayscale-to-color effect on scroll. To implement it, we took advantage of modern front-end features like CSS Grid, the clip-path property, and the Intersection Observer API.

Today, we’ll use these tools and the knowledge gained from that tutorial to build another cool scroll effect. As we scroll, page elements will sequentially appear with a slide animation. This is a common scroll effect that you might see on portfolio websites.

What We’ll be Building

Without further intro, let’s check out what we’ll be building:

Note: Be sure to read the aforementioned tutorial as we’ll grab some code snippets from it.

1. Begin With the HTML Markup

We’ll start with a wrapper section element that will contain nested sections. Each sub-section will have one of the following three possible layouts:

• An image on the left, text on the right.
• Big text on the right.
• Text on the left, the image on the right.

Here’s a visual representation of these layouts:

It’s worth noting that for all layouts, we’re going to use the same Pixabay image. You might remember that we’ve already used it in another scroll tutorial.

The required markup:

2. Define the Styles

Coming up next, we’ll write down the most important aspects of our styles. For this demonstration, we won’t discuss the reset rules as they aren’t anything important. So, straight to the point:

• We’ll define the grid, grid-alternate, and big-text classes to capture the different layouts. The first two take advantage of CSS Grid, while the last one sets a maximum width with margin-left: auto.
• Just to practice with a new CSS function, we’ll use clamp() to create fluid typography.
• Each figure element will have an absolutely positioned ::before pseudo-element. That element will act as an image overlay and be animated first.
• All animated elements will initially be hidden thanks to the clip-path property. Again, to really understand how this property works, check out this tutorial.
• To chain the animations, we’ll pass appropriate values to the transition-delay property of each element. Feel free to make them dynamic by using CSS variables.
• On screens up to 1000px wide, all elements will be stacked. Additionally, on image sections, the image will always come before the text.

Here are the corresponding styles based on these assumptions:

3. Animate on Scroll

As we scroll, we’ll animate only the image sections (i.e. those that have the grid class). More specifically, we’ll animate the following elements with this order:

• The ::before pseudo-element of the figure element (the one with the dark background).
• The image.
• The image caption.

When at least 50% of each figure element enters the viewport, it will receive the is-animated class. Otherwise, it will lose this class and become hidden.

So when the half part of a figure is within the viewport, the associated child elements will appear sequentially from different directions. First, the ::before pseudo-element will appear from the right, then the image from the left, and finally its caption from the right.

The JavaScript code that we’re going to need is taken from the grayscale-to-color effect tutorial. We just have to match different elements:

Here are the associated CSS rules that create the sequence of animations:

Note: Instead of the clip-path property, we could equally have used transform to accomplish the same effect. Check out the third example of this tutorial for more info.

Conclusion

That’s it, folks! Today we went through a way for sequentially revealing elements on scroll by taking advantage of amazing new front-end tools. Most importantly, we built this effect without using any libraries.

Hopefully you’ll be able to enrich your pages with a similar effect.

As a side note, before deciding to use these features on a project that requires support for older browsers, check the browser support at caniuse.com.

Here’s a reminder of what we built:

If you have ever used the clip-path property to build something helpful, let us know! As always, thanks a lot for reading!