How to Create 9 Different CSS Hover Effects From Scratch

In this updated tutorial, we’re going to create seven different CSS hover effects and two additional ones that will require a little JavaScript. We’ll learn loads along the way, including how to animate font icons, borders, SVG paths, and work with Lo…


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos

In this updated tutorial, we’re going to create seven different CSS hover effects and two additional ones that will require a little JavaScript. We’ll learn loads along the way, including how to animate font icons, borders, SVG paths, and work with Lottie animations. You’ll be able to apply these hover effects to all kinds of situations. Let’s dive in!

Just to give you an idea of what you’ll learn during this tutorial, check out one of the demos we’ll be building:

Let’s start with this one, see you in the first step...

1. Revealing Icon CSS Hover Effect

In this first example, we’ll explore the demo you’ve already seen: a hover effect where text is replaced by an icon with slide animation.

The animation in action

Start with the Page Markup

We’ll start with a plain unordered list which will represent a typical page menu. Each menu link will include two span elements. The first span will contain the anchor text, while the second one will hold a Font Awesome icon:

Specify the Styles

The list will be a flex container with horizontally centered content:

Note: this basis style will be added to all the demos we build, so we won’t discuss it again. The demo also has some aesthetic styles we’ll reuse every time (such as the dark background etc.) that you can copy from the CodePen demo.

The first span in each item will have some padding around it:

Then the second span will be absolutely positioned and hidden by default. Plus, its icon will be horizontally and vertically centered:

Each time we hover over a link, its text will disappear. On the other hand, at that point, the associated icon will become visible:

Happily enough, there’s the option to modify the direction of the slide animations according to our needs. By default during the hover animation, the icon will appear from top to bottom, while the text will be moved to the bottom. To change that behavior, we have to pass the data-animation attribute to the target list. Possible values are to-top, to-right, and to-left.

The corresponding styles are shown below:

Here’s the full CodePen demo that summarizes the aforementioned cases:

2. Revealing Swipe CSS Hover Effect

In this second example, we’ll continue with another hover effect where text is replaced by a Font Awesome icon. But, this time, the replacement will happen in the background and won’t be visible to us, thanks to a “swipe” that happens in the foreground.

The animation in action

Specify the Page Markup

As ever, we’ll start with an unordered list that will contain the page links. In this case, though, each of our links will have a data-icon attribute. The value of this attribute will match the Unicode representation of a Font Awesome icon:

Specify the Styles

We’ll then define the ::before and ::after pseudo-elements of all links. 

  • The ::before pseudo-element will be positioned in the center of the link, though it will initially be invisible. 
  • The ::after pseudo-element will cover the full link dimensions, but it will also be hidden by default. Its content will hold a Font Awesome icon stored in the relevant data-icon attribute:

Each time we hover over a link, first the ::before pseudo-element will quickly appear. Then, it will be moved to the right and the icon will appear. During that time, the anchor text color will set to transparent. 

To achieve this precise timing synchronization, we have to customize the transitions' speed. For instance, we’ll add delays to the links as well as their ::after pseudo-element:

If you prefer a reversed animation, just add the data-animation="to-left" attribute to the corresponding list.

The animation in action

Here’s the CodePen demo that covers both cases:

3. Animated Underline CSS Hover Effect

In our third example, we’ll look at a common hover effect where an underline is revealed with slide animation.

The animation in action

Specify the Page Markup

We’ll start with an unordered list that will store the page links:

Specify the Styles

We’ll then define the ::before pseudo-element for all the links. We’ll place it to the bottom of its container and give it translate: scaleX(0), so it will initially be hidden. 

In addition, we’ll apply transform-origin: left to it because we want the animation to start from left to right:

Each time we hover over a link, its pseudo-element will appear:

Similar to the previous example, we have the option to change the direction of the slide animation. The line will appear from left to right by default, but to change that behavior, we have to pass the data-animation attribute to the target list. Possible values are to-left and center.

