CSS selector
:not
only accepts a simple selector as an argument, meaning that complex selectors like
div > span
cannot be used with it. This limitation prevents you from easily styling elements based on their parent-child relationship in CSS without compromise.
To work around this restriction, one approach is to target specific subsets of the desired elements. For instance, if you want to style all spans that do not have a div parent, you can use the following selector:
:not(div) > span { color: red }
In more complex scenarios where this workaround is not possible, another option is to use the "do/undo" trick. By setting the general style for the element and then resetting it for specific cases, you can achieve the desired styling even with limitations:
span { color: red }
div > span { color: inherit }
This technique can be expanded to cover multiple scenarios, but keep in mind that there may be conflicts with other stylesheets when using this method.