Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to change li Background Color on Hover

Tags:

css

Can you please take a look at this link and let me know why I am not able to change the background color of the li on hover?

<ul class="inline">
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>3</li>
  <li>3</li>
</ul>

Css:

.inline li{
    width:18% !important;
    background:#FFF  !important;
}
.inline li: hover{
    background:#A5A5A5  !important;
}
like image 945
Mona Coder Avatar asked Aug 26 '13 17:08

Mona Coder


People also ask

How do you change color when hovering?

Changing link color on hover using CSS To change the color of your link on hover, use the :hover pseudo property on the link's class and give it a different color.

How can I change background of image on hover?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

How do I hover color in HTML?

If you want to change the link color when moving the mouse over a link, you only need the A:hover line. hover - The hover option is the color that the text changes to when the mouse is over the link. In this example, the link changes to a blue color when a mouse cursor is hovering over a link.

What is Li a hover in CSS?

The :hover CSS pseudo-class matches when the user interacts with an element with a pointing device, but does not necessarily activate it. It is generally triggered when the user hovers over an element with the cursor (mouse pointer).


2 Answers

You have an extra space before hover.

.inline li:hover{
    background:#A5A5A5  !important;
}
like image 86
Kris Hollenbeck Avatar answered Oct 19 '22 17:10

Kris Hollenbeck


The space between li and :hover is valid CSS, but not in this case. Using li :hover will apply styles when you hover over any descendant of the li. What you are using is invalid CSS. You can't have a colon between an element and a pseudo-class. So by using li:hover, you are specifiying the styles when the li is being hovered over.

I would also recommend that you not use !important, because it can cause some problems later down the road. Use more specific DOM selectors, like ul.inline li:hover.

Fiddle

like image 37
Cody Guldner Avatar answered Oct 19 '22 16:10

Cody Guldner