The corresponding styles are as follows:

Bonus!

We can go things one step further and change the transform-origin property value of the pseudo-element depending on its state. This makes the underline appear from one side, and disappear from the other.

The animation in action

Here are the rules that implement this functionality:

Here’s the CodePen demo for the scenarios we discussed:

4. Multiline Animation CSS Hover Effect

Moving on, in this fourth example, we’ll cover a hover effect where multiple lines will be revealed sequentially. The beauty of this technique is that it will give us the impression that the border of the hovered element is being painted. How cool is that!

Note: this effect can also be achieved with SVG, and we’ll look at something similar in a later demo.

The animation in action

Specify the Page Markup

Again, our markup will consist of an unordered list. Each of the menu links will contain four empty span elements:

Specify the Styles

The span elements will be absolutely positioned and hidden by default. However, their position and transform-origin will be different:

As we hover over a link, its spans will become visible sequentially, following a clockwise rotation. To do so, we’ll control when the second, third, and fourth spans become visible:

By modifying the transform-origin property value and canceling the delays of our elements, we can easily generate another nice effect.

The animation in action

To incorporate this effect in our menu, we have to append the data-animation="diagonal" attribute to it. This will result in the following CSS styles:

Take a look at the CodePen demo that gives us those effects:

5. Animated Circle SVG Hover Effect

In this fifth example, we’ll walk through a hover effect where an SVG path is animated, encircling our link. The technique we’re going to use isn’t something new; in actual fact, Jake Archibald wrote about it some years ago.

The animation in action

Specify the Page Markup

Once more, we’ll start with an unordered list, but this time we’ll include an SVG nested inside each link:

The SVG will be an ellipse and represented via the path element. We’ll give it preserveAspectRatio="none" because, as we’ll see in a moment, it should cover the full parent dimensions, even if it’s stretched.

Specify the Styles

The SVG will be absolutely positioned and centered within its container. Plus, as mentioned above, it will be big enough to cover the anchor text:

The path element will have a stroke and its fill color will be transparent:

To animate the path, we’ll need two additional properties. Specifically, the stroke-dasharray and stroke-dashoffset properties. 

The stroke-dasharray property is responsible for creating dashes/gaps in our stroke. For example, by setting stroke-dasharray: 50 to the path, it will look as follows:

The svg with dashes around it

The stroke-dashoffset property is responsible for changing the position (offsetting) of those dashes.

So here’s the trick:

  • First, the value of the stroke-dasharray property should match the length of the path. Instead of guessing it, JavaScript provides a handy way for grabbing it through the getTotalLength() method. 
  • Next, we’ll also assign the resulting value (or its negative one for a reversed effect) to the stroke-dashoffset property. This neat combination will hide the SVG. 
  • Then, each time we hover over a link, we’ll run an animation that will move the stroke-dashoffset property value to 0. As a result of that, the path will be gradually “drawn”.

With all the above in mind, let’s first retrieve the path’s length (just for the first path) via JavaScript:

Then, we’ll assign this value to the corresponding properties:

Lastly, as an improvement, instead of hard coding the stroke-dasharray and stroke-dashoffset values, we could have specified them dynamically through JavaScript (see if you can do that bit yourself!).

Here’s the final demo, which gives us a cool chalkboard hover effect:

6. SVG Wavy UnderLine Hover Effect

Now that we know the basic principles behind the SVG path animation, let’s have a quick look at two other very similar examples. In both examples, there won’t be any need to stretch the SVGs, so we won’t make use of the preserveAspectRatio="none" attribute.

The first one will reveal a wavy underline.

The animation in action

Here’s the demo:

7. Pointing SVG Arrow Hover Effect

The third SVG demo will reveal an Iconmonstr icon (a pointing arrow). 

The animation in action

Here’s the demo for you to reverse engineer:

8. Lottie Animation Hover Effect

