Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Selectors Advanced Selectors :not() – The Negation Pseudo-Class

Jeffrey Watters
PLUS
Jeffrey Watters
Courses Plus Student 7,403 Points

square brackets in :not([some element])

on the video for this lesson at 1:41 Guil mentions the use of square brackets. I see the use with the input command but do not understand why you would use square brackets in some cases but not in others. I looked over the reference at: https://developer.mozilla.org/en-US/docs/Web/CSS/:not but there is no mention of the square brackets. Does anyone have a reference that is more informative.

Thank you,

Jeff

For those who are curious, the video in question is this one.

It's interesting that the brackets aren't included in the documentation, because they are required when you are attempting to select (or deselect in this case) for explicit attribute/value pairs. They aren't required if you are using the normal selector methods. I'll explain...

Say you have these two elements:

<li class="good">Superman</li>
<li class="bad">Lex Luthor</li>

You could make the background color of Lex Luthor red using either one of the following selectors (this is just for sake of discussion):

li:not(.good) {
  background-color: red;
}

li:not([class="good"]) {
  background-color: red;
}

So either of those work. But... if you change the use of brackets around (add them to the first selector and take them away from second selector), then neither of them will work (they end up failing so they do nothing):

li:not([.good]) {
  background-color: red;
}

li:not(class="good") {
  background-color: red;
}

So the brackets are required for explicit attribute/value pairs, but they can not be used with shorthand class/id selectors. Nice catch.

Brad Lacke
Brad Lacke
7,438 Points

What I took away from the :not() lesson is that even complex selectors can be a part of the not expression, as long as they are properly written.

The case where there are also square brackets inside the parenthesis, and the reason why it's not being generally discussed in MDN :not() literature, is because in that instance Guil was actually "not-ing" an attribute selector, in this case

[type="submit"]

So maybe it's less confusing to think of those as actually belonging to the [type = "submit"] argument, and not a weird special case of not (bracket-voodoo). I know though man, watch too many of these videos in a row and suddenly nothing makes sense.

1 Answer

Omahr B. Carpinteyro
seal-mask
.a{fill-rule:evenodd;}techdegree
Omahr B. Carpinteyro
Front End Web Development Techdegree Student 4,684 Points

He's using the :not() in combination with an attribute selector, remember the syntax for attribute selectors is element[attribute="value"], so he's using element:not([attribute="value"]) where input is the element, type is the attribute and submit is the value

input[type="submit"] 

:not() 

// This two selectors combined results in: 
input:not([type="submit"])