user6161942
user6161942

Reputation:

CSS - Changing background on hover not working

I am trying to use CSS so when you hover on something it changes background colors. The code I am using does not work though. I can't seem to find out why though. It should work, right?

body {
    margin: 0;
    font-family: Arial;
    font-size: 1em;
}

.navbar-ul, a {
    margin: 0;
    color: white;
    overflow: hidden;
}

li, a {
    text-decoration: none;
    display: inline-block;
    padding: 10px;
    background: black;
}

li a :hover {
    background-color: blue;
}

li {
    list-style-type: none;
    float: left;
}
<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>Dark Website Template by Jordan Baron</title>
        <link rel="stylesheet" href="styles-main.css">
    </head>
    
    <body>
        
        <div class="navbar">
            <ul class="navbar-ul">
                <li><a href="#">HOME</a></li>
                <li><a href="#">CONTACT</a></li>
                <li><a href="#">ABOUT</a></li>
            </ul>
        </div>
        
    </body>
    
</html>

Please help!

Upvotes: 2

Views: 43

Answers (3)

Razia sultana
Razia sultana

Reputation: 2211


* {
margin: 0;
padding: 0; 
} 

.wrapper {
padding-top: 10px;
width: 320px;
margin: 0 auto;
overflow: hidden;
 }
.menu {
background: #093;
} 
.menu ul { 
margin-left: 0; 
list-style: none;
text-align: center; 
} 
.menu ul li {
display: inline-block;
 }


.menu ul li a { 
display: block;
padding: 10px;
color: #CC0; 
text-decoration: none;
 } 
.menu ul li a:hover { 
background: #C30;
color: #FFF; 
} 

<div class="wrapper">
  <div class="menu"> 
   <ul>
    <li><a href="">HOME</a>  
     </li> 
      <li><a href="">CONTACT</a> 
      </li>
     <li><a href="">ABOUT</a> 
   </li>
  </ul>
</div>  

Upvotes: 1

UncaughtTypeError
UncaughtTypeError

Reputation: 8712

Adjust your on-hover rule slightly and consider the a element as well:

li:hover, li:hover a {
    background: blue;
}

body {
    margin: 0;
    font-family: Arial;
    font-size: 1em;
}

.navbar-ul, a {
    margin: 0;
    color: white;
    overflow: hidden;
}

li, a {
    text-decoration: none;
    display: inline-block;
    padding: 10px;
    background: black;
    transition: .7s;
}

li:hover, li:hover a {
    background: blue;
}

li {
    list-style-type: none;
    float: left;
}
<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>Dark Website Template by Jordan Baron</title>
        <link rel="stylesheet" href="styles-main.css">
    </head>
    
    <body>
        
        <div class="navbar">
            <ul class="navbar-ul">
                <li><a href="#">HOME</a></li>
                <li><a href="#">CONTACT</a></li>
                <li><a href="#">ABOUT</a></li>
            </ul>
        </div>
        
    </body>
    
</html>

Upvotes: 1

Yash Jain
Yash Jain

Reputation: 1

You have a VERY simple mistake.

When you have this code...

li a: hover

remove the space between a and :hover and see the magic.

Now, it should look like this.

li a:hover

@Joseph Young mentioned this, don't forget to upvote his comment.

body {
    margin: 0;
    font-family: Arial;
    font-size: 1em;
}

.navbar-ul, a {
    margin: 0;
    color: white;
    overflow: hidden;
}

li, a {
    text-decoration: none;
    display: inline-block;
    padding: 10px;
    background: black;
}

li a:hover {
    background-color: blue;
}

li {
    list-style-type: none;
    float: left;
}
<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>Dark Website Template by Jordan Baron</title>
        <link rel="stylesheet" href="styles-main.css">
    </head>
    
    <body>
        
        <div class="navbar">
            <ul class="navbar-ul">
                <li><a href="#">HOME</a></li>
                <li><a href="#">CONTACT</a></li>
                <li><a href="#">ABOUT</a></li>
            </ul>
        </div>
        
    </body>
    
</html>

Upvotes: 1

Related Questions