Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 199073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:59:31+00:00 2026-05-11T16:59:31+00:00

I am trying to create a simple mouseover effect using a combination of mouseover,

  • 0

I am trying to create a simple mouseover effect using a combination of mouseover, mouseout, addClass, and removeClass. Basically, when the user mouses over an element, I want to apply a different border (1px dashed gray). The initial state is “1px solid white”. I have a class called “highlight” which simply has “border: 1px dashed gray” in it. I want to add that class onmouseover and remove it on onmouseout but I am unable to get the effect I want unless I use !important within the “highlight” class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:59:31+00:00Added an answer on May 11, 2026 at 4:59 pm

    It sounds as though you’ve got the javascript working fine as is, but it’s just a problem with the specificity of your CSS rules, which is why !important makes it work.

    You just have to make your highlighted css rules more specific than the non-highlighted rules.

    #someItem ul li { /* Specificity = 102 */
        border-color: white;
    }
    
    .highlight { /* Specificity = 10 -- not specific enough! */
        border-color: grey;    
    }
    
    #someItem ul li.highlight { /* Specificity = 112 -- this will work */
        border-color: grey;    
    }
    

    Edit with further explanation:
    Let’s say the relevant parts of your HTML look like this:

    <div id="someItem">
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
        </ul>
    </div>
    

    and you have this CSS:

    #someItem ul li {
        border: 1px solid white;
    }
    .highlight {
        border-color: grey;
    }
    

    Currently, all the list items in the ul in #someItem div will have a white border, and nothing has the class highlight so nothing’s grey.

    Through whatever means you want (in your case a hover event in jQuery), you add a class to one of the items:

    $(this).addClass('highlight'); 
    

    The HTML will now look something like this:

    <div id="someItem">
        <ul>
            <li>Item 1</li>
            <li class="highlight">Item 2</li>
            <li>Item 3</li>
        </ul>
    </div>
    

    So far, your Javascript and HTML are working fine, but you don’t see a grey border! The problem is your CSS. When the browser is trying to decide how to style the element, it looks at all the different selectors which target an element and the styles defined in those selectors. If there are two different selectors both defining the same style (in our case, the border colour is contested), then it has to decide which style to apply and which to ignore. It does this by means of what is known as “Specificity” – that is, how specific a selector is. As outlined in the HTML Dog article, it does this by assigning a value to each part of your selector, and the one with the highest score wins. The points are:

    • element selector (eg: “ul”, “li”, “table”) = 1 point
    • class selector (eg: “.highlight”, “.active”, “.menu”) = 10 points
    • id selector (eg: “#someItem”, “#mainContent”) = 100 points

    There are some more rules, eg: the keyword !important and also inline styles, but that’s mostly irrelevant for this, uhh… “lesson”. The only other thing you should know is that if two selectors have the same specificity, then the one defined later in the file wins.

    Going back to your problem, given the CSS we had before, we can see why it’s still not got a grey border:

    #someItem ul li = id + element + element = 100 + 1 + 1 = 102 points
    .highlight = class = 10 points
    

    As mentioned earlier, the solution is to create a more specific selector:

    #someItem ul li.highlight
       = id + element + element + class
       = 100 + 1 + 1 + 10
       = 112 points
    

    And to answer your question in the comments, you don’t need to change any of your javascript or HTML for this to work. If you break down that selector, what it’s saying is:

    Look for the element with id “someItem”, inside that look for a ul element, and then an li element which has the class “highlight” on it.

    …and now, given the simple .addClass() call that you made earlier, the li satisfies these conditions, so the border should turn grey.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 184k
  • Answers 184k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Is it (ValidateInput) on a POST method? It only works… May 12, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer If the service is only delivering an image as a… May 12, 2026 at 4:46 pm
  • Editorial Team
    Editorial Team added an answer Your big problem is the (LPCWSTR) casts in the calls… May 12, 2026 at 4:46 pm

Related Questions

I am trying to create a rather simple effect on a set of images.
I am trying to do drop down horizontal menu in JavaScript. It's works fine
I have written a simple WCF service that accepts and stores messages. It works
I am trying to create a simple dialog in MFC using Visual C++. My

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.