1

My question is really simple, just what i am trying to do is :hover, :after and :before , i want hover anf after to embed in same element, check out my css code:-

#sidebar .widget li a:before:hover, #sidebar .widget li a.active:before { 
    background-position: 65% 65.7%;
}

Here the element have an icon in :before which i cnt remove or modify, and also i want to have an hover effect on it...

Any solution for this, my console doesn't show the hovering effect?

1

1 Answer 1

2

Interesting question. If you're able to show us a working example we could probably be of more help.

However, in theory there's nothing wrong with what you're attempting to do (although not all browsers will like it: particularly IE8 and below).

The important thing to understand here is that :hover is a pseudo-class, whereas :before is a pseudo-element.

Here's a quick excerpt from the standard (with thanks to this answer previously on Stack Overflow):

Pseudo-classes are allowed anywhere in selectors while pseudo-elements may only be appended after the last simple selector of the selector.

The mistake you're making is in your syntax: the order that you're appending them.

Try this instead:

#sidebar .widget li a:hover:before,
#sidebar .widget li a.active:before { 
    background-position: 65% 65.7%;
}

That should do as you wish. However this isn't going to give you great cross-browser coverage, it's not something that all browsers support of have implemented.

A better approach would be to:

  • reset the :before element to nothing (overwrite the styles you can't access);
  • use a non-repeated background image on the anchor instead (to display the image), and padding-left to give the indentation;
  • You can then switch the background-image in whatever fashion you see fit using :hover on the anchor in your CSS.

This will give you far better cross-browser compatibility.

1
  • Hi @SaurabhLP that's great, glad that we were able to help! If your question is answered now, please do ensure that you mark the correct answer (there's a tick icon alongside each answer - the relevant one just needs ticking). This ensures that people coming to the site in the future with a similar problem will be able to easily find the relevant answer too. Commented Jun 4, 2013 at 11:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.