How to limit formatting of navigation menu to just the main menu and NOT to the items in the drop-downs. HTML, CSS-ThrowExceptions

Exception or error:

Hi, if you look at the above screenshot you can see I have added in code to make a small bar transition across when hovering over items in the navigation bar.

However, this bar also appears when I hover over items in the drop down menu. For example, when hovering over “Stock 1” the bar appears and obviously looks awful.

How can I make it so that this bar only appears on the items in the main menu and NOT in the drop down menu’s.

Please see HTML code below:

<header>
    <div class="container-fluid">
        <div class="logo">
                <img src="images/japan_flag.gif" alt="" height="31" width="41">
        </div>
        <div class="container">
            <nav role="navigation">
                <ul class="mainmenu">
                  <li><a href="#">Home</a></li>
                  <li><a href="#">About Us</a></li>
                  <li><a href="#">Stock</a>
                    <ul class="dropdown">
                      <li><a href="#">Stock 1</a></li>
                      <li><a href="#">Stock 2</a></li>
                    </ul>
                  </li>
                  <li><a href="#">Enquiries</a></li>
                  <li><a href="#">Contacts</a></li>
                </ul>
              </nav>
        </div>
        <div id="mobile-menu-wrap"></div>
    </div>
</header>

Please see CSS below:

nav a {
    text-decoration: none;
    text-transform: uppercase;
    font-family: "Oswald", sans-serif;
}
  
nav {
    font-family: monospace;
}
  
nav ul {
    list-style: none;
    margin: 0;
    margin-top: 30px;
    padding-left: 45%;
}
  
nav ul li {
    color: #fff;
    display: block;
    float: left;
    padding: 1rem;
    position: relative;
    text-decoration: none;
    transition-duration: 0.5s;
}
    
nav ul li a {
    color: #fff;
}
  
nav ul li a:hover {
    cursor: pointer;
    color:  rgb(199, 50, 13);
    transition-duration: 0.3s;
}
  
nav ul li ul {
    visibility: hidden;
    background: rgb(199, 50, 13);
    opacity: 0;
    min-width: 100%;
    position: absolute;
    transition: all 0.5s ease;
    margin-top: 1rem;
    display: block;
    min-width: 100%;
    white-space: nowrap;
    z-index: 100;
    padding: 9% 9%;
    text-align: left;
    line-height: 10px;
    padding-top: 8px;
}
  
nav ul li:hover > ul,
nav ul li ul:hover {
    visibility: visible;
    opacity: 1;
    display: block;
}

nav ul li ul li a:hover {
    color: black;
}
  
nav ul li ul li {
    clear: both;
    width: 100%;
}

nav ul li a::after {
    content: '';
    display: block;
    height: 5px;
    background-color: rgb(199, 50, 13);

    position: relative;
    bottom: 0;
    width: 0%;
    transition: all ease-in-out 250ms;
}

nav ul li a:hover::after {
    width:100%;
}

Any help with this would be hugely appreciated as I have tried for ages to research and figure this out. I am very new to HTML, CSS and any sort of coding in general so apologies.

How to solve:

You can give class to <a> on which you want to apply style..
You can do like this.

<header>
<div class="container-fluid">
    <div class="logo">
            <img src="images/japan_flag.gif" alt="" height="31" width="41">
    </div>
    <div class="container">
        <nav role="navigation">
            <ul class="mainmenu">
              <li><a class="abc" href="#">Home</a></li>
              <li><a class="abc" href="#">About Us</a></li>
              <li><a class="abc" href="#">Stock</a>
                <ul class="dropdown">
                  <li><a href="#">Stock 1</a></li>
                  <li><a href="#">Stock 2</a></li>
                </ul>
              </li>
              <li><a href="#">Enquiries</a></li>
              <li><a href="#">Contacts</a></li>
            </ul>
          </nav>
    </div>
    <div id="mobile-menu-wrap"></div>
</div>

And in css file you can do like this:

.abc:hover{
    cursor: pointer;
    color:  rgb(199, 50, 13);
    transition-duration: 0.3s;
}

Leave a Reply

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