惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

AI
AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
N
News and Events Feed by Topic
O
OpenAI News
W
WeLiveSecurity
S
Secure Thoughts
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Heimdal Security Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Schneier on Security
Schneier on Security
Cloudbric
Cloudbric
T
Threatpost
T
Troy Hunt's Blog
T
Tenable Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
V
Vulnerabilities – Threatpost
Google Online Security Blog
Google Online Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
Forbes - Security
Forbes - Security
Cyberwarzone
Cyberwarzone
C
CXSECURITY Database RSS Feed - CXSecurity.com
P
Privacy & Cybersecurity Law Blog
L
Lohrmann on Cybersecurity
博客园 - 司徒正美
腾讯CDC
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
M
MIT News - Artificial intelligence
T
The Blog of Author Tim Ferriss
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
D
Docker
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
I
InfoQ
WordPress大学
WordPress大学
人人都是产品经理
人人都是产品经理
A
Arctic Wolf
L
LINUX DO - 热门话题
The Cloudflare Blog
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件

CSS Articles by Temani Afif

Get Ready For the Powerful CSS border-shape Property! | CSS-Tricks Let’s Play With Gap Decorations! How to Control Infinite CSS Animations (Part 2 of 2) How to Control Infinite CSS Animations (Part 1 of 2) Two Circles, One Arrow, and Anchor Positioning Making a Responsive Pyramidal Grid With Modern CSS | CSS-Tricks How to Create a CSS-only Elastic Text Effect Making Complex CSS Shapes Using shape() | CSS-Tricks Responsive Hexagon Grid Using Modern CSS | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 2) | CSS-Tricks Responsive List of Avatars Using Modern CSS (Part 1) | CSS-Tricks Perfectly Pointed Tooltips: To The Corners Perfectly Pointed Tooltips: All Four Sides Perfectly Pointed Tooltips: A Foundation Sequential linear() Animation With N Elements | CSS-Tricks Infinite Marquee Animation using Modern CSS Better CSS Shapes Using shape() — Part 4: Close and Move | CSS-Tricks Drawing CSS Shapes using corner-shape Better CSS Shapes Using shape() — Part 3: Curves | CSS-Tricks Better CSS Shapes Using shape() — Part 2: More on Arcs | CSS-Tricks Better CSS Shapes Using shape() — Part 1: Lines and Arcs | CSS-Tricks Creating Blob Shapes using clip-path: shape() Creating Flower Shapes using clip-path: shape() Custom progress element using the attr() function A CSS-Only Star Rating Component and More! (Part 2) | CSS-Tricks A CSS-Only Star Rating Component and More! (Part 1) | CSS-Tricks How to Create Wavy Boxes Using CSS Full-Bleed Layout with Modern CSS Fancy Menu Navigation Using Anchor Positioning | CSS-Tricks How to Create a Zig-Zag Box Using CSS How to Create Zig-Zag CSS Loaders Using One Element Custom Progress Element Using Anchor Positioning & Scroll-Driven Animations How to Create Filling CSS Loaders Using One Element How to Create Curved-Edge and Rounded-Edge Shapes Using CSS CSS Tricks That Use Only One Gradient | CSS-Tricks How to create Shapes with Inner Curves using CSS Mask Custom Range Slider Using Anchor Positioning & Scroll-Driven Animations How to Get the Width/Height of Any Element in Only CSS How Keyboard Navigation Works in a CSS Game How To Create Cut-Out Shapes using The clip-path property The Modern Guide For Making CSS Shapes — Smashing Magazine css-shape.com: The Ultimate Collection of CSS-only Shapes ⚡️ Sliding 3D Image Frames In CSS — Smashing Magazine CSS Tricks To Master The clip-path Property Creating Wavy Circles with Fancy Animations in CSS Modern CSS Tooltips And Speech Bubbles (Part 2) — Smashing Magazine Do you need a Tooltip or a Speech Bubble? I have created 100 using CSS 😲 Modern CSS Tooltips And Speech Bubbles (Part 1) — Smashing Magazine Creating Flower Shapes using CSS Mask & Trigonometric Functions CSS Shapes: The Triangle The Complex But Awesome CSS border-image Property — Smashing Magazine CSS Tricks to add 3D Effects to your Text CSS Responsive Multi-Line Ribbon Shapes (Part 2) — Smashing Magazine CSS Responsive Multi-Line Ribbon Shapes (Part 1) — Smashing Magazine I have made 100+ CSS-only Ribbon Shapes | The Perfect Collection 🎀 How to create a CSS-only infinite scroll animation Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 2) — Smashing Magazine Re-Creating The Pop-Out Hover Effect With Modern CSS (Part 1) — Smashing Magazine How to Create CSS Ribbon Shapes with a Single Element CSS Shapes: The Ribbon Revealing Images With CSS Mask Animations — Smashing Magazine css-loaders.com: The Biggest Collection of Loading Animations (more than 500 🤯) How To Define An Array Of Colors With CSS — Smashing Magazine CSS effects on images II Shines, Perspective, And Rotations: Fancy CSS 3D Effects For Images — Smashing Magazine What's your Lucky CSS Pattern? Check It Now! How to Add a CSS Reveal Animation to Your Images Modern Layouts using CSS Grid I created 100+ unique CSS patterns | The best collection 🤩 How to Create a Custom Range Slider Using CSS CSS Shapes: The Heart CSS Shapes: Polygon & Starburst CSS Tip: learn CSS the easy way! How to build a CSS-only accordion How to create Breadcrumb Navigation with CSS Different Ways to Get CSS Gradient Shadows | CSS-Tricks A CSS-only responsive Stepper component A Fancy Hover Effect For Your Avatar | CSS-Tricks How to make a zoom effect using CSS Creating a Custom Cursor using CSS CSS Infinite 3D Sliders | CSS-Tricks CSS Infinite Slider Flipping Through Polaroid Images | CSS-Tricks How to create a responsive sidebar menu using CSS CSS Infinite and Circular Rotating Image Slider | CSS-Tricks Making Static Noise From a Weird CSS Gradient Bug | CSS-Tricks CSS Grid and Custom Shapes, Part 3 | CSS-Tricks Fancy Image Decorations: Outlines and Complex Animations | CSS-Tricks Fancy Image Decorations: Masks and Advanced Hover Effects | CSS-Tricks Fancy Image Decorations: Single Element Magic | CSS-Tricks How to create an infinite image slider using CSS How to Create Wavy Shapes & Patterns in CSS | CSS-Tricks How I Made a Pure CSS Puzzle Game | CSS-Tricks How to create a Tooltip/Speech Bubble using CSS CSS Grid and Custom Shapes, Part 2 | CSS-Tricks CSS Grid and Custom Shapes, Part 1 | CSS-Tricks Zooming Images in a Grid Layout | CSS-Tricks How to create a CSS-only loader with one element Exploring CSS Grid’s Implicit Grid and Auto-Placement Powers | CSS-Tricks How to create background pattern using CSS & conic-gradient Single Element Loaders: The Bars | CSS-Tricks
A Text Reveal Animation using CSS
2023-04-22 · via CSS Articles by Temani Afif

