CSS 奇思妙想 background-clip

开发 前端
说起 background-clip ,可能很多人都很陌生。Clip 的意思为修剪,那么从字面意思上理解,background-clip 的意思即是背景裁剪。

[[410846]]

说起 background-clip ,可能很多人都很陌生。Clip 的意思为修剪,那么从字面意思上理解,background-clip 的意思即是背景裁剪。

我曾经在 从条纹边框的实现谈盒子模型[1] 一文中谈到了这个属性,感兴趣的可以回头看看。

简单而言,background-clip 的作用就是设置元素的背景(背景图片或颜色)的填充规则。

与 box-sizing 的取值非常类似,通常而言,它有 3 个取值:

  1.     background-clip: border-box;  // 背景延伸到边框外沿(但是在边框之下) 
  2.     background-clip: padding-box; // 边框下面没有背景,即背景延伸到内边距外沿。 
  3.     background-clip: content-box; // 背景裁剪到内容区 (content-box) 外沿。 

不过这些都不是本文的主角。本文的主角是 background-clip: text; ,当然现在只有 chrome 支持,所以通常想使用它,需要 -webkit-background-clip:text;。

何为 -webkit-background-clip:text

使用了这个属性的意思是,以区块内的文字作为裁剪区域向外裁剪,文字的背景即为区块的背景,文字之外的区域都将被裁剪掉。

