Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
In this hands-on course, James Williamson demonstrates the concepts that form the foundation of Cascading Style Sheets (CSS), including styling text, adding margins and padding, and controlling how images display. The course also explores the tools needed to work with CSS, the differences between embedded and external styles, how to use selectors to target elements, and what to do when styles conflict.
Most of the time the cascade, or inheritance, can be counted on to resolve styling conflicts; however, there is a third concept, specificity, that can come into play as well. Now understanding it is critical, as the majority of the time when authors have styling errors that they just can't quite figure out due to conflicts, specificity is usually the culprit. Now the specificity of a selector is just that--how specific is it? In the event of a conflict between two selectors that inheritance can't solve, the specificity of the selector is used to determine which selector has precedence.
Okay, so how is it calculated? Well, all simple selectors are weighted with a value. IDs are worth 100, classes are worth 10, and element selectors are worth 1. So in this chart, the first selector here, body, has 1 element selector. Its specificity would be 1. #mainContent is an ID selector. Its specificity would be 100. .quote, that's a class selector. Its specificity would be 10. A seemingly complex descendent selector, div p, would actually only have a specificity of 2, because it has 2 elements in it, and then #sidebar p that has 1 ID, 1 element, specificity of 101.
So, once you get the hang of this, once you know, okay, IDs are 100, classes are 10, elements are 1, it's a little easier to calculate. But let's go ahead and try it out ourselves. So here I am in specificity.htm, and you can find this in the 03_04 folder. Again, very simple page structure. If I scroll down, I can see that I have a section with an ID of mainContent. Inside of that there is just a single paragraph, and inside of that there is a strong tag. Okay, so what I want to do is go over to my selectors, and I am just going to add another paragraph selector right below the first one.
That seems kind of weird, I know, but I am going to change the color of this to blue. Now, knowing what we know about the cascade, we realize that all the paragraphs on this page would be blue rather than red, because blue comes after it, right? Well, not always. If I go up to the first selector and I change that, when I say #mainContent-- remember that's the ID of the section that this paragraph is contained in, so you are saying any paragraph inside of an element with an ID of mainContent.
Now if I save this and test this in my browser, I can see that the color it's using is red instead of blue. So if I go back to this, the cascade doesn't appear to always be right, does it, because this should be the last rule that's applied, but it's not. This one is because this selector is much more specific. We have 101 versus 1. The 101 always wins. I am going to go back down to my code, and I am going to change the paragraph element by adding a class attribute to it, so I am going to say class green.
You can probably guess where we are going to go with this. Down below, in my first paragraph selector, I am going to type in .green, so we are writing a class selector for that. And I am just going to say color: green. I am going to save this, preview it in my browser, and nothing happens. This is typically where designers just go crazy because they are like, okay, I wrote a class selector, I applied the class attribute to my element, and I'm not getting the styling. It's not working. Why is that? Well, it's not working because there is a selector elsewhere with a higher degree of specificity than the one that you're trying to use.
This has a specificity of 10. Remember this one has a specificity of 101. Now this is one of the reasons why a lot of people avoid using ID selectors. They are so specific that once you write them they are very hard to overwrite. I don't think they are a problem. I still use them. I just have to make sure that my styles are organized in such a fashion that I'm not surprised by those at any one point. What I am going to do now is go down to my strong tag, I am going to remove the class from the paragraph tag, and I am going to apply the exact same class to the strong tag. And when I save this and preview it in my browser, I can see that the text that's nested within the strong tag does indeed get that styling, so why is that? So in this case we know that green only has a specificity of 10, which shouldn't override this, right, because that has a specificity of 101.
Okay, so here's where things get a little complex. So specificity works just fine until inheritance is involved. Remember, this strong text is nested inside of this section. So we learned from inheritance that yes, those properties like color will inherit, but if a child element has a style that differs or conflicts with the parent styles, the child styles always win. So in this case we are seeing inheritance and not specificity. So you have to be pretty sure of when one's going to occur versus the other of course.
Now if I go down in my CSS and I do strong and say color: purple, for example, and save that, now when I preview this, it's not purple; it remains green--and again here specificity is the reason. Green, specificity of 10, strong specificity of 1, green wins. Now if I come here and make this a little bit more specific and say strong.green, so that is now an element-specific class selector, this now has a specificity of 11, the 10 for the class, the one for the element, which is going to be higher than the 10 here.
So if I save this and preview it, now I get the purple. So there are a couple things to remember here. Number one, you always have to keep track of how specific a rule is to know if you need to make something a little bit more specific to overwrite something. You want to try to keep your specificity of your rules as low as possible. They are easier to parse that way and they are also a lot easier to overwrite later on. And the other thing is, semantic naming matters. We named this class green and yet the color that's displaying is purple. So that specific class really doesn't mean anything.
We won't want to name that something that's a little bit more descriptive. So if you're new to CSS, keeping track of all the rules around cascade, inheritance, and specificity can be a little tricky. I totally understand that. However, I cannot stress to you enough how critical these skills are to writing your CSS properly. As your pages and your styles become more complex, being able to track down and solve conflicts will become an increasingly important skill--trust me on that. Also, having a thorough understanding of these rules will allow you to plan and write CSS that avoids these conflicts altogether.
The styles will be a lot more easy to maintain, and your styles will use the most efficient rule structure possible.
There are currently no FAQs about CSS: Core Concepts.
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.