A Text Reveal Animation using CSS

Written on by Temani AfifWeb Developer

Estimated read time 5 minutes

In this article, we are going to build a simple but cool reveal animation that you can apply to your text. It will also work with multi-line of texts! All you have to do is to add a class and see the magic in play.

Here is the demo we are going to build together

The animation will run on page load, to run it again click the small button “Rerun” on the bottom right.

How does it work?

To achieve such an effect we are going to rely on the background property and more precisely gradient layers. We are not going to clutter our code with a lot of <div> or pseudo-elements. Only two background layers will do the trick.

We will have one layer that will color the text and another one will come on top of the text. Then we will adjust the size/position of each layer to create the animation.

The HTML code will look like the below:

<span class="reveal">The text to reveal</span>

It’s important to notice that we need to consider an inline element as a container for the text. If you want to apply the effect to a title, for example, don’t do

<h1 class="reveal">The text to reveal</h1>

But instead, you have to write

<h1><span class="reveal">The text to reveal</span></h1>

The use of an inline element is the trick to make the effect works on multi-line of text.

Now that we have a clear idea of how it does work, let’s write some CSS.

40%

💰 40% OFF YOUR FIRST MONTH WITH VERPEX MANAGED CLOUD SERVERS

with the discount code

SERVERS-SALE

Introducing background-clip: text

We will start with the first background layer. Instead of using the color property, we are going to color the text using a gradient layer. To do this, we need background-clip: text

.reveal {
  color: #0000;
  background: linear-gradient(red 0 0) no-repeat;
  -webkit-background-clip: text;
          background-clip: text;
}

