JMay
JMay

Reputation: 361

css is hover not working on class

I'm trying to get a bar to transition in when the li is hovered over however the hover seems to be ignored and nothing happens. If I instead put 'nav ul:hover', it works but the bar pops in under all the li's.

<nav>
  <ul>
    <li class="nav-li">All Departments</li>
    <li class="nav-li">Shop by Room</li>
    <li class="nav-li">DIY Projects & Ideas</li>
    <li class="nav-li">Home Services</li>
    <li class="nav-li">Speacials & Offers</li>
    <li class="nav-li">Local Ad</li>
  </ul>
</nav>

CSS

.nav-li {
margin-right: 40px;
display: inline-block;
padding-bottom: 2em;
transition: all .5s ease;
cursor: pointer;
position: relative;
}

.nav-li::after {
content: "";
display: block;
position: absolute;
left: 0;
bottom: 0;
height: 3px;
width: 0;
transition: width .5s ease;
}

.nav-li:hover .nav-li::after {
background-color: #f96302;
width: 100%;
}

Upvotes: 2

Views: 777

Answers (2)

Blackmagyk
Blackmagyk

Reputation: 80

Try combining the two.

.nav-li:hover::after

Example: https://jsfiddle.net/pc26LnLz/1/

Upvotes: 1

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195972

The .nav-li:hover .nav-li::after should be .nav-li:hover::after

.nav-li {
  margin-right: 40px;
  display: inline-block;
  padding-bottom: 2em;
  transition: all .5s ease;
  cursor: pointer;
  position: relative;
}

.nav-li::after {
  content: "";
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 0;
  transition: width .5s ease;
}

.nav-li:hover::after {
  background-color: #f96302;
  width: 100%;
}
<nav>
  <ul>
    <li class="nav-li">All Departments</li>
    <li class="nav-li">Shop by Room</li>
    <li class="nav-li">DIY Projects & Ideas</li>
    <li class="nav-li">Home Services</li>
    <li class="nav-li">Speacials & Offers</li>
    <li class="nav-li">Local Ad</li>
  </ul>
</nav>

Upvotes: 2

Related Questions