Is there a way to do something like this in CSS2 (not 3)?
开发者_如何学编程h2:first{bla bla;}
CSS2.1 only provides the :first-child
pseudo-class, which selects an element if it's the first child of its parent. It does not provide any way to select the first element of its kind regardless of whether it is the first child of its parent.
Depending on what you mean by "kind", Selectors 3 provides :first-of-type
to select the first element of its given element type within its parent, and Selectors 4 has proposed pseudo-classes for the nth element satisfying a compound selector within its parent. Besides that, there is no other way to select the first element in a set of matches or the first that satisfies a complex selector using CSS.
Yup,
"The :first-child pseudo-class matches an element that is the first child element of some other element." - from http://www.w3.org/TR/CSS2/selector.html
There's also a listing of all available selectors in CSS2. Be warned though, most of these are not available in lower versions of IE
There is E:first-of-type
, which I think would be the right answer. It's defined in CSS3 and supported by most browsers except your old friend IE (8 and below don't support it).
精彩评论