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
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.
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.
Pseudo-classes are allowed anywhere in selectors while pseudo-elements may only be appended after the last simple selector of the selector.
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.)
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
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: "--";
}
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