views:

52

answers:

2

How to detect, does browser support the CSS :first-child selector?

+2  A: 

I don't think there is a jQuery function to find out support for this. I expect if it does exist, it is going to be quite complicated. Are you really sure you need this? Care to share why?

If you can use jQuery anyway, why not add a jQuery statement to assign the class/property/whatever to the desired element, instead of relying on CSS?

As a "manual" answer, looking at the quirksmode.org compatibility table, the selector is fully supported in all modern browsers except the IE family which seems to have problems even in IE8.

Pekka
Ive found this page http://api.jquery.com/jQuery.support/ but dont know how to use it
Happy
@WorkingHard: jQuery.support cannot test for `:first-child` support.
BoltClock
@WorkingHard I would ask myself first *why* I need this in the first place.
Pekka
+4  A: 

You could just use that :first-child rule to set some specific value, and then get the computed style in Javascript to see if the :first-child rule is applied, e.g.

<style>
#foo { width: 200px; }
#foo:first-child { width: 400px; }
</style>

<span><span id="foo"></span></span>

<script>
if ($('#foo').width() < 400)
   alert('first-child not supported.');
</script>

(This is not tested. I have no IE 6.)

KennyTM
This is nice and an exact answer to the question, +1. Although I have the feeling the basic premise of *having to find this out in the first place* is the result of a faulty design decision.
Pekka
its not good to use extra block
Happy
@WorkingHard: what extra block?
BoltClock
@WorkingHard what are you talking about?
Pekka
@WorkingHard true. But I don't think you'll get around introducing an element like that (although you should be able to set `display: none` so it's hidden). Still, the basic question to me still is: *Why do you need this?*
Pekka