当前位置:Gxlcms > css > 如何使用纯CSS实现锡纸撕开的文字效果(附代码)

如何使用纯CSS实现锡纸撕开的文字效果(附代码)

时间:2021-07-01 10:21:17 帮助过:45人阅读

本篇文章给大家带来的内容是关于如何使用纯CSS实现锡纸撕开的文字效果(附代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

1237872444-5b849a1e37a81_articlex.png

源代码下载

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含若干子元素,每个子元素中包含一个字母:

  1. <div class="text">
  2. <span>A</span>
  3. <span>W</span>
  4. <span>E</span>
  5. <span>S</span>
  6. <span>O</span>
  7. <span>M</span>
  8. <span>E</span>
  9. </div>

定义容器尺寸:

  1. body {
  2. margin: 0;
  3. height: 100vh;
  4. }
  5. .text {
  6. width: 100%;
  7. height: 100%;
  8. }

设置子元素的布局方式:

  1. .text {
  2. display: flex;
  3. justify-content: space-between;
  4. }
  5. .text span {
  6. width: 100%;
  7. }

定义文本样式:

  1. .text span {
  2. color: darkslategray;
  3. background-color: rgb(127, 140, 141);
  4. font-family: serif;
  5. font-size: 12vmin;
  6. text-shadow: 1px 1px 1px white;
  7. display: flex;
  8. align-items: center;
  9. justify-content: center;
  10. }

设置文本的背景的渐变色,奇数位的文字和偶数位的文字的渐变方向是相反的:

  1. .text span:nth-child(odd) {
  2. background: linear-gradient(
  3. to bottom,
  4. rgba(127, 140, 141, 0.2) 0%,
  5. rgba(127, 140, 141, 0) 33%,
  6. rgba(127, 140, 141, 0.7) 66%,
  7. rgba(127, 140, 141, 0.2) 100%
  8. );
  9. }
  10. .text span:nth-child(even) {
  11. background: linear-gradient(
  12. to top,
  13. rgba(127, 140, 141, 0.2) 0%,
  14. rgba(127, 140, 141, 0) 33%,
  15. rgba(127, 140, 141, 0.7) 66%,
  16. rgba(127, 140, 141, 0.2) 100%
  17. );
  18. }

增加文字之间的分隔线,第1个文字之前不用加分隔线:

  1. .text span {
  2. position: relative;
  3. }
  4. .text span:not(:first-child)::before {
  5. content: '';
  6. position: absolute;
  7. width: 10px;
  8. height: 90%;
  9. background-color: black;
  10. left: -5px;
  11. border-left: 1px solid white;
  12. border-radius: 50%;
  13. }

让分隔线上下错位:

  1. .text span:not(:first-child):nth-child(odd)::before {
  2. top: 2%;
  3. }
  4. .text span:not(:first-child):nth-child(even)::before {
  5. bottom: 2%;
  6. }

大功告成!

相关推荐:

如何用纯CSS实现接扎啤的特效(附源码)

如何使用纯CSS实现一个沙漏的动画效果

如何使用css实现监控网络连接状态的页面

以上就是如何使用纯CSS实现锡纸撕开的文字效果(附代码)的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行