Jordan Baron Jordan Baron - 2 months ago 9
HTML Question

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!

Answer

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>