Site logo

Archived topic

Main Menu Hover border the same width as submenu container

5 replies · Started by Maria Rowena on May 22, 2021

Viewing posts 1–6 of 6

Hi support team,

I managed to create a main menu hover effect by using this css that I found in this forum.

@media (min-width: 769px) {
    .main-navigation .menu > .menu-item > a::after {
        content: "";
        position: absolute;
        right: 12px;
        left: 12px;
        bottom: 0;
        display: block;
        height: 5px;
        background-color: #d5db12;
        opacity: 0;
        transition: opacity 0.2s;
    }
    .main-navigation .menu > .menu-item.current-menu-item > a::after,
    .main-navigation .menu > .menu-item.current-menu-ancestor > a::after,
    .main-navigation .menu > .menu-item > a:hover::after {
        opacity: 1;
    }
}

I already modofied that code my only problem is, I cannot make the width of the hover border the same width as the submenu container. I want to achieve this. https://snipboard.io/rSkmV3.jpg

So far this is what I got-->https://snipboard.io/vNUow7.jpg

I hope you can help me with this. Thank you so much.

Hi there,

can you share a link to your site ?

Hi David,

The website is still under construction but you can visit the link I attached.

Thank you so much. I hope to hear from you again. :)

Hi Maria,

Try change this part of CSS:

 .main-navigation .menu > .menu-item > a::after {
        content: "";
        position: absolute;
        right: 12px;
        left: 12px;
        bottom: 0;
        display: block;
        height: 5px;
        background-color: #d5db12;
        opacity: 0;
        transition: opacity 0.2s;
    }

to:

.main-navigation .menu > .menu-item > a::after {
    content: "";
    position: absolute;
    bottom: 0;
    display: block;
    height: 2px;
    background-color: #000000;
    opacity: 0;
    transition: opacity 0.2s;
    width: 200px; /*your submenu width*/
    left: 0;
}

Let me know :)

Hi Ying,

Thank you so much for this!

It's working well.

I don't know if this is too much but,

Do you think it's possible to apply this css to main menu item that has submenu item only?

Thank you so much. I really appreciate.

Hi there,

I don’t know if this is too much but,

Do you think it’s possible to apply this css to main menu item that has submenu item only?

Thank you so much. I really appreciate.

The best practice is to add CSS classes to the menu you want this applied on.

WordPress let's you add CSS classes to menu items so we can specific menu items. See this - https://share.getcloudapp.com/rRujXWO2

With this, we can specifically target menu items you've specifically added classes on.

Example: Say, you've added your-menu-item-class on specific menu items.

.main-navigation .menu > .menu-item.your-menu-item-class > a::after {
    content: "";
    position: absolute;
    bottom: 0;
    display: block;
    height: 2px;
    background-color: #000000;
    opacity: 0;
    transition: opacity 0.2s;
    width: 200px; /*your submenu width*/
    left: 0;
}
This archived topic is closed to new replies.