0

Problem is very very simple: When clicking on "red red red" in browsers: Chrome 17, FireFox 10, IE 9, Opera 11.61 both elements have been lightened with their appropriate colors.

When clicking on "GREEN" it happens only in Chrome and FF. In IE and OPERA nothing happens. Why?

Demonstration:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
.container:active
{
    background: red;
}
.container:active .in
{
    background: green;
}
</style>
</head>
<body>
    <div class="container">
        red<br />red<br />red<br />red<br />red<br />
        <div class="in">GREEN</div>
    </div>
</body>
</html>

Does anyone know any workarounds?

James A Mohler
  • 10,562
  • 14
  • 41
  • 65
Roman
  • 1,696
  • 2
  • 16
  • 27
  • It works in IE8 and 9; earlier versions of IE (7 and below) do not support psuedo-selectors on non-anchor elements, I believe. – Jared Farrish Mar 02 '12 at 13:02
  • No, in IE9 it doesn`t work. Please open Demonstration and click on GREEN. I have IE 9.0.8112. Is any possibility we have different IE9 versions? – Roman Mar 02 '12 at 13:06
  • Oh, I see; the active selector doesn't fire on child elements. – Jared Farrish Mar 02 '12 at 13:06
  • If the browser doesn't allow the click to fall through to an element that contains it, the only solution I can think of is to use Javascript to make browser's perform the same. My own opinion is that IE and Opera are handling it the way I would expect. – Jared Farrish Mar 02 '12 at 13:13
  • Yes, it seems like it`s the only way - JavaScript. Thank you – Roman Mar 02 '12 at 13:17

2 Answers2

4

CSS does not define which elements can be "active" and if a parent element of a clicked-on element also becomes "active".

http://www.w3.org/TR/CSS2/selector.html#dynamic-pseudo-classes

So all browsers are behaving according to the specs. Sorry!

If you want a workaround, try using an <a> element instead of the outer <div>. Need more styling then though. And the inner <div> should be an inline element to make sure it remains valid HTML.
Edit: And the <a> also must have a href attribute, otherwise it still won't work in IE. (Can't test on Opera here.)

jsFiddle

Mr Lister
  • 42,557
  • 14
  • 95
  • 136
2

I believe you need to use Javascript to handle this, as CSS is not capable of selecting parent elements.

In jQuery:

$(document).ready(function(){
    $('.container .in').mousein(function(){
        $(this).addClass('container_active');
    }).mouseout(function(){
        $(this).removeClass('container_active');
    });
});

http://jsfiddle.net/uYfna/8/

Jared Farrish
  • 46,034
  • 16
  • 88
  • 98
  • Yes, but I don`t see point of waiting for whole page to load, so I edited this a bit. Like so: http://jsfiddle.net/uYfna/18/ – Roman Mar 02 '12 at 14:09