如何使用純CSS實現錫紙撕開的文字效果(附代碼)

來源:互聯網
上載者:User
本篇文章給大家帶來的內容是關於如何使用純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%;}

大功告成!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.