skip to Main Content

I have this CSS on my WordPress website that will change the font-size to 2em of the main navigation menu items when hovering. What I need is to make it so that it remains with that font-size while going to the sub-items. Is there a way to achieve this inside of WordPress?

.menu a:hover {
font-size:2em !important;
transition-duration: all .2s !important;
}

That is the code I’m using right now, and I added the main menu items that .menu class.

I gave the sub-items a .submenu-item class. So what I researched is that using something like this could do the trick:

.submenu-item:hover + .menu-about {
font-size:2em !important;
transition-duration: 0.05s;
}

But it doesn’t. I’ve tried using > and ~ and neither of those worked. Is there a way to do it?

Thanks a lot in advance!

2

Answers


  1. I have checked your site and hope this CSS will work for you.

    .hfe-nav-menu > li:hover > a {
    font-size: 2em;
    }
    

    And make sure you do not add !important if it is not override with other css.

    Login or Signup to reply.
  2. According to this question it is not recommended or even possible to access the parent element with only CSS. You can do that with JavaScript as the codes below:

    let mySubmenu = document.getElementById("mySubmenu");
    let aboutTag = document.getElementById("aboutTag");
    
    aboutTag.onmouseenter = function () {
        aboutTag.style.fontSize = "2em";
    }
    
    aboutTag.onmouseleave = function () {
        aboutTag.style.fontSize = "1.2em";
    }
    
    mySubmenu.onmouseenter = function () {
        aboutTag.style.fontSize = "2em";
    }
    
    mySubmenu.onmouseleave = function () {
        aboutTag.style.fontSize = "1.2em";
    }
    * , *::after, *::before {
        margin: 0;
        padding: 0;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }
    
    .nav-menu {
        display: flex;
        align-items: center;
        justify-content: space-around;
        margin-top: 25px;
    }
    
    .menu-item {
        list-style: none;
        color: black;
        border: none;
    }
    
    .sub-menu {
        list-style: none;
        display: none;
        position: absolute;
        background-color: #f1f1f1;
        min-width: 160px;
        box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
        z-index: 1;
    }
    
    .menu-about {
        position: relative;
    }
    
    .menu-about:hover .sub-menu {
        display: block;
    }
    
    .submenu-item {
        padding: 10px 5px;
        transition: all .5s;
    }
    
    .submenu-item:hover {
        background-color: wheat;
    }
    
    .menu-item-text {
        font-size: 1.2em;
        transition: all .2s;
    }
    
    .menu-item-text:hover {
        font-size: 2em;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>hover effect</title>
        <link rel="stylesheet" href="styles.css">
    </head>
    <body style="background-color: #e3e3e3;">
    
    <main>
        <nav>
            <ul class="nav-menu">
                <li class="menu-about menu-item">
                    <div class="submenu-container">
                        <a id="aboutTag" href="#" class="menu-item-text">About
                        </a>
                    </div>
                    <ul id="mySubmenu" class="sub-menu">
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">Mission</a>
                        </li>
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">Services</a>
                        </li>
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">USP</a>
                        </li>
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">Pricing</a>
                        </li>
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">Clients</a>
                        </li>
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">Team</a>
                        </li>
                        <li class="submenu-item">
                            <a href="#" class="sub-menu-item">Partners</a>
                        </li>
                    </ul>
                </li>
    
                <li class="menu menu-item">
                    <a href="#" class="menu-item-text">Blog</a>
                </li>
    
                <li class="menu menu-item">
                    <a href="#" class="menu-item-text">Hub</a>
                </li>
    
                <li class="menu menu-item">
                    <a href="#" class="menu-item-text">Career</a>
                </li>
            </ul>
        </nav>
    </main>
    
    <script src="myCode.js"></script>
    </body>
    </html>

    I don’t know how exactly you could add that codes to your WordPress codes (I’m not very professional in WordPress). But if you search, maybe found the way. Also for the last tip: it is not correct to say transition-duration: all .2s (that you mentioned in the question), the correct way is transition-duration: .2s or transition: all .2s.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search