Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
CSS gives Web designers control over the appearance of their web sites by separating the visual presentation from the content. It lets them easily make minor changes to a site or perform a complete overhaul of the design. In CSS Web Site Design, instructor and leading industry expert Eric Meyer reviews the essentials of CSS, including selectors, the cascade, and inheritance. The training also covers how to build effective navigation, how to lay out pages, and how to work with typography, colors, backgrounds, and white space. Using a project-based approach, Eric walks through the process of creating a Web page, while teaching the essentials of CSS along the way. By the end of the training, viewers will have the tools to master professional site design. Exercise files accompany the training videos.
In this video we're going to talk about specificity, which in a lot of ways is the heart of conflict resolution in CSS, and the easiest way to sort of approach the subject is to say, suppose that I have two rules that absolutely conflict, I said I want my list items, my li elements to have a background of red, but then, I also said that I want the level background of green and I can't have both, I mean it would be cool if CSS would just sort of strike them together, candy cane style, but it doesn't. The list items have to have either a red background or a green background.
Which one is it going to be, the answer as it turns out is green. But the question asked is why, you might say to yourself, well, because green came later, but suppose that I said this, I said body li background red, but then I said li background green. The green came second, but the red looks like it might be, I don't know, a little more specific perhaps. So which one do you think it is, red or green. As it turns out, when we hit Reload it's red.
The reason for this is specificity, which is one of those kind of dried topics that you really have to understand very close in order to be able to write CSS really well. So let's use these as an example. For this list item, role the li background green, there's one element descriptor, li and so the specificity of that is 0,0,0,1. The one being that there's one element descriptor here. Now the other zeros, we'll get to in a moment, but just accept that they're there. The specificity of the body li is 0,0,0,2 because there's two element descriptors, there's body and there's li.
So, when you have a case where there are two declarations that conflict, we have background red, background green, we have conflicting declarations applying to the same elements which are list items in this document, then the declaration whose associated selector has a higher specificity will win out. The other really doesn't matter, if the specificity isn't equal. All right, so, because background red, the associate selector has a higher specificity 0,0,0,2 then, the selectors specificity associated with background green, red wins out. Now, suppose we put in a class to one of these, like we might say, li.first background yellow, then, when we go over, we discover that the list item with the class of first is yellow, not because it's last, but because it has a higher specificity. Here we have one element descriptor and we have one class descriptor, li for the element and .first for the class, so the specificity here is 0,0,1 for the class and 1 for the element, so 0,0,1,1 is a higher specificity then 0,0,0,2, which is, will only have appear for body li, so li.first wins out over body li. Now, the reason that, only that one list item ends up with the yellow background, is because it's the only one that has a class of first.
If we can group these together, then both of those will win out because li.first 0,0,1,1 and li.last 0,0,1,1, these don't all get counted up together, because this is a group selector here, this one gets counted up and this one gets counted up, separate from each other, exactly the same as if I had written these as two different rules, remember that how group selectors work. Now, we can start to take this, you know, even further. I can say html body li.first, which would give this one a specificity of 0,0,1,3 because there's one class descriptor and three element descriptors HTML, body and li. That's not really going to have an impact in the presentation here, but it does point out how these things sort of act together.
Let's do an ID, navlinks li, now you list item inside an element with an ID of navlinks, should have a background of fuchsia. So the specificity for this will be 0,1 for the ID, 0,1 for the element li. So 0,1 navlinks, 0 cause there are no classes and 1 for the element, the li and if we hit Reload, now they are all fuchsia, because the, the specificity of this selector outweighs any of these others.
Now, of course is important to remember that you only have to worry about the specificity conflict if you have conflicting declarations. We've been, you know, fighting with background right now, but suppose right here the lowliest of these selectors, just plain, old li, I set font style italic and the declaration font-style italic is associated with a selector, li has a specificity of 0,0,0,1, but it's still going to work, it's still going to come through, because there is no conflicting font style declaration in any of the rules whose selectors have hire specificities. So, the specificity resolution only takes in when there are conflicts.
You might have been wondering this whole time I've been muttering numbers at you, I've always started with a zero, and you might have caused to wonder why that is, and that's because there's one more way to cause specificity to happen and that's the in-line style attribute. The specificity of the style declaration here is, the style attribute value is 1,0,0,0.
That's higher than 0,1,0,1, which is the specificity of the navlinks li rule. So when we go back and we hit Reload, the element, which has the in-line style attribute, will win out over basically anything that we put into our author style sheet, into our own style sheet, because it's specificity 1,0,0,0 can never be outweighed by any rule up here, because rules that we put in our own style sheet will always start with zero and this one will always start with one. So, this is a way to override anything you might've written in a style sheet although it's not a recommended way to style at all because you're sticking style information into the document source and at that point, you may as well just go back to font elements and spacer divs. So, you can override with the highest specificity styled attribute, but it is just not recommended, so I'm going to take it back out.
So let's go back to sort of our original case, which is, we have two rules and this sort of have the same specificity, and there's a conflict. So here, we're going to see yellow backed, italic text. And if we reverse these, background yellow and then background green, we're going to see green backed, italic text.
That's specificity in a nutshell, and it's not the most immediately intuitive of concepts, but as I say it is very important to understand in order to write more efficient CSS and also to figure out why, on occasion, maybe you've added a rule, and nothing happens. Sometimes that happens, you want to change something and you go back to your browser and hit Reload and there's no change. Whatever was you just typed in, had no apparent effect, and there's a way to figure out whether or not that's a specificity conflict, which we'll discuss in the next chapter.
There are currently no FAQs about CSS Web Site Design.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.