Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to combine the css3 pseudo classes :after and :lastchild?

I have a list of links and I am using the following code to put a word after each link item:

a:after {content: "eg"}

However, I don't want the content to come after the last item in the list, so ideally I would like to say something like

a:last-child:after {content: ""}

but this is stripping the content that comes after all the links. Is there anyway way of combining these two? If there is and you can explain what is going on exactly I would really appreciate it :)

Thanks

like image 461
nick501414 Avatar asked Mar 13 '12 16:03

nick501414


People also ask

Can pseudo-elements and pseudo classes be combined?

Combining pseudo-classes and pseudo-elementsIf you wanted to make the first line of the first paragraph bold you could chain the :first-child and ::first-line selectors together. Try editing the previous live example so it uses the following CSS.

Can CSS classes combine with pseudo classes?

Combining pseudo classes with CSS classesIt is possible to combine pseudo classes and regular CSS classes. This combination lets you style elements that have specific classes and also react to certain external factors.

Can you use two 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.

Can you have multiple pseudo classes?

1, an element can only have at most one of any kind of pseudo-element at any time. (This means an element can have both a :before and an :after pseudo-element — it just cannot have more than one of each kind.)


2 Answers

Try this:

HTML:

<ul>
<li><a href="#">Check this out</a></li>
<li><a href="#">Check this out</a></li>
<li><a href="#">Check this out</a></li>
</ul>

​ CSS:

a:after {content: "what what"}
li:last-child > a:after {content: ""}​

Here is a Fiddle to demonstrate.

Also, keep in mind, if you have many users that use IE7 and IE8, the :after pseudo class does not work in IE7 and below, and the :last-child pseudo class does not work in IE8 and below. See here: http://www.quirksmode.org/css/contents.html

like image 85
Brian Sweat Avatar answered Sep 27 '22 17:09

Brian Sweat


I'd do that with a combination of :not, :last-child and :after. With :not(:last-child) you select every link except the last one: http://jsfiddle.net/2CS9G/

a:not(:last-child):after{
    content: "--";
}

If your a-tags are in a list (ul): http://jsfiddle.net/2CS9G/2/

li:not(:last-child) a:after{
    content: "--";
}
like image 30
Alex Avatar answered Sep 27 '22 18:09

Alex