I want to chain CSS pseudo-classes

While coding up some CSS for a site recently, I typed out something that — to me — made perfect sense.

The HTML:

…
<div>
    <p>Paragraph one</p>
    <p>Paragraph two</p>
    <img src="imageone.gif" title="Image one" />
    <p>Paragraph three</p>
    <p class="class">Paragraph four</p>
</div>
…

The (theoretical) CSS:

div p:not(.class):last-of-type { background-color:#fff; }

The Result:

Paragraph three has a white background.

Logic Flow:

  1. Find 'div' elements (one found)
  2. Look for 'p' elements inside 'div' elements (four found)
  3. Find 'p' elements NOT with class of "class" in previous result (three found)
  4. Get last 'p' element in previous result (one found)

Alternate (theoretical) CSS:

div p:last-of-type:not(.class) { background-color:#fff; }

Result:

Nothing.

Logic Flow:

  1. Find 'div' elements (one found)
  2. Look for 'p' elements inside 'div' elements (four found)
  3. Get last 'p' element in previous result (one found)
  4. Find 'p' elements NOT with class in "class" of previous result (zero found)

It’s really simple logic that I don't think browsers would have any trouble implementing, yet it’s not allowed by spec. Granted, I haven't given this too much thought, and there are probably cases where this would cause collisions or maybe even circular logic, but until I find those, I like the idea.

One Response to “I want to chain CSS pseudo-classes”

  • [...] }); Nesting rules in CSS style sheets 2009.10.07 06:41 Web Dev I seem to have a more and more things to complain about CSS lately. In my quest to create overly verbose style sheets, I can't help but to dream up better ways [...]

Leave a Reply

Common formatting HTML allowed (when in doubt use code, if it fails, I'll edit appropriately).
Always close tags.