0

I have an ordered list:

<ol id="selectable">
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>
</ol>

when I hover over an item I want it to have some sort of highlight therefore I have:

     #selectable li:hover
 {
        /* some style */
     }

with JavaScript I gave those list items the attribute that when clicked they change the style. so if I click on Item 4 for example it will have a red background color.

now how can I disable the hover style if the element happens to be with the style ckicked (red background)?

In other words I don't want :

     #selectable li:hover
 {
        /* some style */
     }

to apply on li that are selected... how can I do that?

3 Answers 3

2

You will need to override the hover style

#selectable li.selectedclass:hover{
    /* overriding styles */
}
Sign up to request clarification or add additional context in comments.

Comments

2

The real CSS3 answer to this is the :not() selector.

You'd change your existing CSS to the following:

#selectable li:hover:not(#selecteditem)
{
  /* some style */
}

Unfortunately however, this answer is probably not the one you want, because IE8 and earlier don't support the :not() selector.

You could use a tool like Selectivizr to add support for this to IE, but frankly, it's hardly worth it for this example, as you can easily add another CSS style to override the other one:

#selectable li#selecteditem:hover
{
    /* override styles */
}

Hope that helps.

Comments

2

CSS3:

#selectable li:hover:not(.selected) {
 background:red;

}

HTML:

<ol id="selectable">
  <li>Item 1</li>
  <li class="selected">Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
</ol>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.