CSS Selector for not a child of element type?

CssCss Selectors

Css Problem Overview


I want to style code elements that are not inside a tags.

What is the best approach to accomplish this?

code:not(a code) doesn't seem to work at all, at least on Chrome, even though it seems like it should

I can't get it to work from the console either.

Are there any other css-only approaches I could use for this?

Css Solutions


Solution 1 - Css

:not does not support combinator selectors.

If we're talking about its direct parent:

:not(a) > code

Otherwise there's no way to do this in CSS. You'll have to override it:

code {
    /* some styles */
}

a code {
    /* override previous styles */
}

Solution 2 - Css

Actually, you should be able to use your code 樂, or you could use the wildcard character to select all elements to not be selected

code:not(a *) {
  font-weight: bold;
}

Codepen

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbevacquaView Question on Stackoverflow
Solution 1 - CssJoseph SilberView Answer on Stackoverflow
Solution 2 - CssMagisternView Answer on Stackoverflow