CSS Selector for No-Children-But-Not-Empty

Depending on what CSS properties you intend to apply, you may or may not be able to do this. If you’re going to apply something like color for example, you can simply set it to all code elements and reset it in code a, assuming all the text is contained within that a and not spilled outside of it within the code. This will only work for a handful of properties, however (mostly the font ones).

Otherwise, there’s not much in the way of pure CSS here, if you’re trying to select code elements that don’t contain a children. jQuery has code:not(:has(> a)) (or, for any arbitrary E element with no child elements at all, E:not(:has(> *))), but that’s not coming to CSS anytime soon, and Selectors 4 doesn’t provide anything else for “an element with no child elements”.

“Feh!” is right.

Leave a Comment