Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In addition to the cascade, another concept that comes into play when resolving conflicts is inheritance. Many CSS properties will inherit, that is, if you apply the property to a parent element, all the elements inside of it will have that property applied to it as well. Now that not only makes sense, it can save you, as an author, a tremendous amount of time. If you wanted to use Arial for example, you could simply apply the font-family property of Ariel to the body element, and let every element inside the body tag inherit it.
Of course, there comes a time when you're going to want child elements to have formatting that differs from their parent. So in the event of a conflict, child element styles will overwrite parent element styles. All right, let's try that out. I have the inheritance.htm file open from the 03_03 folder, and just to look at the structure of the page really quickly here, it's very simple. Within the body we've got a section element, inside that we have a paragraph that also has a strong tag inside of it.
And then directly following that, we have an aside, with another paragraph, and a nested strong tag inside of that. So a simple structure, but we do have some nested elements on the page as well. One of the easiest ways to visualize inheritance is to apply styles to a parent tag and let them inherit down, so we're going to go ahead and do that with our first rule, so I'm just going to click right here, inside the style tag where I have the add styles here, and we'll write our first selector, and we're going to do a selector for the body tag. And inside that I'm just going to type in font-family, we'll go ahead and do Georgia, just like we talked about earlier.
And I'll do font-size, let's say 120%, and just below that we'll do a color, and we'll just go ahead and make it green. So I'm going to go ahead and save this, and then I'm just going to preview this in one of my browsers. And as you can see, all the elements on the page are green, they're Georgia, and they're a little bit larger than the default font setting because we set the size of the text to 120%. Just so we clearly see that every single element on the page is inheriting this, not just the stuff that's on the page right now, I'm going to go back into my code, and inside the section just above the paragraph, I'm not going to add another element, I'm just going to add some text. I'm going to type in, I'm just text within the section.
We don't need add another element, I'm just going to add some text right there within the section, save this, and when I go back to my browser, and refresh the page, you can see the text shows up, and it has exactly the same formatting as all the other text on the page, even though it wasn't there originally, and even though it's not inside of a paragraph. It's inheriting the same styling as all the other elements on the page. So that demonstrates for us the ways that styling applied to a parent tag is inherited by all of the nested elements inside of it. But what about having child elements overwrite that parent styling? Let's go ahead and experiment with that.
So I'm going to go back into my code and I'm just going to change this text here a little bit. Number one, I'm going to replace that with an h1 tag, and inside the h1 tag I'm going to type in, This is a test of inheritance. I'm going to go ahead and save that, and again, I'm going to switch back over to my browser and refresh the content again. Now, everything looks the same for the most part. I mean it's green and it is Georgia, but there's one thing very different and I think it jumps right out at you, it's the size of the text. The size is much different.
Now, we didn't explicitly set a size for the h1 did we? So how in the world is the h1 actually larger than everybody else, when we clearly told the body tag to be 120% and we're expecting that font size to inherit every other element on the page? Well, there's a little something going on here. Now remember, we talked about this a little bit earlier, all browsers have default style sheets that have set formatting rules inside of them, and one of those formatting rules for each of the elements on the page, is a font size. So even though we said 120%, what's happening here is that the h1, and for that matter the paragraphs as well, their value is overriding the body's font size setting. Although that's not entirely accurate either. It's not really overwriting it.
It's more of a cumulative effect, and let me show you what I mean by that. I'm going to go back to the code, just below the body tag, I'm going to go ahead and do a selector for the sections. So remember, the section element is sort of that top section, the one just below that is an aside. So here, I'm going to type in font-size, and let's go ahead and make that 1.4ems, and color is going to be red. So we're going to go ahead and save this, go back to my browser and refresh it.
That is a huge change, so what's happening here? Well, first off, we're seeing red. What's happening there, of course, is that the elements inside of the section, all of those elements, the paragraph and the heading, they're inheriting red from section. Sections red is actually overriding the parent body tags green, but what about size? I'm going to go back to my code for just a moment here. So what we're doing here is, we're using an em, which is a relative unit of measurement. What it basically means is, I want it to be 1.4 times the size of something. So here's what's happening, in the body tag we're telling our font size to be 120%.
So it's telling the browser, or whatever device this is being rendered on, take all of your text and make it about 120% bigger than it would normally be. So your headings will still be larger than paragraphs, they'll just be 120% larger than they would have been originally, paragraph default size will be 120% of what it would've been. So now what section is doing is, section is saying hey, take that computed value, whatever it may be, and multiply it by 1.4 times what it was, and make it that much larger. So that's one of the interesting things about inheritance. Most of the time it's a simple overwrite. Properties are either being inherited or they're being overwritten.
But in some instances, especially when you're using relative units of measurement, the effect will be cumulative, so that does occur as well. Finally, if I go in, and just below section I write another selector, and this time just for my paragraphs, so p. I'm just going to do color, and we're going to do blue for that. Now let's think about this before we test this. I'm going to go ahead and save this, but let's think about this for a moment. So, body is telling everybody to be green, so all elements on the page are going to be green. Section is saying okay, but everybody inside me I want red, and paragrahps are saying, no no no no, I want to be blue.
So, if we go back over to our browser and refresh it, we can see exactly what happens. The heading remains red because again, it's inheriting that from the section. Each of the paragraphs however, is now blue, and there's nothing on the page that's green, because everything else is getting a value either from a child element or inheriting it from an element that's a little bit closer to the source. Before we wrap up, there's one more thing I wanted to point out. You may have noticed that the strong tags -- I didn't really talk about them a whole lot, but they're bold right? And that's because their default value of font-weight is overriding the normal value of the paragraph because again, it is a child element. On the surface inheritance is an easy concept to understand, however, when you consider the complexity of most sites, along with having to account for user agents default styling, it's not always easy to keep up with all the different sources that an element can receive styling from. That's one of the reasons why it's so important to keep your styles organized as you plan and write them. You know, inheritance also gives us an easy way to write really efficient styles. We simply write the most basic global styles on parent elements, and then overwrite them using inheritance when you need nested elements to be formatted differently. Now this prevents you from having to write multiple rules for every child element, and makes updating content much easier. You know, as with any technique, mastering inheritance takes a little bit of time, so as you start out, be very mindful of how parent and child styles are affecting each other, and over time, using inheritance will just become second nature.
Get unlimited access to all courses for just $25/month.Become a member
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.
Your file was successfully uploaded.