看个最简单的 Demo ,没有使用 -webkit-background-clip:text :

  1. <div>Clip</div> 
  2.  
  3. <style> 
  4. div { 
  5.   font-size: 180px; 
  6.   font-weight: bold; 
  7.   color: deeppink; 
  8.   background: url($img) no-repeat center center; 
  9.   background-size: cover; 
  10. </style> 

效果如下:

[[410847]]

CodePen Demo - Clip[2]

使用 -webkit-background-clip:text

我们稍微改造下上面的代码,添加 -webkit-background-clip:text:

  1. div { 
  2.   font-size: 180px; 
  3.   font-weight: bold; 
  4.   color: deeppink; 
  5.   background: url($img) no-repeat center center; 
  6.   background-size: cover; 
  7.   -webkit-background-clip: text; 

效果如下:

图片

CodePen Demo - clip[3]

看到这里,可能有人就纳闷了,

图片

,啥玩意呢,这不就是文字设置 color 属性嘛。

将文字设为透明 color: transparent

别急!当然还有更有意思的,上面由于文字设置了颜色,挡住了 div 块的背景,如果将文字设置为透明呢?文字是可以设置为透明的 color: transparent 。

  1. div { 
  2.   color: transparent; 
  3.   -webkit-background-clip: text; 

效果如下:

图片

CodePen Demo - clip[4]

通过将文字设置为透明,原本 div 的背景就显现出来了,而文字以为的区域全部被裁剪了,这就是 -webkit-background-clip:text 的作用。

嗨起来

了解了最基本的用法,接下来可以想想如何去运用这个元素制作一些效果。

大大增强了文字的颜色填充选择

文字颜色的动画效果

配合其他元素,实现一些其他巧妙的用法

实现文字渐变效果

利用这个属性,我们可以十分便捷的实现文字的渐变色效果。

  1. <div> background-clip: text</div> 
  1. div { 
  2.     font-size: 54px; 
  3.     color: transparent; 
  4.     background: linear-gradient(45deg, #ffeb3b, #009688, yellowgreen, pink, #03a9f4, #9c27b0, #8bc34a); 
  5.     background-clip: text; 
  6.     animation: huerotate 3s infinite; 
  7.  
  8. @keyframes huerotate { 
  9.     100% { 
  10.         filter: hue-rotate(360deg); 
  11.     } 
图片

CodePen Demo -- background-clip: text 文字渐变色[5];

背景渐变动画 && 文字裁剪

因为有用到 background 属性,回忆一下,我在上一篇 巧妙地制作背景色渐变动画![6] 利用了渐变 + animation 巧妙的实现了一些背景的渐变动画。可以很好的和本文的知识结合起来。

结合渐变动画,当然不一定需要过渡动画,这里我使用的是逐帧动画。配合 -webkit-background-clip:text,实现了一种,嗯,很红灯区的感觉:

  1. <div class="text">保健沐足按摩</div> 
  1. .text { 
  2.     font-size: 80px; 
  3.     background: linear-gradient(90deg, red 0, orange 15%, yellow 30%, green 45%, teal 60%, blue 75%, purple 90% ,purple 100%); 
  4.     background-clip: text; 
  5.     color: transparent; 
  6.     animation: changeColor .5s linear infinite alternate; 
  7.  
  8. @keyframes changeColor { 
  9.     0% { 
  10.         background-image: linear-gradient(90deg, red 0, orange 15%, yellow 30%, green 45%, teal 60%, blue 75%, purple 90% ,purple 100%); 
  11.     } 
  12.     50% { 
  13.         background-image: linear-gradient(90deg, deeppink 0, yellowgreen 15%, fuchsia 30%, lime 45%, olive 60%, aqua 75%, coral 90% ,brown 100%); 
  14.     } 
  15.     100% { 
  16.         background-image: linear-gradient(-90deg, red 0, orange 15%, yellow 30%, green 45%, teal 60%, blue 75%, purple 90% ,purple 100%); 
  17.     } 
图片

CodePen Demo -- 背景渐变动画 && 文字裁剪[7]

给文字增加高光动画

利用 background-clip, 我们还可以轻松的给文字增加高光动画。

譬如这样:

图片

其本质也是利用了 background-clip,伪代码如下:

  1. <p data-text="Lorem ipsum dolor"> Lorem ipsum dolor </p> 
  1. p { 
  2.     position: relative
  3.     color: transparent; 
  4.     background-color: #E8A95B; 
  5.     background-clip: text; 
  6. p::after { 
  7.     content: attr(data-text); 
  8.     position: absolute
  9.     left: 0; 
  10.     top: 0; 
  11.     width: 100%; 
  12.     height: 100%; 
  13.     background-image: linear-gradient(120deg, transparent 0%, transparent 6rem, white 11rem, transparent 11.15rem, transparent 15rem, rgba(255, 255, 255, 0.3) 20rem, transparent 25rem, transparent 27rem, rgba(255, 255, 255, 0.6) 32rem, white 33rem, rgba(255, 255, 255, 0.3) 33.15rem, transparent 38rem, transparent 40rem, rgba(255, 255, 255, 0.3) 45rem, transparent 50rem, transparent 100%); 
  14.     background-clip: text; 
  15.     background-size: 150% 100%; 
  16.     background-repeat: no-repeat; 
  17.     animation: shine 5s infinite linear; 
  18. @keyframes shine { 
  19.  0% { 
  20.   background-position: 50% 0; 
  21.  } 
  22.  100% { 
  23.   background-position: -190% 0; 
  24.  } 

去掉伪元素的 background-clip: text,就能看懂原理:

图片

CodePen Demo -- shine Text && background-clip[8]

按钮填充效果

运用这个属性,我们可以给按钮实现这样一种遮罩填充动画(主要是用于防止文字闪烁):

  1. <div class="btn">Btn</div> 
  1. .btn { 
  2.     position: relative
  3.     color: deeppink; 
  4.     background-color: transparent; 
  5.     border: 3px solid deeppink; 
  6.      
  7.     &::after { 
  8.         content: ''
  9.         position: absolute
  10.         z-index: -1; 
  11.         top: 0; 
  12.         left: 50%; 
  13.         height: 100%; 
  14.         width: 0; 
  15.         background-color: deeppink; 
  16.         transition: width .5s, left .5s; 
  17.     } 
  18.     &:hover { 
  19.         color: white; 
  20.     } 
  21.     &:hover::after { 
  22.         top: 0; 
  23.         left: 0; 
  24.         width: 100%; 
  25.         transition: width .5s, left .5s; 
  26.     } 
  27.  
  28. .btn { 
  29.     background-color: deeppink; 
  30.     background-image: linear-gradient(white, white); 
  31.     background-repeat: no-repeat; 
  32.     background-size: 0% 100%; 
  33.     background-position: center; 
  34.     -webkit-background-clip: text; 
  35.     -webkit-text-fill-color: transparent; 
  36.     transition: background-size .5s; 
  37.  
  38.     &:hover { 
  39.         background-size: 100% 100%; 
  40.     } 

效果如下:

图片

CodePen Demo -- background-clip:text && 按钮填充效果[9]

图片窥探效果

再演示其中一个用法,利用两个 div 层一起使用,设置相同的背景图片,父 div 层设置图片模糊,其中子 div 设置 -webkit-background-clip:text,然后利用 animation 移动子 div ,去窥探图片。简单的效果示意图:

[[410855]]

CodePen Demo -- background-clip: text 遮罩图片[10]

总结一下

其实还有很多有趣的用法,只有敢想并动手实践。当然很多人会吐槽这个属性的兼容性,到如今(2021-07-12),兼容性已经非常好了,主要是在使用的时候记得加上 -webkit- 前缀:

图片

更多精彩 CSS 技术文章汇总在我的 Github -- iCSS[11] ,持续更新,欢迎点个 star 订阅收藏。

如果还有什么疑问或者建议,可以多多交流,原创文章,文笔有限,才疏学浅,文中若有不正之处,万望告知。

参考资料

[1]从条纹边框的实现谈盒子模型:

http://www.cnblogs.com/coco1s/p/5895764.html

[2]CodePen Demo - Clip:

https://codepen.io/Chokcoco/pen/WjOBzB

[3]CodePen Demo - clip:

https://codepen.io/Chokcoco/pen/eWRaVJ

[4]CodePen Demo - clip:

https://codepen.io/Chokcoco/pen/oWwRmE

[5]CodePen Demo -- background-clip: text 文字渐变色:

https://codepen.io/Chokcoco/pen/PmjMwz

[6]巧妙地制作背景色渐变动画!:

http://www.cnblogs.com/coco1s/p/6603403.html

[7]CodePen Demo -- 背景渐变动画 && 文字裁剪:

https://codepen.io/Chokcoco/pen/xdroGp

[8]CodePen Demo -- shine Text && background-clip:

https://codepen.io/Chokcoco/pen/OJbEOmb

[9]CodePen Demo -- background-clip:text && 按钮填充效果:

https://codepen.io/Chokcoco/pen/MmoNoq

[10]CodePen Demo -- background-clip: text 遮罩图片:

https://codepen.io/Chokcoco/pen/LyNmQv

[11]Github -- iCSS:

https://github.com/chokcoco/iCSS

 

责任编辑:姜华 来源: iCSS前端趣闻
相关推荐

2022-02-22 07:50:10

CSS前端CSS-doodle

2023-01-31 10:23:00

CSS倒影效果

2022-03-31 07:46:17

CSS动画技巧

2021-07-06 06:07:14

CSS 技巧背景

2019-04-08 08:08:15

JS口令web安全

2022-07-14 07:01:56

Eureka读锁线程

2021-07-16 05:59:27

CSS 技巧带圆角的三角形

2014-09-25 01:31:22

办公设备智能硬件

2019-04-18 10:09:00

网络安全网络安全技术周刊

2010-09-01 13:37:58

CSSclip属性

2010-09-03 14:00:29

CSSbackground

2010-09-14 16:04:40

CSSclip属性

2010-08-31 10:57:44

clipCSS

2010-08-25 15:15:52

CSSclip

2012-03-31 10:12:55

CSSWEB

2011-10-21 09:10:12

JavaScript

2014-01-03 17:18:45

Windows 9开始屏幕

2021-08-13 09:01:31

Python小游戏Python基础

2021-05-11 18:39:24

AI

2024-01-11 12:45:12

AI训练
点赞
收藏

51CTO技术栈公众号