Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't this CSS selector work: a:hover ~ span?

a:hover + span { background:yellow; }
a:hover > span { background:yellow; }
a:hover ~ span { background:yellow; }

The first two selectors work just fine. However, the third selector does not work?

Demo: http://jsfiddle.net/UAHw7/

Why?


Update: I fired up all my browsers ...

Opera 11 - Works
Safari 5 - Works
Firefox 3.6 - Works
IE9 RC - Works
Chrome 9 - Does not work

A Chrome issue then ...


Note: As Chrome no longer contains this bug, this question is obsolete.

like image 771
Šime Vidas Avatar asked Feb 21 '11 01:02

Šime Vidas


People also ask

How does hover work 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).

How to make CSS selector more specific?

Increasing specificity by duplicating selector As a special case for increasing specificity, you can duplicate weights from the CLASS or ID columns. Duplicating id, class, pseudo-class or attribute selectors within a compound selector will increase specificity when overriding very specific selectors.

What is CSS selector specificity and how does it work?

What is Specificity? If there are two or more CSS rules that point to the same element, the selector with the highest specificity value will "win", and its style declaration will be applied to that HTML element.

Which selector will add a hover style to an h3 element?

So, if you wanted the :hover effect to trigger whenever a user hovers over a <h3> element, you would specify h3 . :hover instructs the browser that the styles in the CSS rule should only be applied when the user is hovering over a certain element. This element is by the selector property.


2 Answers

Looks like a Webkit bug related to using the :hover pseudo-class.

It works fine for me

  • in FF 3.6.13
  • in IE 8 of all browsers
  • in Opera 11

It doesn't work for me

  • In Chrome 9
  • In Safari 5.0.3

Might be file-worthy.

like image 145
Pekka Avatar answered Oct 06 '22 01:10

Pekka


It seems to me that you can't combine the general sibling selector ~ with the pseudo-class :hover; note that if you change the selector to a ~ span then both of the span elements turn yellow.

like image 43
Sophie Alpert Avatar answered Oct 06 '22 01:10

Sophie Alpert