1

Simple Question: what's the proper way to write a CSS selector with a class AND a pseudo class?

For example:

a.someclass:active {
  top:1px;
}

or

a:active.someclass {
  top:1px;
}

Which is correct? If possible, can you give me a source on this - such as a W3C reference? I tried to find it but alas I could not.

3
  • 1
    The selected answer to this question is wrong, as its own link shows
    – Gareth
    Commented Jul 21, 2010 at 0:02
  • with :hover #2 is preferred (for ie6)
    – Knu
    Commented Jul 21, 2010 at 8:10
  • PS - thank you to all the stackoverflow nazi's for help with that. ;p Commented Jul 21, 2010 at 13:29

3 Answers 3

3

They are both correct, although the first one is more common.

Here's what's specified in the CSS grammar. You can see that the .class and :pseudo selectors are both allowed in the same places, along with #id and [attrib=val] selectors:

simple_selector
  : element_name [ HASH | class | attrib | pseudo ]*
  | [ HASH | class | attrib | pseudo ]+
  ;
class
  : '.' IDENT
  ;
pseudo
  : ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ]
  ;
2

First one is Both are right

Selectors Level 3

1
1

Both should be ok, if I understand the documentation correctly:

Pseudo-classes are allowed in all sequences of simple selectors contained in a selector. Pseudo-classes are allowed anywhere in sequences of simple selectors, after the leading type selector or universal selector (possibly omitted).

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