Start learning with our library of video tutorials taught by experts. Get started

CSS3 First Look

Negation pseudo-class selectors


From:

CSS3 First Look

with James Williamson

Video: Negation pseudo-class selectors

The negation pseudo-class selector is almost in a class by itself. Unlike other selectors the negation pseudo-class selector target elements based on what they are not instead of what they are. Confused? Well, to be honest, this is a selector that might take some time to wrap your head around. So let's take a look at the syntax and see what it can do for us. Much like other pseudo-class selectors, the negation selector starts with a simple selector with the not pseudo-class attached to it. Unlike other selectors, however, the negation pseudo-class selector then accepts an argument to be passed into it.
Expand all | Collapse all
  1. 3m 5s
    1. Welcome
      1m 20s
    2. Using the exercise files
      1m 45s
  2. 31m 30s
    1. What is CSS3?
      5m 26s
    2. The current status of CSS3
      3m 35s
    3. An overview of CSS3 capabilities
      2m 24s
    4. Can you use CSS3 now?
      5m 31s
    5. Detecting support for CSS3
      9m 0s
    6. Understanding vendor prefixes
      5m 34s
  3. 1h 9m
    1. An overview of child and sibling selectors
      3m 11s
    2. Using child and sibling selectors
      7m 17s
    3. An overview of attribute selectors
      3m 19s
    4. Using attribute selectors
      8m 32s
    5. Pseudo-class UI selectors
      5m 56s
    6. Negation pseudo-class selectors
      6m 48s
    7. Target pseudo-class selectors
      5m 39s
    8. Structural selectors
      3m 58s
    9. Nth-child selector syntax
      10m 0s
    10. First, last, and only structural selectors
      5m 39s
    11. Using structural selectors to write more efficient code
      8m 52s
  4. 45m 28s
    1. Color formats in CSS3
      7m 9s
    2. Transparency in CSS3
      9m 10s
    3. CSS3 gradients
      4m 11s
    4. Creating linear gradients
      13m 57s
    5. Creating radial gradients
      11m 1s
  5. 49m 8s
    1. Working with web fonts
      6m 38s
    2. @font-face syntax
      4m 52s
    3. Downloading sample fonts
      6m 5s
    4. Writing @font-face declarations
      7m 57s
    5. Using web fonts
      6m 42s
    6. Using text shadows
      7m 14s
    7. Creating multi-column text
      9m 40s
  6. 50m 55s
    1. An overview of the flexible box model
      4m 42s
    2. Controlling box orientation
      5m 2s
    3. Setting element flexibility
      12m 59s
    4. Distributing boxes
      7m 54s
    5. Controlling box alignment
      12m 38s
    6. Working with box-sizing
      7m 40s
  7. 1h 5m
    1. Using border-radius
      6m 20s
    2. Creating custom rounded corners
      10m 21s
    3. Understanding border images
      5m 15s
    4. Using border images
      8m 52s
    5. Creating box shadows
      8m 58s
    6. CSS3 backgrounds
      4m 55s
    7. Controlling background size
      8m 46s
    8. Creating multiple background images
      6m 4s
    9. Using background-origin
      3m 18s
    10. Clipping background content
      3m 2s
  8. 40m 8s
    1. An overview of CSS3 2D transforms
      4m 26s
    2. Using 2D transforms
      8m 16s
    3. Setting transform origins
      5m 24s
    4. An overview of CSS3 transitions
      5m 0s
    5. Animating CSS properties
      6m 12s
    6. Using easing in animations
      5m 41s
    7. An overview of 3D transforms
      5m 9s
  9. 37m 56s
    1. Understanding media queries
      6m 18s
    2. Strategies for targeting multiple devices
      6m 4s
    3. Writing styles for target screen sizes
      12m 11s
    4. Deploying styles through media queries
      3m 55s
    5. Basing media queries on page orientation
      2m 24s
    6. Targeting media queries for iOS devices
      7m 4s
  10. 1m 6s
    1. Goodbye
      1m 6s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
CSS3 First Look
6h 34m Appropriate for all Nov 29, 2010

Viewers: in countries Watching now:

In CSS3 First Look, staff author James Williamson provides an in-depth introduction to the newest CSS standard, detailing its modular format, history, and current level of browser support, while also demonstrating its capabilities and applications. The course includes tutorials on using new selectors, modifying typography and color, working with the box model, and understanding media queries. Exercise files accompany the course.

Topics include:
  • Understanding the history of CSS3
  • Working with the new selectors
  • Adding transparency and gradients
  • Specifying web fonts with @font-face
  • Understanding the advances to page layout
  • Looking at CSS3 box model capabilities
  • Using 2D and 3D transforms
  • Understanding media queries
Subject:
Web
Software:
CSS
Author:
James Williamson

Negation pseudo-class selectors

The negation pseudo-class selector is almost in a class by itself. Unlike other selectors the negation pseudo-class selector target elements based on what they are not instead of what they are. Confused? Well, to be honest, this is a selector that might take some time to wrap your head around. So let's take a look at the syntax and see what it can do for us. Much like other pseudo-class selectors, the negation selector starts with a simple selector with the not pseudo-class attached to it. Unlike other selectors, however, the negation pseudo-class selector then accepts an argument to be passed into it.

You can see that here inside the parenthesis. The selector then uses the argument to filter the results and target all elements not represented by their argument itself. Since that probably didn't make that crystal-clear, let's take a look at an example. Now, here's a negation pseudo- class selector that targets div tags. However, instead of targeting every div tag, notice that we were passing as an argument the class caption. So in this case the selector would target every div tags except for the one with the class caption applied to it.