In this eighth example, we'll cover a hover effect where a Lottie animation is played infinitely. If you are unfamiliar with this concept, take some time to read this tutorial before continuing.

For this demo that will require some JavaScript, we'll use a free Lottie animation taken from the LottieFiles library and work with the LottieFiles web player. If you haven't used this player before, don't worry. There's also a dedicated tutorial that describes all the steps needed to incorporate it into a project. Be sure to read it first, then come back.

The animation in action

Specify the Page Markup

We’ll start with the usual unordered list. Each of the menu links will contain the same lottie-player custom element:

Notice that we give this element fixed dimensions (140px x 140px). Feel free to make it bigger or smaller depending on your needs.

Specify the Styles

The lottie-player will be absolutely positioned and centered within its container. Plus, it will be hidden by default:

Add the JavaScript

Each time we hover over a link, its associated animation will appear thanks to the visible class and play:

Here’s the related demo:

9. Lottie Animation With Random Color Hover Effect

In this last example, we'll continue our exploration in the Lottie animations world. This time though, some things will change during the hover effect. That said, the animation will appear yet also receive a random color coming from an array of colors.

Again, for this demo that will require some JavaScript, we'll use a free Lottie animation taken from the LottieFiles library and work with the LottieFiles web player.

The animation in action

Specify the Page Markup

Once again, we'll start with the usual unordered list and the same lottie-player custom element inside each link:

However, this time our animation won't contain fixed dimensions. It should cover the full parent dimensions, even if it’s stretched. That's the reason why we give preserveAspectRatio="none" to the custom element. 

Specify the Styles

The lottie-player will be absolutely positioned and hidden by default:

Add the JavaScript

Each time we hover over a link, its associated animation will appear thanks to the visible class and play. Additionally, the animation will receive a random color selection from the arrayOfColors array:

Tip: remember that the lottie-player is a custom element. To change its color, we have to modify the fill value of its path element. But, we cannot directly target this element by using JavaScript's standard traversing methods. To gain this ability, we have first to target the player's contents by using the ShadowRoot interface and more specifically its shadowRoot property. 

Here’s the related demo:

Conclusion

That concludes another tutorial, folks! It was quite a long journey, but I hope that you enjoyed it and have learned some new things which you will include in your front-end toolkit.

Last but not least, I have added all the demos of this tutorial to a CodePen Collection. Be sure to check it out and give it some love!

I’d love to see your favorite CSS hover effects. They can come from CodePen, an inspirational website, or somewhere else. Don’t forget to share them with us!

As always, thanks a lot for reading!

More CSS and JS Hover Effect Tutorials

Learn how to create even more interesting CSS hover effects with these Tuts+ tutorials:


This content originally appeared on Envato Tuts+ Tutorials and was authored by George Martsoukos


Print Share Comment Cite Upload Translate Updates
APA

George Martsoukos | Sciencx (2021-05-24T08:44:09+00:00) How to Create 9 Different CSS Hover Effects From Scratch. Retrieved from https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/

MLA
" » How to Create 9 Different CSS Hover Effects From Scratch." George Martsoukos | Sciencx - Monday May 24, 2021, https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/
HARVARD
George Martsoukos | Sciencx Monday May 24, 2021 » How to Create 9 Different CSS Hover Effects From Scratch., viewed ,<https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/>
VANCOUVER
George Martsoukos | Sciencx - » How to Create 9 Different CSS Hover Effects From Scratch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/
CHICAGO
" » How to Create 9 Different CSS Hover Effects From Scratch." George Martsoukos | Sciencx - Accessed . https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/
IEEE
" » How to Create 9 Different CSS Hover Effects From Scratch." George Martsoukos | Sciencx [Online]. Available: https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/. [Accessed: ]
rf:citation
» How to Create 9 Different CSS Hover Effects From Scratch | George Martsoukos | Sciencx | https://www.scien.cx/2021/05/24/how-to-create-9-different-css-hover-effects-from-scratch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.