Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple CSS Pseudo Classes

Tags:

syntax

css

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.

like image 313
Brian Fisher Avatar asked Jan 29 '09 20:01

Brian Fisher


People also ask

How do I use multiple pseudo-classes in CSS?

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 ?

Can I use multiple pseudo-elements?

You can combine several CSS pseudo-elements for one element. However, you cannot use CSS ::after two times or ::before two times.

Can CSS classes combine with pseudo-classes?

If you're talking about pseudo-classes, then yes, you can combine them in any order.

Can pseudo-elements and pseudo-classes be combined?

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.


3 Answers

: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 ;)

like image 131
Christoph Avatar answered Sep 19 '22 22:09

Christoph


You could use the adjacent sibling selector:

ul.phone_numbers li + li:before {    content: ","; } 
like image 24
Gumbo Avatar answered Sep 19 '22 22:09

Gumbo


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 ... */
}
like image 38
big kev Avatar answered Sep 19 '22 22:09

big kev