How to Add Underline Hover Effect to Divi Menu with CSS

Creating Underline Hover Effect for Divi Menu Module with CSS: A Step-by-Step Tutorial

You can add fun animated hover effects to the Divi Menu with CSS. It’s easier than you think and so fun to experiment with! In this tutorial, I’ll walk you step-by-step through the process, showing you how to create this effect on your own – and how to customize it so it can suit the style of your website and look fantastic.

If you’re a bit intimidated by the hands-on approach to CSS in this one, you can check out my previous tutorial that allows you to achieve 7 variations of this effect with easy copy-and-paste snippets here. But I really hope you will join me in this fun adventure! If you think of yourself as a CSS beginner, though, and want to feel more confident, I encourage you to check out our free CSS in Divi guide.

Make sure you watch the video – and let’s get started!

Free Resources for Divi Lovers

Join our newsletter to get the good stuff!

We’ll provide you with updates on new tutorials, webdesign assets and special offers. Get top quality Divi goodies straight into your inbox! Read our Privacy Policy to learn how we manage and protect your submitted information.

Sign up to download

Get access to this layout and all the free resources made exclusively for our subscribers!

Already subscribed?

Fill the signup form and if you’re on the list it’ll get you straight to the download page!


Can’t see the form? Click here

The hover effect looks fun and easy – and it is also really easy to achieve. We’re going to use CSS pseudo-elements to add this effect and manipulate it using a few different CSS properties. First, we’ll have to find the right CSS selector for the task, then decide what our links should look like, and finally – how to achieve that animated hover effect. Hopefully, this explanation will make it clear how to do this – and also, how you can manipulate this effect in any way you want.

Custom CSS Step-by-step

CSS :before pseudo-element with Divi Menu links

CSS pseudo-elements work as if you had added a whole new HTML element into the markup, rather than applying styling to existing elements. We can use it to add any element next to our Menu links. Next, we can edit the appearance of the pseudo-element with any CSS properties!

Here’s the CSS selector I used to add the :before pseudo-element to all links inside the menu module with CSS class of dl-menu:

/* This targets all links within a container */

.dl-menu a:before {
    content:'Sample Text';
}

The problem with this selector is that it is too generic – it would target the Logo link, too. Here’s a more specific selector that specifies two parent containers: dl-menu CSS class added to the Menu module and an unordered list with a CSS class of et-menu:

/* This targets all links within a menu list inside the container */

.dl-menu ul.et-menu a:before {
    content:'Sample Text';
}

And to limit it further, we can make sure to only target first-level links and not sub-menu items:

/* This targets first-level links within a menu list inside the container */

.dl-menu ul.et-menu > li > a:before {
    content:'Sample Text';
}

Now, instead of “Sample text” we can specify the position and the appearance of the pseudo-element. The CSS code below shows the 5px underline below the Menu text:

.dl-menu ul.et-menu > li > a:before {
    content: '';
    width: 110%;
    left: -5%;
    height: 5px;
    border-radius: 3px;
    background: lightsalmon;
    position: absolute;
    z-index: -1;
    bottom: 20px;
}

Next, we need to decide on the apprearance of the pseudo-element and separate the styling for the initial state and for the hover state:

/* The Initial State */
.dl-menu ul.et-menu > li > a:before {
    content: '';
    width: 0;
    left: 50%;
    height: 5px;
    border-radius: 3px;
    background: lightsalmon;
    position: absolute;
    z-index: -1;
    bottom: 20px;
    opacity: 0;
}

/* The Hover State */
.dl-menu ul.et-menu > li > a:hover:before {
    width: 110%;
    left: -5%;
    opacity: 1;
}

To make sure the transition beetween the initial and the hover state is not abrupt, we have to add a transition property. We can use the Cubic Bezier generator to generate custom timing function: transition: .3s cubic-bezier(.25,.01,.32,1.61);

And to display the underline on the current active page, we can use WordPress-generated custom CSS classes. If one of the menu links targets the currently viewed URL – it gets additional CSS class of current-menu-item. We can use that class to add a second target that needs to look the same as the hover state. The final CSS code used in the example looks like this:

.dl-menu ul.et-menu > li > a:before {
    content: '';
    width: 0;
    left: 50%;
    height: 5px;
    border-radius: 3px;
    background: lightsalmon;
    position: absolute;
    z-index: -1;
    bottom: 20px;
    opacity: 0;
    transition: .3s cubic-bezier(.25,.01,.32,1.61);
}

.dl-menu ul.et-menu > li > a:hover:before,
.dl-menu ul.et-menu > li.current-menu-item > a:before  {
    width: 110%;
    left: -5%;
    opacity: 1;
}

.dl-menu ul.et-menu > li > a {
    z-index: 1;
}
That’s it!

Enjoy your new fun hover effect!

Feel free to copy the code snippets – or write your own and play with the possibilities! There is a lot you can do to add your own spin to this simple hover effect. I invite you to share your results in the Divi Lovers Facebook group, because I’m really curious what you came up with!

If you enjoyed this tutorial and would like to see more content like this, tell me in the comments below. Thank you for watching and reading!

Shares

Have you seen our

Divi child themes?

Divi Toolbox plugin?

We offer a great selection of high-quality Divi products to speed up your work. Feel free to use a coupon code IREADBLOG at the checkout to get a 10% discount!

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Enjoing our content?

You might find this interesting…

License Details

REGULAR LICENSE

Single Site

A regular license allows an item to be used in one project for either personal or commercial use by you or on behalf of a client. The item cannot be offered for resell either on its own or as a part of a project. Distribution of source files is not permitted.

EXTENDED LICENSE

Unlimited Sites

An extended license allows an item to be used in unlimited projects for either personal or commercial use. The item cannot be offered for resell either on its own or as a part of a project. Distribution of source files is not permitted.

SUPPORT & UPDATES

Each license is a one-time payment. There are no annual fees. You get lifetime access to product updates. Support is provided for 6 months from the date of purchase.

Server Requirements

SERVER SETTINGS:

  • PHP 7.2 or later
  • upload_max_filesize (256M)
  • max_input_time (300)
  • memory_limit (256M)
  • max_execution_time (300)
  • post_max_size (512M)

PHP.INI SETTINGS:

  • php-xml or/and php-dom
  • XMLReader
  • PHP CURL module

PHP MODULES:

  • allow_url_fopen

Would you like to...

Consult Ania and fellow students?

Join the conversation inside our private FB group.

Would you like to...

Get support privately?

Submit a support ticket via your account page.

Ask Ania?

Submit a question for the next live Q&A session.

Consult fellow students?

Join the conversation inside our private FB group.