How we can fill a text from the direction of hover? Solution: Fill From Hover Direction, CSS Text Fill Effect On Hover.
Maybe you have seen many types of text fill effects before. There are many kinds of fill effects, with some effect fill slowly, with some fill fast or with glitch, etc. Many peoples create a creative one, I saw a fill effect like water filling in text, that was awesome.
Today I will show you a different kind of fill effect, maybe have seen before. This filling effect depends on hover direction, if you hover on text from the left then it will fill left to right. This is the whole concept of this text fill effect. The same conditions will apply for other directions like right, top, & bottom.
So, Today I am sharing a CSS Text Fill Effect on hover, fill from hovering direction. This effect is pure CSS, but it’s a little bit tricky. Using CSS3 we can create a lot of amazing stuff like this. Creating this effect possible with CSS variables otherwise, we have to use JavaScript. I am sure that I will understand fully how its work after seeing the codes.
If you are thinking now how this effect actually, then see the preview given below.
Preview Of Fill From Hover Direction
See this video preview to getting an idea of how this fill effect look like.
Now you can see the effect visually. If you like this then get the source code of its.
You May Also Like:
- CSS Text Shadow Effects
- Responsive CSS Flexbox
- HTML CSS Glitch Effect
- Calendar With HTML CSS JavaScript
- Animate & Divide Text On Scroll
Discussion
Before sharing source code, let’s talk about how it works. I created a div and put 4 <a>
and one <span>
inside it. & gave positon: absolute
in both tags. And put both one same position, One over the other. After that, I created a path on every <a>
tag using CSS clip-path (get info) property. I created a different path for each hyperlink tag, by using CSS :nth-child(x)
property.
Basically, the path creates for different directions, 4 paths for four sides left, right, top, & bottom. I used CSS var for giving text color, fill from, & fill to which direction values. If you don’t know about var then this is an example:
1 |
--color-of-text: red; |
Now we created a variable for text color, now we don’t have to put black value in multiple fields. Just have to put the variable name which place we want the black color.
After that, I created a fill effect for each direction. As you know I created a path for different directions. At this time I used :nth-child()
property again, but this time I add :hover
with that. Now it looks like a:nth-child(1):hover ~ .text:before { }
. This creates the fill direction on which direction we will hover. Suppose we create a path on the left side, when we will hover from the left or on the path, then the text will fill from left to right.
Inside this direction hover effect I created two variable, on for declare where we start the fill and where ends. I gave the name for those variable, --from
& --to
. Then I created an animation using the @keyframe
property & put it on every direction’s hovering effect. I used variables name in animation, its look like this.
1 2 3 4 5 6 7 8 9 10 11 |
@keyframes textani { 0% { -webkit-clip-path: var(--from); clip-path: var(--from); } 100% { -webkit-clip-path: var(--to); clip-path: var(--to); color: var(--color-of-text); } } |
After all, let’s talk about the main things, The highest priority in this effect goes to data-text. The data-* attributes is used to store custom data private to the page or application (more info). I think now you can understand the whole concept after getting the codes.
CSS Text Fill Source Code
For creating this program you have to create just 2 files. One for HTML & one for CSS. Follow the steps to creating this without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<!DOCTYPE html> <!-- code by webdevtrick ( https://webdevtrick.com ) --> <html> <head> <meta charset="UTF-8"> <title>CSS Text Fill From Hover Direction | Webdevtrick.com</title> <link rel="stylesheet" href="style.css"> </head> <body> <link href="https://fonts.googleapis.com/css?family=Staatliches&display=swap" rel="stylesheet"> <div class="main"> <a href=""></a> <a href=""></a> <a href=""></a> <a href=""></a> <span class="text" data-text="WEBDEV">WEBDEV</span> </div> </body> </html> |
style.css
Now create a CSS file named ‘style.css‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
/** code by webdevtrick ( https://webdevtrick.com ) **/ * { margin: 0; padding: 0; box-sizing: border-box; } body { display: flex; align-items: center; justify-content: center; height: 100vh; background-color: #f3f3f3; } .main { --color-of-text: #f05855; position: relative; } .main a { position: absolute; width: 100%; height: 100%; z-index: 1; } .main a:nth-child(1) { -webkit-clip-path: polygon(0 0, 100% 0, 50% 50%); clip-path: polygon(0 0, 100% 0, 50% 50%); } .main a:nth-child(2) { -webkit-clip-path: polygon(100% 0, 100% 100%, 50% 50%); clip-path: polygon(100% 0, 100% 100%, 50% 50%); } .main a:nth-child(3) { -webkit-clip-path: polygon(0 100%, 50% 50%, 100% 100%); clip-path: polygon(0 100%, 50% 50%, 100% 100%); } .main a:nth-child(4) { -webkit-clip-path: polygon(0 0, 50% 50%, 0 100%); clip-path: polygon(0 0, 50% 50%, 0 100%); } .main a:hover { -webkit-clip-path: none; clip-path: none; z-index: 2; } .main a:nth-child(1):hover ~ .text:before { --from: circle(0% at 50% 0%); --to: circle(150% at 0% 50%); -webkit-animation: textani 1s forwards; animation: textani 1s forwards; } .main a:nth-child(2):hover ~ .text:before { --from: circle(0% at 150% 50%); --to: circle(100% at 50% 50%); -webkit-animation: textani 1s forwards; animation: textani 1s forwards; } .main a:nth-child(3):hover ~ .text:before { --from: circle(0% at 50% 100%); --to: circle(150% at 0% 50%); -webkit-animation: textani 1s forwards; animation: textani 1s forwards; } .main a:nth-child(4):hover ~ .text:before { --from: circle(0% at 0% 50%); --to: circle(150% at 0% 50%); -webkit-animation: textani 1s forwards; animation: textani 1s forwards; } .text { color: transparent; -webkit-text-stroke: 1px var(--color-of-text); text-stroke: 1px var(--color-of-text); font-size: 350pt; font-family: 'Staatliches', cursive; } .text:before { content: attr(data-text); position: absolute; } @keyframes textani { 0% { -webkit-clip-path: var(--from); clip-path: var(--from); } 100% { -webkit-clip-path: var(--to); clip-path: var(--to); color: var(--color-of-text); } } |
That’s It. Now you have successfully created the CSS Text Fill effect from hover direction. If you have any doubt or question comment down below.
Credit & Inspiration From Vangeltzo
Thanks For Visiting, Keep Visiting.
What if you wanted to use this twice in the same block of text? As I’ve given it a go, but they both hover and fill at the same time? Rather than one at a time?