Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Selector for not a child of element type?

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?

like image 668
bevacqua Avatar asked Feb 15 '13 18:02

bevacqua


People also ask

Which selector select an element that has no children?

The :empty CSS pseudo-class represents any element that has no children.

Which is not a type of CSS selector?

The :not() CSS pseudo-class represents elements that do not match a list of selectors. Since it prevents specific items from being selected, it is known as the negation pseudo-class.

How do you select an element that is not a class?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector. Since it is used to prevent a specific items from list of selected items.


2 Answers

: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 */ } 
like image 173
Joseph Silber Avatar answered Oct 05 '22 19:10

Joseph Silber


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

like image 23
Magistern Avatar answered Oct 05 '22 20:10

Magistern