1

I want to hover the li but it is not working, I did this before and it worked but I don't know why it is not working now I set background color for li, I want to when it is hover it change and another question, I set opacity for footer but it affected li background color too!

body {
  background-image: url(background.jpg);
  background-repeat: no-repeat;
}

footer {
  width: 1350px;
  height: 200px;
  background-color: dimgray;
  border-radius: 10px;
  opacity: 0.7;
  position: absolute;
}

#li1 {
  display: inline-block;
  list-style-type: none;
  margin: 2px;
  padding: 20px;
  background-color: aqua;
  width: 100px;
  font-family: Calibri;
  font-weight: 700;
}

#ul1 {
  margin-left: 50px;
  margin-top: 60px;
  position: relative;
}

a {
  text-decoration: none;
  color: black;
  display: block;
  font-size: 20px;
  text-align: center;
}

li:hover {
  background-color: chartreuse;
}
<footer>
  <ul id="ul1">
    <li id="li1"><a href="##">Home</a></li>
    <li id="li1"><a href="##">Categories</a></li>
    <li id="li1"><a href="##">languages</a></li>
    <li id="li1"><a href="##">Contries</a></li>
    <li id="li1"><a href="##">Cities</a></li>
    <li id="li1"><a href="##">Planets</a></li>
    <li id="li1"><a href="##">Seas</a></li>
    <li id="li1"><a href="##">Deserts</a></li>
  </ul>
</footer>
Ivar
  • 4,655
  • 12
  • 45
  • 50
Nima Bahar
  • 13
  • 6

3 Answers3

0

It's to do with how css is prioritised. You give all your lis the same id - bad idea. Make it a class instead, or (as I have done in the snippet), give them neither a class or id, and style using "#ul1 li".

You'll see in the snippet below the hover works:

body{

        background-image: url(background.jpg);
        background-repeat: no-repeat;
    }
    footer{

        width: 1350px;
        height: 200px;
        background-color:dimgray;
        border-radius: 10px;
        opacity: 0.7;

        position: absolute;
    }
    #ul1 li{
        display:inline-block;
        list-style-type: none;
        margin: 2px;
        padding: 20px;
        background-color:aqua;
        width: 100px;
        font-family:Calibri;
        font-weight: 700;

    }
    #ul1{
        margin-left: 50px;
        margin-top: 60px;
        position: relative;
    }
    a{
        text-decoration: none;
        color: black;
         display: block;
        font-size: 20px;
        text-align: center;


    }
    #ul1 li:hover{
        background-color:chartreuse;
    }
<footer>
<ul id="ul1">
    <li><a href="##">Home</a></li>
    <li><a href="##">Categories</a></li>
    <li><a href="##">languages</a></li>
    <li><a href="##">Contries</a></li>
    <li><a href="##">Cities</a></li>
    <li><a href="##">Planets</a></li>
    <li><a href="##">Seas</a></li>
    <li><a href="##">Deserts</a></li>

    </ul>

</footer>
Michael Beeson
  • 2,265
  • 2
  • 15
  • 23
0

Use

li:hover {
  background-color: chartreuse !important;
}

To override your

background-color: aqua;
  • thank you, it worked but what is the reason? I did this before the same way and it just worked – Nima Bahar Jun 04 '18 at 14:45
  • @NimaBahar It's because CSS is prioritised you set the background to aqua before the hover so you need to "force" the change with the `!important` to make it override your previous background. (Please accept my answer if this help you) – Dessauges Antoine Jun 04 '18 at 14:47
  • thank you @DessaugesAntoine your solution is possible too but the mistake was from setting same id to all li and it solved, anyway thank you. – Nima Bahar Jun 04 '18 at 14:55
0

You cant set same id for multi elements. And im not sure what html do if you set ## as href in tag

Change id attr to class attr in li like this:

Class="li1"

Its hover will work

mohsen solhnia
  • 316
  • 2
  • 14