本篇文章給大家帶來的內容是關於如何使用純CSS實現錫紙撕開的文字效果(附代碼) ,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所協助。
效果預覽
原始碼下載
https://github.com/comehope/front-end-daily-challenges
代碼解讀
定義 dom,容器中包含若干子項目,每個子項目中包含一個字母:
<div class="text"> <span>A</span> <span>W</span> <span>E</span> <span>S</span> <span>O</span> <span>M</span> <span>E</span></div>
定義容器尺寸:
body { margin: 0; height: 100vh;}.text { width: 100%; height: 100%;}
設定子項目的布局方式:
.text { display: flex; justify-content: space-between;}.text span { width: 100%;}
定義文本樣式:
.text span { color: darkslategray; background-color: rgb(127, 140, 141); font-family: serif; font-size: 12vmin; text-shadow: 1px 1px 1px white; display: flex; align-items: center; justify-content: center;}
設定文本的背景的漸層色,奇數位的文字和偶數位的文字的漸層方向是相反的:
.text span:nth-child(odd) { background: linear-gradient( to bottom, rgba(127, 140, 141, 0.2) 0%, rgba(127, 140, 141, 0) 33%, rgba(127, 140, 141, 0.7) 66%, rgba(127, 140, 141, 0.2) 100% );}.text span:nth-child(even) { background: linear-gradient( to top, rgba(127, 140, 141, 0.2) 0%, rgba(127, 140, 141, 0) 33%, rgba(127, 140, 141, 0.7) 66%, rgba(127, 140, 141, 0.2) 100% );}
增加文字之間的分隔線,第1個文字之前不用加分隔線:
.text span { position: relative;}.text span:not(:first-child)::before { content: ''; position: absolute; width: 10px; height: 90%; background-color: black; left: -5px; border-left: 1px solid white; border-radius: 50%;}
讓分隔線上下錯位:
.text span:not(:first-child):nth-child(odd)::before { top: 2%;}.text span:not(:first-child):nth-child(even)::before { bottom: 2%;}
大功告成!