And it's worth noting that there are variations on the syntax as well. If you want to group multiple arguments together, you simply daisy-chain them together with no white space between them. Arguments passed to negation pseudo- class selector can be any simple selector, including attributes selectors and other pseudo-class selector themselves. However, you can't pass pseudo-elements or other negation pseudo-class selectors. So you can't do a not inside of a not. That's kind of like dividing by 0. You don't want to do that. Now specificity is another thing that you want to watch out for.

Obviously, if you ever need to use a negation selector, you're probably needing to target a very specific set of elements. Now this can be overridden by rules with a higher degree of specificity. To count a negation rule's specificity, the selectors prior to negation selector and any rules in the argument all count toward the rules specificity. The negation pseudo-class itself however doesn't count. Now support for the support for the negation pseudo-class selector is fairly widespread. However, Internet Explorer's lack of support would require you to target elements in another way for it.

I should point out that while Opera is listed as supporting the negation pseudo-class selectors, in my own testing I found the results to be a little mixed. So I would test thoroughly before deploying this within your own style sheets. With all that out of the way, let's give the negation pseudo-class selector a try. So here I'm previewing the gallery. htm file, which can be found in the Explorer's directory. I have got this gallery where I have a featured image and then I have some thumbnails, which will apparently eventually link you to a gallery with those sets of images.

The problem is the default styling for the images are working for the thumbnails, not so much the main photo here. You can see it's still kind of an inline image, which is causing some problems with the text there. I like to do something a little bit more to it than just that orange border or something to sort of set it apart a little bit. So in order to do that, we are going to write a negation pseudo-class selector. So here we are in the gallery.htm file and again that can be found in the 0206 Explorer's folder. I am just going to go ahead and write the selector directly in the embedded styles on the page.

So just after the last selector, but still within the style tag, I am going to type in #mainArticle img, and then my negation selection, so not(). Now the only reason I am doing that is just to get the rest of the skeleton of this out of the way. So I am opening and closing the curly braces. This is just our basic structure for the negation pseudo-class selector. Now inside the parentheses of the negation pseudo-class selector I need to pass an argument. If I go down and take a closer look at these images, I can see that the thumbnails all have the word thumb in their alt tags.

Isn't that convenient? How very nice. I am fond of telling people demos are designed to work. But you know what? If you're really paying attention to that what you're doing when you create your site, this is definitely something that you could be consistent with. I am going to have thumbnails. They are going to have the thumb word in the alt tags. Of course, our main image doesn't have that. You can see that this main image up here does not have thumb inside the alt tags. So there is our hook. That is what we can check for. So I am going to back up to the CSS up here and inside that negation pseudo-class selector I am going to type in an attribute selector.

So in bracket notation, I am type in alt*= because I just want any particular literal string that has that word thumbnail and I want that to be used. Then I am going to pass the thumb string. Then don't forget to close yours square brackets there. So essentially I'm passing in that attribute selector that's looking for any particular main article image with an alt attribute that contains the word thumb. So how we are going to style this? With magic. I am just going to paste in my styles.

Now the reason I'm doing that is because watching me type is boring. We also haven't talked about a couple of these selectors that I am going to be using here. Now we will get to those a little bit later on, such as border image and border radius. We will talk about all those a little bit later. As it is, just pause your video and go ahead and copy these line for line, or you can find the code in the finished files and then copy and paste it as well. That's just a lot quicker than sitting here and watching me type that, I'm sure. So I am going to go ahead and save that and then I am going to preview that back in my browser. For this particular example, I am previewing in Safari.

No reason other than the fact that we haven't really used Safari yet. Let me go ahead and refresh this. You can see that our main image styling is totally different now. I am getting a nice sort of border around it, the beveled border. It's displaying as a block level element. So the text is being pushed down on the line that it is supposed to be pushed down to. Now there is a good bit of difference between the styling of my thumbnails and the main image itself. Now again, this is all happening because it's basely saying, hey, find any image in that main article that does not have the alt attribute, so it contains thumb.

So a very, very specific selector to be sure. Now more than likely you won't find yourself using the negation pseudo-class selector a lot. I've got to be honest with you. Sometimes it's a stretch to figure out when you can use it. However, it is an incredibly handy selector to keep in mind, sometimes in those last-ditch efforts. That's the one that's going to come through. It's especially good for filtering out which elements receive styling based on element attributes or selector types when you have one or two elements that just don't meet the criteria of everybody else.

So have fun using the negation pseudo- class selector and again, remember there is lack of support in Internet Explorer. So, again, it's probably something I would use for enhancing my styles rather than presenting the main styling for that particular element.

Find answers to the most frequently asked questions about CSS3 First Look.


Expand all | Collapse all
Please wait...
Q: I'm following along with the video "Transparency in CSS3".  James shows us how to achieve transparency in Internet Explorer by going to Kimili.com and entering a HSLA value to generate code for transparency.

Here that code:

background: transparent;-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#BF0E0C0B,endColorstr=#BF0E0C0B)"; /* IE8 */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#BF0E0C0B,endColorstr=#BF0E0C0B); /* IE6 & 7 */ zoom: 1;

When this code is added to my HTML file it removes transparency on browsers that do in fact support it. So I'm left with NO transparency. Why?
A: The problem is in the filter code. If the IE background is called last, the first "transparent" declaration will remove all previous colors, regardless of browser. To resolve this, place the rule inside a conditional comment for IE or remove the "transparent" declaration at the front of the rule.
Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS3 First Look.

Return to your organization's learning portal to continue training, or close this page.


OK

Course retiring soon

CSS3 First Look will be retired from the lynda.com library on April 24, 2014. Training videos and exercise files will no longer be available, but the course will still appear in your course history and certificates of completion.


Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked