2

I've made a nav bar and I'd like to use the hover selector only on the pages that aren't active. So I used the selector a:not(.active):hover but it doesn't work. I'd really appreciate if someone could help me.

ul 
{
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    display:block;
    position:absolute;
    top:-2px;
    left:0;
    right: 0;
    background-color: darkred;
}

li 
{
    float: left;
}

li a 
{
    display: block;
    color: white;
    text-align: center;
    padding: 20px 23px;
    text-decoration: none;
}

li a:not(.active):hover
{
    background-color: #B22222;
}

.active {
    background-color: #470005;
}
<ul>
                <li class="active"><a href="home.html">Home</a></li>
                <li><a href="#about">About me</a></li>
                <li><a href="#contacts">Contacts</a></li>
                <li><a href="#help">Help</a></li>
                <li><a href="#other">Other Works</a></li>
                <li><a href="#news">News</a></li>
</ul>
Jasmine
  • 117
  • 10

4 Answers4

1

Your :not pseudo-class is on your link. However, the active class is on the li.

li:not(.active) a:hover should work

JSfiddle Example: https://jsfiddle.net/ubntkk46/

Jennifer Goncalves
  • 1,442
  • 1
  • 12
  • 29
Axnyff
  • 9,213
  • 4
  • 33
  • 37
0

This Fix It :

li:not(.active):hover a {

}

Full Code:

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  display:block;
  position:absolute;
  top:-2px;
  left:0;
  right: 0;
  background-color: darkred;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 20px 23px;
  text-decoration: none;
}

li:not(.active):hover a {
  background-color: #B22222;
}

.active {
  background-color: #470005;
}
<ul>
  <li class="active"><a href="home.html">Home</a></li>
  <li><a href="#about">About me</a></li>
  <li><a href="#contacts">Contacts</a></li>
  <li><a href="#help">Help</a></li>
  <li><a href="#other">Other Works</a></li>
   <li><a href="#news">News</a></li>
</ul>
Ehsan
  • 12,655
  • 3
  • 25
  • 44
0

The class is linked to your li element, not the hyperlink

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  display: block;
  position: absolute;
  top: -2px;
  left: 0;
  right: 0;
  background-color: darkred;
}

li {
  float: left;
}

li a {
  display: block;
  color: white;
  text-align: center;
  padding: 20px 23px;
  text-decoration: none;
}

li:not(.active) a:hover {
  background-color: #B22222;
}

.active {
  background-color: #470005;
}
<ul>
  <li class="active"><a href="home.html">Home</a></li>
  <li><a href="#about">About me</a></li>
  <li><a href="#contacts">Contacts</a></li>
  <li><a href="#help">Help</a></li>
  <li><a href="#other">Other Works</a></li>
  <li><a href="#news">News</a></li>
</ul>
Gerard
  • 15,418
  • 5
  • 30
  • 52
0

You could just apply the same background colour to the active a tag and the active a tags hover state, and a different background colour to the the non active a tags hover states. This would allow it to work in IE8, because :not isn't supported in older versions of IE.

ul 
{
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    display:block;
    position:absolute;
    top:-2px;
    left:0;
    right: 0;
    background-color: darkred;
}

li 
{
    float: left;
}

li a 
{
    display: block;
    color: white;
    text-align: center;
    padding: 20px 23px;
    text-decoration: none;
}

li a:hover {
    background-color: #B22222;
}

li.active a, li.active a:hover {
    background-color: #470005;
}
<ul>
  <li class="active"><a href="home.html">Home</a></li>
  <li><a href="#about">About me</a></li>
  <li><a href="#contacts">Contacts</a></li>
  <li><a href="#help">Help</a></li>
  <li><a href="#other">Other Works</a></li>
  <li><a href="#news">News</a></li>
</ul>
WizardCoder
  • 3,353
  • 9
  • 20