What is the proper CSS syntax for applying multiple pseudo classes to a selector. I'd like to insert "," after each item in a list except the last one. I am using the following css:
ul.phone_numbers li:after {
content: ",";
}
ul.phone_numbers li:last-child:after {
content: "";
}
This works fine on FF3, Chrome and Safari 3. IE7 doesn't work because it doesn't support :after (as expected). In IE 8 this renders with a comma after each li including the last one. Is this a problem with IE8 or is my syntax incorrect? It's ok if it doesn't work in IE8 but I would like to know what the proper syntax is.
Pseudo-classes are allowed anywhere in selectors while pseudo-elements may only be appended after the last simple selector of the selector. This means your syntax is correct according to CSS2. 1 and CSS3 as well, i.e. IE8 still sucks ;) Can I write element:hover:disable {} multiple pseudo-classes ?
You can combine several CSS pseudo-elements for one element. However, you cannot use CSS ::after two times or ::before two times.
If you're talking about pseudo-classes, then yes, you can combine them in any order.
There are no special rules around combining pseudo-classes and pseudo-elements, besides the one rule that says there can only be one pseudo-element per complex selector and it must appear at the very end.
:last-child
is a pseudo-class, whereas :after
(or ::after
in CSS3) is a pseudo-element.
To quote the standard:
Pseudo-classes are allowed anywhere in selectors while pseudo-elements may only be appended after the last simple selector of the selector.
This means your syntax is correct according to CSS2.1 and CSS3 as well, i.e. IE8 still sucks ;)
You could use the adjacent sibling selector:
ul.phone_numbers li + li:before { content: ","; }
There is no problem nesting pseudo class selectors as long as the rules applied match the particular element in the DOM-tree . I want to echo the possibilities of styling with pseudo class selectors from w3 website . That means you can also do stuff like :
.ElClass > div:nth-child(2):hover {
background-color: #fff;
/*your css styles here ... */
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With