Nothing complex so far. We have text colored in red (the color defined by the gradient). Now let’s add some animation. In a previous article, I used the same animation technique to create a few loaders.

The logic is pretty simple. We are going to animate the width of the gradient from 0% to 100% to reveal the text.

TADA! We already have a cool reveal animation for our text with a simple code:

.reveal {
  color: #0000;
  background: 
    linear-gradient(red 0 0) no-repeat;
  background-size: 0% 100%; /* we start at 0% 100% */
  -webkit-background-clip: text;
          background-clip: text;
  animation: t 1.2s .5s both;
}

@keyframes t{
  to {background-size: 100% 100%} /* we animate to 100% 100%*/
}

Adding another background layer

Now let’s add the second background layer. This one will color the whole element and will behave the same as the previous layer. We will first animate its size from 0% to 100%

.reveal {
  color: #0000;
  background: 
    linear-gradient(red 0 0) no-repeat,
    linear-gradient(red 0 0) no-repeat;
  background-size: 0% 100%; /* we start at 0% 100% */
  -webkit-background-clip: padding-box,text;
          background-clip: padding-box,text;
  animation: t 1.2s .5s both;
}

@keyframes t{
  to {background-size: 100% 100%} /* we animate to 100% 100%*/
}

Note how in the code I defined two identical gradients but two different values for background-clip because we need one layer to color only the text while the second one is to color the whole element. I used padding-box but it can be content-box or border-box depending on whether you want to color the padding/border area or not.

We still need to add another step to our animation. We have to remove the top layer by sliding it to the right to reveal the text. For this, we can animate background-position.

This part is a bit tricky because we cannot update the position of a gradient that takes 100% width of its element. I am explaining this little quirk in detail here. To overcome this, we will update the size to make it 150% instead of 100% and then we can easily update the position of the first gradient to slide it to the right.

.reveal {
  color: #0000;
  background: 
    linear-gradient(red 0 0) no-repeat,
    linear-gradient(red 0 0) no-repeat;
  background-size: 0% 100%; /* we start at 0% 100% */
  -webkit-background-clip: padding-box,text;
          background-clip: padding-box,text;
  animation: 
    t 1.2s .5s both,
    b 1.2s 1.3s both; /* we run the second animation after the first one is done*/
}

@keyframes t{
  to {background-size: 150% 100%} /* we animate to 150% instead of 100%*/
}
@keyframes b {
  to {background-position:-200% 0,0 0} /* we update the position of only the first layer */
}

You are probably wondering what’s the logic behind the -200%. It’s the value I need to use to slide a background having a width equal to 150% to the right. I won’t deep dive into the math logic but I highly recommend reading my Stack Overflow answer where I am giving a detailed explanation.

We are done! we have nice reveal animation with minimal code. A code that we can optimize a little using a CSS variable to avoid the redundancy of the linear-gradient definition.

.reveal {
  color: #0000;
  --g: linear-gradient(red 0 0) no-repeat;
  background: var(--g),var(--g);
  background-size: 0% 100%;
  -webkit-background-clip: padding-box,text;
          background-clip: padding-box,text;
  animation: 
    t 1.2s .5s both,
    b 1.2s 1.3s both; 
}

@keyframes t{
  to {background-size: 150% 100%} 
}
@keyframes b {
  to {background-position:-200% 0,0 0}
}

Adding box-decoration-break: clone

You have probably noticed that the animation we made is slightly different that the one we saw at the beginning of the article. There is no bug but it’s how backgrounds are meant to work with inline elements. You may want to keep that behavior but you can also change it using the box-decoration-break property and more precisely the clone value

Each box fragment is rendered independently with the specified border, padding, and margin wrapping each fragment. The border-radius, border-image, and box-shadow are applied to each fragment independently. The background is also drawn independently for each fragment, which means that a background image with background-repeat: no-repeat may nevertheless repeat multiple times. ref

Here is a demo to compare both versions. Feel free to use the one that suits you the most.

90%

💰 90% OFF YOUR FIRST MONTH WITH ALL VERPEX HOSTING PLANS FOR WORDPRESS

with the discount code

AWESOME

Temani Afif

Temani Afif is an expert web developer, a content creator, and a CSS addict. He is the mastermind behind CSS Loaders, CSS Generators, CSS Tip and many other CSS-related websites.

View more posts by Temani Afif