Uhkam
Uhkam

Reputation: 19

CSS hover in a nav bar

I have tried to make a nav bar using the ul li and disabling the text formatting in CSS. The code works almost fine, as I have made also a small fade-in animation for the buttons. The problem appears if I have a dropdown menu in my nav bar. Hovering my mouse over the li which has dropdown in it also causes the things in the dropdown to be animated.

I know there may be some selectors you can use to make it not do so, but so far I haven't got it to work. What would be the right thing to do?

Here's the HTML:

<nav>
    <ul>

        <a href="index.html"><li id="left">Home</li></a>
        <a href="#"><li>Menu 1</li></a>

        <li><span>Dropdown</span>
            <ul>
                <a href="#"><li>Dropdown menu 1</li></a>
                <a href="#"><li>Dropdown menu 2</li></a>
            </ul>
        </li>

        <a href="#"><li>Menu 3</li></a>
        <a href="#"><li id="right">Contact me</li></a>

    </ul>

</nav>

Here's the CSS:

nav{
  display: inline;
  float: left;
  background-color: #d48041;
  box-shadow: 0 1vh 2vh -0.5vh #F2B57B inset;
  width:90%;
  padding-left:10%;
  height:5%;
  font-weight: bold;
}

nav ul{
  list-style: none;
  margin:0;
  padding:0;
  display: inline;
}

nav ul a{
  text-decoration: none;
  color: black;
  font-size: 1.5vh;
}

nav ul a li{
  display:inline-block;
  float:left;
  text-align: center;
  height: 5vh;
  line-height: 5vh;
  width:10%;
  margin:0;
  box-sizing: border-box;
}

nav ul li ul{
  display:none;
}

nav ul li:hover ul{
  display:block;
  position: absolute;
  z-index: 1;
  width: 91%;
  margin-left: -0.1vw;
}

nav ul li ul li{
  background-color: #FFA968;
  border: solid 0.04vw black;
}

nav ul a li:not(.noborder), nav ul li {
  border-right: solid 0.04vw black;
}

#left {
  border-left: solid 0.04vw black;
}

#right {
  float: right;
  width: 20%;
  margin-right:11.1%;
  border-left: solid 0.04vw black;
}

nav ul a:hover{
  animation-name: text_fadein;
  animation-duration: 0.3s;
  animation-fill-mode: forwards;
}

nav ul li:hover span{
  cursor: default;
}

nav ul a:hover li{
  animation-name: menu_fadein;
  animation-duration: 0.3s;
  animation-fill-mode: forwards;
}

Upvotes: 1

Views: 7975

Answers (1)

Div_P
Div_P

Reputation: 179

To prevent animation getting applied to children use > selector to select the immediate child.

nav ul>a:hover{
  animation-name: text_fadein;
  animation-duration: 0.3s;
  animation-fill-mode: forwards;
}

nav ul>li:hover span{
  cursor: default;
}

nav ul>a:hover>li{
  animation-name: menu_fadein;
  animation-duration: 0.3s;
  animation-fill-mode: forwards;
}

Upvotes: 7

Related Questions