4

I was wondering if its possible to "embed" pseudo-classes inside of each other. I have a hunch that you can't, but I just want to make sure I don't just have syntax wrong.

Here's an example of what I'm trying to do:

p.description { margin-bottom: 20px; }

Given that style, if you only want that to happen on matches that aren't the LAST p.description, is there anyway to do the following?

p.description:not(p.description:last-child)

Naturally, I'd have two styles, like so:

p.description { margin-bottom: 20px; }
p.description:last-child { margin-bottom: 0; }

...but that seems wasteful if it can be done in a single line.

Thanks a lot!

3 Answers 3

6

Yes, to the title of your question:

p.description:not(:last-child)

No, to the CSS example in the body of your question

p.description:not(p.description:last-child)

The spec says:

The negation pseudo-class, :not(X), is a functional notation taking a simple selector (excluding the negation pseudo-class itself) as an argument.

A simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.

Sign up to request clarification or add additional context in comments.

3 Comments

Hm, apparently the definition was changed between CSS2 and CSS3. In CSS2, p.description:first-child would be considered a simple selector.
Aside from whether or not it's "reasonable" for :not() to take simple selectors only, I think the CSS3 definition of "simple selector" is reasonable, and makes more sense than the CSS2 definition.
This is what I was looking for. I had a hunch it was possible but I wasn't sure what the correct syntax was. Thanks!
2

Yes, p.description:not(:last-child).

2 Comments

... because :not() can take a simple selector, which the pseudo-class :last-child is.
Sorry, I had already edited the post when you said this. (I wrote a JavaScript library that allows complex selectors in :not() so I got a bit confused :P)
0

Yes you can, in webkit it works fine. I use this for example:

.middlenav:not(:nth-last-child(1))

Works great.

so p.description:not(:last-child). should too

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.