Skip to content

Animated Underline on Hover with CSS

Adding an animated underline on hover to links using CSS is a popular modern technique that likely has a few different solutions. The one in this snippet animates the underline by means of a gradient, various background-related properties, and a pseudo-element.

This can also be done using an extra <span> element inside the link (instead of the pseudo-element), but using a pseudo-element keeps the HTML clean.

Below is the code. Note the comments, which explain the customizable parts:

a, a:link, a:visited {
  color: white;
  text-decoration: none;
  position: relative;
  padding-bottom: 4px; /* The distance between text and underline */

a::before {
  content: "";
  position: absolute;
  width: 100%;
  top: 0;
  bottom: 0;
  background-image: linear-gradient(90deg, white, white); /* underline color */
  background-size: 0 2px; /* vertical size of underline */
  background-repeat: no-repeat;
  background-position: left bottom; /* start position of underline */
  transition: background-size .3s ease-in; /* duration and timing style of animation */

a:hover::before {
  background-size: 100% 2px;
Code language: CSS (css)

Notice the use of absolute positioning, along with the top and bottom properties to ensure the pseudo-element expands to the height of its parent. You can view the above code in action in the following CodePen demo:

The look and animation of the underline is fairly customizable. For example, in the following CodePen demo, I’ve adjusted some of the customizable values to make the underline a little more unique:

Above I’ve modified the direction of the animated underline on hover along with the spacing and the size of the underline. I’ve also used a real two-color gradient instead of a solid colo for the animated underline.

Note: To the best of our knowledge, the information above and the snippet are accurate and up to date. However, in case you notice something wrong, please report snippet or leave a comment below.
View all Snippets
Louis Lazaris

Inline Feedbacks
View all comments

Or start the conversation in our Facebook group for WordPress professionals. Find answers, share tips, and get help from other WordPress experts. Join now (it’s free)!

Would love your thoughts, please comment.x

Most Searched Articles

Best JavaScript Libraries and Frameworks: Try These 14 in 2024

In this post, we look at the best JavaScript libraries and frameworks to try out this year. Why? Well, with JavaScript being available in every web browser, this makes it the most accessible programming language of ...

20 Best Free WordPress Themes for 2024 (Responsive, Mobile-Ready, Beautiful)

If you're looking for only the best free WordPress themes in the market for this year, then you're in the right place. We have more than enough such themes for you right ...

12 Best WordPress Hosting Providers of 2024 Compared and Tested

Looking for the best WordPress hosting that you can actually afford? We did the testing for you. Here are 10+ best hosts on the market ...

Handpicked Articles

How to Make a WordPress Website: Ultimate Guide for All Users – Beginners, Intermediate, Advanced

Many people wonder how to make a WordPress website. They’ve heard about WordPress, its incredible popularity, excellent features and designs, and now they want to join the pack and build a WordPress website of their own. So, where does one get ...

How to Start an Ecommerce Business: Ultimate Guide for 2024

Is this going to be the year you learn how to start an eCommerce business from scratch? You’re certainly in the right place! This guide will give you a roadmap to getting from 0 to a fully functional eCommerce business. ...