Join James Williamson for an in-depth discussion in this video Understanding the cascade, part of CSS: Core Concepts.
- View Offline
How important is it that you understand the cascade? Well, considering that it's in the name of cascading style sheets, yeah, I'd say it's pretty important. Now I've read a lot of articles on the cascade and seen it described in a lot of different ways, but when it really comes down to it, the cascade can be summed up in one simple sentence: the last rule applied wins. Styles, you see, are applied in the order that they are found, and in the event of a conflict, the style that comes last is the one that's used.
Now like many things, it's not quite as simple as it sounds, so let's check it out, and I want to do that by opening up cascade.htm and cascade.css, which you can find in the 03_02 folder. All right, if I look at this page structure of the HTML file, you don't get much simpler than that. I just have a paragraph down there that says, "I require styling," which we're going to endeavor to do for it. The reason that the page structure is so simple is that I really want to focus on the cascade itself, so I don't want to you get caught up in any structured elements or nested elements and wondering if they're having any type of effect; I want to just to focus only cascade itself.
Okay, now the first thing I'm going to do is link to this external style sheet, so just above the style tag, I'm going to open up a link tag. I'm going to give it an href attribute of cascade.css, so that's pointing to this file right here, and they are in the same directory so I don't need to give a path; I can just point right to that. I'm going to give it a relationship of stylesheet. I'm going to go ahead and set its type to text/css. Again, that's not necessary in HTML5, but I'm just doing it to be thorough.
Okay, so there we go. That is now linking to that external style sheet. If I save this, I can flip over to the style sheet and I can go ahead and write a selector for my paragraph. So I'm just going to open up a new selector, use the p element selector, and I am going to choose the color of red. So I'm going to save that. If I go back to cascade now and preview that in one of my browsers, indeed our text is red, exactly as we wanted it to be. So if I come back into my HTML file, I notice also that I do have some embedded styles, some local styles right here. And if I go down into that section and add another paragraph selector here and I choose, say, color: blue, if I save that and preview that in the browser, when we refresh that I can see that now the styling is blue.
Remember what we said, last styled applies wins. Now a lot of people use this to their advantage. They'll have a lot of styles in an external style sheet. They'll need to just change one thing on one local page, and they'll do this: they'll go ahead and create local styles using a style tag, and then they'll overwrite that external style with just a simple selector that they know it's going to conflict with the external style, and they'll keep that local. So you'll read occasionally that embedded styles always overwrite external styles. Well, it's not entirely true. Remember what I said earlier: the last style applied wins and styles are applied in the order that they're found.
So where you put the link tag to the external style sheet and where you place the embed styles matters. So if I take this external style sheet and I cut it and then I paste it below my embedded styles, if I save this and test it again, you can see the styling is right back to red and that is because the styles are applied in the order that they're found. So as it goes down the page, it applies this style, and then it applies any styles that it finds within here, which is this style.
So it doesn't really matter, in terms of whether it's an external or embedded style; what matters is where it's found within the code. So if you use this type of a strategy where you have local styles and you have external styles that you're using as well, you want to make sure that those external styles, the link for them at least, is located above your embedded styles, just like this. Okay, now I could keep styling things. Certainly if I added another paragraph selector underneath this one, the one on the bottom would win out in the event of any type of conflict, but what about inline styles? So if I go down to the actual paragraph itself and inside that I type in style=, and then inside that, let's do color: green, so this is an inline style.
We've already written one of these so far in the title, but just to refresh your memory on the syntax, we use the style attribute, and then we set that value using quotation marks, and inside the quotation marks you just go ahead and write your properties as you would normally. You have the property and the value separated by the colon and then a semicolon, and you could keep stringing properties along if you wanted them. Okay, so this is an inline style. If I save this and test it, I can see that now it wins. Remember, the last rule applied wins. So this rule is applied first. Go down the page and parse it, then apply this one.
I go down the page and parse it and then finally apply this one. Now inline styles are a little different because they also have several other things they take into account: specificity, which we'll talk about in just a moment, and inheritance, which we're going to talk about in just a moment as well. But inline styles almost always win in the event of a conflict. So it's really important to understand the cascading nature of styles, especially when you're planning styles for larger sites. Again, if you have any type of a mixture of internal and embedded styles, or even duplicate styles within the same style sheet, the cascade is definitely going to come into play.
Once you understand that browsers are going to apply those styles in the order that they are found, it makes it a lot easier to start planning styles that can avoid unnecessary conflicts and take advantage of the cascade to overwrite styles where you actually want to overwrite them.
- Exploring default styling
- Writing a selector
- Setting properties
- Working with common units of measurement, including ems and pixels
- Structuring HTML correctly
- Understanding the cascade and inheritance
- Setting a font family, font size, text color, and more
- Understanding the box model
- Styling container elements
- Working with RGB vs. HSL values
- Styling drop shadows