This article brings to you the content is about how to use the pure CSS to achieve the text break animation effect (with source), there is a certain reference value, the need for friends can refer to, I hope to help you.
Effect Preview
Source code Download
Https://github.com/comehope/front-end-daily-challenges/tree/master/012-broken-text-effects
Code interpretation
Define the DOM, with only one element, the element has a Data-text attribute, and the property value equals the text within the element:
<div class= "text" data-text= "Break" >BREAK</div>
Center display:
HTML, body { height:100%; Display:flex; Align-items:center; Justify-content:center;}
To set the gradient background color:
Body { background:linear-gradient (brown, Sandybrown);}
Set font size for text:
. text { font-size:5em; font-family: "Arial Black";}
Add text using pseudo-elements:
. text { position:relative;}. Text::before,.text::after { content:attr (data-text); Position:absolute; top:0; left:0; Color:lightyellow;}
Set the matte for the text on the left:
. text::before { background-color:darkgreen; Clip-path:polygon (0 0, 60% 0, 30% 100%, 0 100%);}
Set the background and matte for the text on the right:
. text::after { background-color:darkblue; Clip-path:polygon (60% 0, 100% 0, 100% 100%, 30% 100%);}
When the mouse is over, the text of the mask is shifted to both sides:
. text::before,.text::after { transition:0.2s;}. Text:hover::before {left : -0.15em;}. text:hover::after { left:0.15em;}
Hides auxiliary elements, including the background color of original text and pseudo-elements:
. text { color:transparent;}. Text::before { /*background-color:darkgreen;*/}.text::after { /*background-color:darkblue;*/}
Text on both sides adds a skew effect:
. text:hover::before { transform:rotate ( -5deg);}. Text:hover::after { transform:rotate (5deg);}
Fine-tune the height of text:
. text:hover::before { top: -0.05em;}. text:hover::after { top:0.05em;}
Done!