We’ve walked through a series of posts now about interesting approaches to CSS hover effects. We started with a bunch of examples that use CSS background
properties, then moved on to the text-shadow
property where we technically didn’t use any shadows. We also combined them with CSS variables and calc()
to optimize the code and make it easy to manage.
In this article, we will build off those two articles to create even more complex CSS hover animations. We’re talking about background clipping, CSS masks, and even getting our feet wet with 3D perspectives. In other words, we are going to explore advanced techniques this time around and push the limits of what CSS can do with hover effects!
Cool Hover Effects series:
- Cool Hover Effects That Use Background Properties
- Cool Hover Effects That Use CSS Text Shadow
- Cool Hover Effects That Use Background Clipping, Masks, and 3D (you are here!)
Here’s just a taste of what we’re making:
Hover effects using background-clip
Let’s talk about background-clip
. This CSS property accepts a text
keyword value that allows us to apply gradients to the text of an element instead of the actual background.
So, for example, we can change the color of the text on hover as we would using the color
property, but this way we animate the color change:
All I did was add background-clip: text
to the element and transition
the background-position
. Doesn’t have to be more complicated than that!
But we can do better if we combine multiple gradients with different background clipping values.
In that example, I use two different gradients and two values with background-clip
. The first background gradient is clipped to the text (thanks to the text
value) to set the color on hover, while the second background gradient creates the bottom underline (thanks to the padding-box
value). Everything else is straight up copied from the work we did in the first article of this series.
How about a hover effect where the bar slides from top to bottom in a way that looks like the text is scanned, then colored in:
This time I changed the size of the first gradient to create the line. Then I slide it with the other gradient that update the text color to create the illusion! You can visualize what’s happening in this pen:
We’ve only scratched the surface of what we can do with our background-clip
ping powers! However, this technique is likely something you’d want to avoid using in production, as Firefox is known to have a lot of reported bugs related to background-clip
. Safari has support issues as well. That leaves only Chrome with solid support for this stuff, so maybe have it open as we continue.
Let’s move on to another hover effect using background-clip
:
You’re probably thinking this one looks super easy compared to what we’ve just covered — and you are right, there’s nothing fancy here. All I am doing is sliding one gradient while increasing the size of another one.
But we’re here to look at advanced hover effects, right? Let’s change it up a bit so the animation is different when the mouse cursor leaves the element. Same hover effect, but a different ending to the animation:
Cool right? let’s dissect the code:
.hover {
--c: #1095c1; /* the color */
color: #0000;
background:
linear-gradient(90deg, #fff 50%, var(--c) 0) calc(100% - var(--_p, 0%)) / 200%,
linear-gradient(var(--c) 0 0) 0% 100% / var(--_p, 0%) no-repeat,
var(--_c, #0000);
-webkit-background-clip: text, padding-box, padding-box;
background-clip: text, padding-box, padding-box;
transition: 0s, color .5s, background-color .5s;
}
.hover:hover {
color: #fff;
--_c: var(--c);
--_p: 100%;
transition: 0.5s, color 0s .5s, background-color 0s .5s;
}
We have three background layers — two gradients and the background-color
defined using --_c
variable which is initially set to transparent (#0000
). On hover, we change the color to white and the --_c
variable to the main color (--c
).
Here’s what is happening on that transition
: First, we apply a transition to everything but we delay the color
and background-color
by 0.5s
to create the sliding effect. Right after that, we change the color
and the background-color
. You might notice no visual changes because the text is already white (thanks to the first gradient) and the background is already set to the main color (thanks to the second gradient).
Then, on mouse out, we apply an instant change to everything (notice the 0s
delay), except for the color
and background-color
that have a transition. This means that we put all the gradients back to their initial states. Again, you will probably see no visual changes because the text color
and background-color
already changed on hover.
Lastly, we apply the fading to color and a background-color
to create the mouse-out part of the animation. I know, it may be tricky to grasp but you can better visualize the trick by using different colors:
Hover the above a lot of times and you will see the properties that are animating on hover and the ones animating on mouse out. You can then understand how we reached two different animations for the same hover effect.
Let’s not forget the DRY switching technique we used in the previous articles of this series to help reduce the amount of code by using only one variable for the switch:
.hover {
--c: 16 149 193; /* the color using the RGB format */
color: rgb(255 255 255 / var(--_i, 0));
background:
/* Gradient #1 */
linear-gradient(90deg, #fff 50%, rgb(var(--c)) 0) calc(100% - var(--_i, 0) * 100%) / 200%,
/* Gradient #2 */
linear-gradient(rgb(var(--c)) 0 0) 0% 100% / calc(var(--_i, 0) * 100%) no-repeat,
/* Background Color */
rgb(var(--c)/ var(--_i, 0));
-webkit-background-clip: text, padding-box, padding-box;
background-clip: text, padding-box, padding-box;
--_t: calc(var(--_i,0)*.5s);
transition:
var(--_t),
color calc(.5s - var(--_t)) var(--_t),
background-color calc(.5s - var(--_t)) var(--_t);
}
.hover:hover {
--_i: 1;
}
If you’re wondering why I reached for the RGB syntax for the main color, it’s because I needed to play with the alpha transparency. I am also using the variable --_t
to reduce a redundant calculation used in the transition
property.
Before we move to the next part here are more examples of hover effects I did a while ago that rely on background-clip
. It would be too long to detail each one but with what we have learned so far you can easily understand the code. It can be a good inspiration to try some of them alone without looking at the code.
I know, I know. These are crazy and uncommon hover effects and I realize they are too much in most situations. But this is how to practice and learn CSS. Remember, we pushing the limits of CSS hover effects. The hover effect may be a novelty, but we’re learning new techniques along the way that can most certainly be used for other things.
Hover effects using CSS mask
Guess what? The CSS mask
property uses gradients the same way the background
property does, so you will see that what we’re making next is pretty straightforward.
Let’s start by building a fancy underline.
I’m using background
to create a zig-zag bottom border in that demo. If I wanted to apply an animation to that underline, it would be tedious to do it using background properties alone.
Enter CSS mask
.
The code may look strange but the logic is still the same as we did with all the previous background animations. The mask
is composed of two gradients. The first gradient is defined with an opaque color that covers the content area (thanks to the content-box
value). That first gradient makes the text visible and hides the bottom zig-zag border. content-box
is the mask-clip
value which behaves the same as background-clip
linear-gradient(#000 0 0) content-box
The second gradient will cover the whole area (thanks to padding-box
). This one has a width that’s defined using the --_p
variable, and it will be placed on the left side of the element.
linear-gradient(#000 0 0) 0 / var(--_p, 0%) padding-box
Now, all we have to do is to change the value of --_p
on hover to create a sliding effect for the second gradient and reveal the underline.
.hover:hover {
--_p: 100%;
color: var(--c);
}
The following demo uses with the mask layers as backgrounds to better see the trick taking place. Imagine that the green and red parts are the visible parts of the element while everything else is transparent. That’s what the mask will do if we use the same gradients with it.
With such a trick, we can easily create a lot of variation by simply using a different gradient configuration with the mask
property:
Each example in that demo uses a slightly different gradient configuration for the mask
. Notice, too, the separation in the code between the background configuration and the mask configuration. They can be managed and maintained independently.
Let’s change the background configuration by replacing the zig-zag underline with a wavy underline instead:
Another collection of hover effects! I kept all the mask configurations and changed the background to create a different shape. Now, you can understand how I was able to reach 400 hover effects without pseudo-elements — and we can still have more!
Like, why not something like this:
Here’s a challenge for you: The border in that last demo is a gradient using the mask
property to reveal it. Can you figure out the logic behind the animation? It may look complex at first glance, but it’s super similar to the logic we’ve looked at for most of the other hover effects that rely on gradients. Post your explanation in the comments!
Hover effects in 3D
You may think it’s impossible to create a 3D effect with a single element (and without resorting to pseudo-elements!) but CSS has a way to make it happen.
What you’re seeing there isn’t a real 3D effect, but rather a perfect illusion of 3D in the 2D space that combines the CSS background
, clip-path
, and transform
properties.
The first thing we do is to define our variables:
--c: #1095c1; /* color */
--b: .1em; /* border length */
--d: 20px; /* cube depth */
Then we create a transparent border with widths that use the above variables:
--_s: calc(var(--d) + var(--b));
color: var(--c);
border: solid #0000; /* fourth value sets the color's alpha */
border-width: var(--b) var(--b) var(--_s) var(--_s);
The top and right sides of the element both need to equal the --b
value while the bottom and left sides need to equal to the sum of --b
and --d
(which is the --_s
variable).
For the second part of the trick, we need to define one gradient that covers all the border areas we previously defined. A conic-gradient
will work for that:
background: conic-gradient(
at left var(--_s) bottom var(--_s),
#0000 90deg,var(--c) 0
)
0 100% / calc(100% - var(--b)) calc(100% - var(--b)) border-box;
We add another gradient for the third part of the trick. This one will use two semi-transparent white color values that overlap the first previous gradient to create different shades of the main color, giving us the illusion of shading and depth.
conic-gradient(
at left var(--d) bottom var(--d),
#0000 90deg,
rgb(255 255 255 / 0.3) 0 225deg,
rgb(255 255 255 / 0.6) 0
) border-box
The last step is to apply a CSS clip-path
to cut the corners for that long shadow sorta feel:
clip-path: polygon(
0% var(--d),
var(--d) 0%,
100% 0%,
100% calc(100% - var(--d)),
calc(100% - var(--d)) 100%,
0% 100%
)
That’s all! We just made a 3D rectangle with nothing but two gradients and a clip-path
that we can easily adjust using CSS variables. Now, all we have to do is to animate it!
Notice the coordinates from the previous figure (indicated in red). Let’s update those to create the animation:
clip-path: polygon(
0% var(--d), /* reverses var(--d) 0% */
var(--d) 0%,
100% 0%,
100% calc(100% - var(--d)),
calc(100% - var(--d)) 100%, /* reverses 100% calc(100% - var(--d)) */
0% 100% /* reverses var(--d) calc(100% - var(--d)) */
)
The trick is to hide the bottom and left parts of the element so all that’s left is a rectangular element with no depth whatsoever.
This pen isolates the clip-path
portion of the animation to see what it’s doing:
The final touch is to move the element in the opposite direction using translate
— and the illusion is perfect! Here’s the effect using different custom property values for varying depths:
The second hover effect follows the same structure. All I did is to update a few values to create a top left movement instead of a top right one.
Combining effects!
The awesome thing about everything we’ve covered is that they all complement each other. Here is an example where I am adding the text-shadow
effect from the second article in the series to the background
animation technique from the first article while using the 3D trick we just covered:
The actual code might be confusing at first, but go ahead and dissect it a little further — you’ll notice that it’s merely a combination of those three different effects, pretty much smushed together.
Let me finish this article with a last hover effect where I am combining background, clip-path, and a dash of perspective
to simulate another 3D effect:
I applied the same effect to images and the result was quite good for simulating 3D with a single element:
Want a closer look at how that last demo works? I wrote something up on it.
Wrapping up
Oof, we are done! I know, it’s a lot of tricky CSS but (1) we’re on the right website for that kind of thing, and (2) the goal is to push our understanding of different CSS properties to new levels by allowing them to interact with one another.
You may be asking what the next step is from here now that we’re closing out this little series of advanced CSS hover effects. I’d say the next step is to take all that we learned and apply them to other elements, like buttons, menu items, links, etc. We kept things rather simple as far as limiting our tricks to a heading element for that exact reason; the actual element doesn’t matter. Take the concepts and run with them to create, experiment with, and learn new things!
Cool CSS Hover Effects That Use Background Clipping, Masks, and 3D originally published on CSS-Tricks. You should get the newsletter.
0 Comments