background-clip:text
简单来说,使用background-clip: text 的元素,其背景内容只保留文字所在区域部分,配合透明文字 color: transparent,就能够利用文字透出背景。
这个属性配合滤镜以及混合,可以实现一些有趣的动效
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
margin: 0;
background-color: #000;
overflow: scroll;
}
p {
position: relative;
font-size: 15px;
text-align: justify;
font-weight: bold;
min-height: 100vh;
max-width: 800px;
letter-spacing: -1px;
word-spacing: 0px;
margin: 0 auto;
color: transparent;
background: url(动态gif图片);
background-position: center;
background-size: contain;
background-repeat: no-repeat;
-webkit-background-clip: text;
background-clip: text;
filter: grayscale(1) invert(1);
mix-blend-mode: hard-light;
}
</style>
</head>
<body>
<p>
<!-- 填充文字 -->
</p>
</body>
</html>
我们首先通过滤镜 filter: grayscale(1),将动态gif图片从彩色的,处理成黑白灰的。
基于一张黑白底色的图片,我们再运用 background-clip: text,在黑色的背景之上,通过混合模式的叠加处理,将文字的亮部保留,暗部与黑色背景融合。
让后,通过使用filter的invert() 函数,反转输入图像的色值,将 grayscale() 处理过的原图的明暗部分置换
这样,就可以实现文字的动态背景效果了