


How to use CSS to achieve fancy text effects? The following article will introduce to you how to use the background series attributes to achieve some fancy text effects. I hope it will be helpful to you!
This article will explain how to use the background
series of properties to cleverly achieve some fancy text effects. Through this article, you will be able to learn:
- Achieve cool text underline effects through
background-size
andbackground-position
- Through
background-size
andbackground-position
andbackground-clip
to achieve the effect of text gradually appearing - Through
animation- delay
Realize the gradual appearance effect of text
Cause
The motivation for writing this article is that one day, I was attracted by such a title--10 Masterfully Designed Websites, which lists 10 extremely creative Web sites.
One of the Red Bull Racing websites is the homepage introducing the F1 Red Bull Racing Team. There is such an interesting hover animation effect:
#The hover effect of this text seems simple, but in fact, if you want to fully realize it, it is very complicated to rely only on CSS. , one of the more difficult parts is - How to make an effect gradually apply to the part of the entire text, instead of giving the entire effect to the entire text at once.
Use background to achieve the underline effect of text
At this point, I remembered the previous article in this article--CSS text decoration text-decoration & text-emphasis, I introduced a using background to simulate the effect of underline.
Look at a simple DEMO, use background
to simulate the underline effect of text:
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. <a>Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam</a>, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</p>
p { width: 600px; font-size: 24px; color: #666; } a { background: linear-gradient(90deg, #0cc, #0cc); background-size: 100% 3px; background-repeat: no-repeat; background-position: 100% 100%; color: #0cc; }
Use background
to simulate the underline effect of text, the effect is as follows :
background to simulate a dotted underline:
a { background: linear-gradient(90deg, #0cc 50%, transparent 50%, transparent 1px); background-size: 10px 2px; background-repeat: repeat-x; background-position: 100% 100%; }
CodePen Demo -- Use background to simulate underline and dotted underlinehttps://codepen.io/Chokcoco/pen/YzNQKwmOf course this is the most basic, clever use# With various attributes of ##background
, we can achieve various interesting effects.
background-size and background-position
realize text hover animation
Here, through clever changes# With the ##background-size
and background-position properties, we can achieve some very interesting text hover effects.
Let’s first look at such a Demo. The core code acts on the
tag:
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. <a>Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam</a>, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</p>
a { background: linear-gradient(90deg, #ff3c41, #fc0, #0ebeff); background-size: 0 3px; background-repeat: no-repeat; background-position: 0 100%; transition: 1s all; color: #0cc; } a:hover { background-size: 100% 3px; color: #000; }
Although we set
background: linear-gradient(90deg, #ff3c41, #fc0, #0ebeff), but initially defaulted to its background-size: 0 3px, That is to say, you cannot see the underline at the beginning. When hovering, change
background-size: 100% 3px. At this time, there will be a
0 3px to
100% 3px transformation, which is a stretching effect from scratch.
Look at the final effect:
Because the set background-position
0 100% , if the set
background-position is
100% 100%, we can get a reverse effect:
// 其他都保持一致,只改变 background-position,从 0 100% 改为 100% 100% a { ... background-position: 100% 100%; ... }
Look at the effect again, you can Compare the above animation to see the specific differences:
##CodePen Demo -- background underline animation
OK, if we usebackground-positionbackground
to implement two overlapping underlines, then use the above With two different
values, we can get a more interesting underline hover effect. <p>CSS 代码示意,注意看两条使用 background 模拟的下划线的 <code>background-position
的值是不一样的:
a { background: linear-gradient(90deg, #0cc, #0cc), linear-gradient(90deg, #ff3c41, #fc0, #8500d8); background-size: 100% 3px, 0 3px; background-repeat: no-repeat; background-position: 100% 100%, 0 100%; transition: 0.5s all; color: #0cc; } a:hover { background-size: 0 3px, 100% 3px; color: #000; }
可以得到这样一种效果,其实每次 hover, 都有两条下划线在移动:
CodePen Demo -- background 下划线动画
https://codepen.io/Chokcoco/pen/MWJogjQ
通过 background-size
与 background-position
配合 background-clip
实现文字的渐现
上述一大段都在围绕 -- 下划线。
回归到本文一开始提到的 Gif 效果,我们能否实现在一段文字中,实现文字的渐现效果呢?
上述技巧利用的是 background
,那么 background
背景色能否改变文字的颜色的?答案是可以的,只需要借助 background-clip
。
我们稍微改造下代码,实现利用 background-clip
实现 hover 的时候部分文字逐渐改变颜色:
<p> Lorem ipsum dolor sit amet consectetur adipisicing elit. <a>Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, </a> molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero. </p>
p { color: #666; cursor: pointer; } a { background: linear-gradient(90deg, #fc0, #fc0); background-size: 0 100px; background-repeat: no-repeat; background-position: 0 100%; background-clip: text; transition: .6s all linear; } p:hover a { background-size: 100% 100%; color: transparent; }
看看效果,通过 background-clip: text
的遮罩裁剪,我们将 background: linear-gradient(90deg, #fc0, #fc0)
背景色作用给了文字,同时利用 color: transparent
让文字展示出背景色的色值:
CodePen Demo -- background-size 与 background-position 以及 background-clip 实现文字逐个渐现
https://codepen.io/Chokcoco/pen/qBjmvdq?editors=1100
当然,稍微对上述代码变形,我们就可以演化出几种不同的效果。
实现整段文字的渐现 - 从透明到出现
第一种就是从透明到有颜色,逐渐展现,这里我们只需要让 color 一直是 transparent 即可(下述效果借助了一个按钮去触发效果):
<div class="button">Button</div> <p><a>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</a></p>
a { background: linear-gradient(90deg, #fc0, #fc0); background-size: 0 100px; background-repeat: no-repeat; background-position: 0 100%; color: transparent; background-clip: text; } .button:hover ~ p a { transition: .8s all linear; background-size: 0 100px, 100% 100%; }
效果如下:
实现整段文字的渐现 - 从一种颜色到另外一种颜色
还可以实现文字从一种颜色到另外一种颜色的逐个转变,只需要添加多一层 background-image
渐变。
<div class="button">Button</div> <p><a>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</a></p>
a { background: linear-gradient(90deg, #999, #999), linear-gradient(90deg, #fc0, #fc0); background-size: 100% 100%, 0 100px; background-repeat: no-repeat; background-position: 100% 100%, 0 100%; color: transparent; background-clip: text; } .button:hover ~ p a { transition: .8s all linear; background-size: 0 100px, 100% 100%; }
这里需要解释一下,虽然设置了 color: transparent
,但是文字默认还是有颜色的,默认的文字颜色,是由第一层渐变赋予的 background: linear-gradient(90deg, #999, #999), linear-gradient(90deg, #fc0, #fc0)
,也就是这一层:linear-gradient(90deg, #999, #999)
。
当 hover 触发时,linear-gradient(90deg, #999, #999)
这一层渐变逐渐消失,而另外一层 linear-gradient(90deg, #fc0, #fc0)` 逐渐出现,借此实现上述效果。
CodePen -- background-clip 文字渐现效果
https://codepen.io/Chokcoco/pen/XWgpyqz
简单模拟题图效果
这里,我们简单利用这个技巧模拟一下文章一开始列出的 Gif 的效果:
这个效果原作者的技巧是:
将每个单词作为一个对象,包裹一个特殊的 class
利用
animation-delay
将动画逐渐赋予每个单词
这里,我们将整段文本统一处理,简单还原:
<div class="button">Button</div> <p><a>Lorem ipsum dolor sit amet consectetur adipisicing elit. Mollitia nostrum placeat consequatur deserunt velit ducimus possimus commodi temporibus debitis quam, molestiae laboriosam sit repellendus sed sapiente quidem quod accusantium vero.</a></p>
/** 动画核心 background、line-height、opacity **/ a { background: linear-gradient(90deg, #ff5722, #ff5722), linear-gradient(90deg, #aaa, #aaa); background-size: 100% 100%, 0 100px; background-repeat: no-repeat; background-position: 100% 100%, 0 100%; cursor: pointer; color: transparent; background-clip: text; line-height: 3; opacity: 0; } .button:hover ~ p a { transition: 1.2s background .3s ease-out, .8s line-height ease-out, .6s opacity ease-in; background-size: 0 100px, 100% 100%; color: transparent; line-height: 1; opacity: 1; } / ** 简单控制半透明黑色遮罩出现 **/ a::before { content: ""; position: fixed; background: rgba(0, 0, 0, .8); top: 0; left: 0; right: 0; bottom: 0; z-index: -1; transition: .3s all linear; opacity: 0; } .button:hover ~ p a::before { opacity: 1; }
效果如下:
可以看到,由于是整体控制整段文本,效果上没有逐个单词控制的好,但是优点是代码量非常少。对于一些简单卡片类的 hover 场景,足以。
background-image、background-clip 实现文字渐现效果
https://codepen.io/Chokcoco/pen/abwWMJm
完美还原题图效果
当然,题图效果使用纯 CSS 也是不在话下的。只不过就不是简单能够统一处理的了。
这里,我们需要对每一个单词进行精细化的处理,并且使用每个单词的伪元素进行额外的动画。
简单的结构如下:
<div class="button">Button</div> <div class="g-wrap"></div> <p> <span data-text="Lorem">Lorem</span> <span data-text="ipsum">ipsum</span> <span data-text="dolor">dolor</span> <span data-text="sit">sit</span> <span data-text="amet">amet</span> // ... 类似结构 </p>
可以看到,每个单词都被 <span></span>
包裹,并且添加了 data-text
,方便伪元素拿到当前单词。
接下来,就是设定动画,并且通过顺序给每个 <span></span>
添加相应递增的 animation-delay
以实现没个单词动画的差异性。核心的伪代码如下:
p { position: relative; width: 500px; overflow: hidden; } p span { position: relative; display: inline-block; opacity: 0; transform: translateY(15px) translateZ(0); transition-property: transform, opacity; transition-duration: .3s, .2s; } .button:hover ~ p span { opacity: 1; color: #ddd; transform: translateY(0) translateZ(0); transition-duration: 1s, .2s; } p span:after, p span:before { position: absolute; content: attr(data-text); top: 0; left: 0; z-index: 1; transform: translateZ(0); } p span:after { color: #e62541; transition-property: opacity; transition-duration: .1s; } .button:hover ~ p span:after { opacity: 0; transition-property: opacity; transition-duration: .4s; } @for $i from 1 to 21 { p span:nth-child(#{$i}) { transition-delay: #{$i * 0.04}s; &::after { transition-delay: #{$i * 0.04 + 0.2}s; } } }
其实动画本身不太复杂,主要讲两点:
hover 状态下和非 hover 状态下的
transition-duration
是不一样的,是因为取消 hover 过程中,动画消失过程的时间通常是要求更短的;借助了 SASS 的循环
@for $i from 1 to 21 {}
递增给每个span
和它的伪元素添加了递增的transition-delay
;
最终,我们可以得到如下的结果:
完整的代码,你可以参看 -- CSS 灵感 - 利用 animation-delay 实现文字渐现效果
https://csscoco.com/inspiration/#/./animation/animation-delay-control-text-effect
原文地址:https://segmentfault.com/a/1190000040693286
作者:chokcoco
更多编程相关知识,请访问:编程视频!!
The above is the detailed content of How to use CSS background series properties to achieve some fancy text effects. For more information, please follow other related articles on the PHP Chinese website!

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools