Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Using inheritance

From: CSS: Core Concepts

Video: Using inheritance

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.

Using inheritance

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.

Show transcript

This video is part of

Image for CSS: Core Concepts
CSS: Core Concepts

81 video lessons · 39985 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 57s
    1. Welcome
      55s
    2. Using the exercise files
      4m 2s
  2. 1h 7m
    1. Exploring default styling
      4m 56s
    2. CSS authoring tools
      2m 29s
    3. CSS syntax
      4m 45s
    4. Writing a selector
      4m 10s
    5. Setting properties
      8m 40s
    6. Common units of measurement
      7m 47s
    7. Inline styles
      5m 1s
    8. Embedded styles
      5m 19s
    9. Using external style sheets
      10m 34s
    10. Checking for browser support
      8m 48s
    11. Dealing with browser inconsistencies
      5m 30s
  3. 2h 15m
    1. Structuring HTML correctly
      2m 51s
    2. Element selectors
      4m 52s
    3. Class selectors
      6m 4s
    4. ID selectors
      3m 27s
    5. Using classes and IDs
      10m 7s
    6. Element-specific selectors
      4m 35s
    7. The universal selector
      5m 42s
    8. Grouping selectors
      4m 49s
    9. Descendent selectors
      7m 32s
    10. Child selectors
      5m 7s
    11. Adjacent selectors
      5m 30s
    12. Attribute selectors
      12m 43s
    13. Pseudo-class selectors
      3m 54s
    14. Dynamic pseudo-class selectors
      8m 29s
    15. Structural pseudo-class selectors
      6m 45s
    16. Nth-child selectors
      13m 10s
    17. Pseudo-element selectors
      12m 40s
    18. Targeting page content: Lab
      8m 56s
    19. Targeting page content: Solution
      7m 59s
  4. 42m 39s
    1. What happens when styles conflict?
      4m 0s
    2. Understanding the cascade
      5m 47s
    3. Using inheritance
      6m 11s
    4. Selector specificity
      6m 55s
    5. The !important declaration
      4m 5s
    6. Reducing conflicts through planning
      3m 33s
    7. Resolving conflicts: Lab
      6m 45s
    8. Resolving conflicts: Solution
      5m 23s
  5. 1h 47m
    1. Setting a font family
      7m 10s
    2. Using @font-face
      9m 18s
    3. Setting font size
      7m 35s
    4. Font style and font weight
      6m 52s
    5. Transforming text
      3m 58s
    6. Using text variants
      2m 49s
    7. Text decoration options
      4m 26s
    8. Setting text color
      3m 2s
    9. Writing font shorthand notation
      8m 49s
    10. Controlling text alignment
      6m 33s
    11. Letter and word spacing
      9m 11s
    12. Indenting text
      4m 30s
    13. Adjusting paragraph line height
      10m 30s
    14. Controlling the space between elements
      6m 41s
    15. Basic text formatting: Lab
      8m 45s
    16. Basic text formatting: Solution
      7m 14s
  6. 2h 1m
    1. Understanding the box model
      16m 53s
    2. Controlling element spacing
      14m 29s
    3. Controlling interior spacing
      10m 49s
    4. Margin and padding shorthand notation
      6m 27s
    5. Adding borders
      8m 57s
    6. Defining element size
      10m 7s
    7. Creating rounded corners
      6m 58s
    8. Background properties
      2m 51s
    9. Using background images
      5m 10s
    10. Controlling image positioning
      10m 25s
    11. Using multiple backgrounds
      7m 5s
    12. Background shorthand notation
      5m 25s
    13. Styling container elements: Lab
      7m 55s
    14. Styling container elements: Solution
      8m 17s
  7. 47m 51s
    1. Color keyword definitions
      5m 4s
    2. Understanding hexadecimal notation
      6m 5s
    3. Using RGB values
      4m 58s
    4. Using HSL values
      5m 17s
    5. Working with opacity
      2m 23s
    6. Using RGBa and HSLa
      3m 8s
    7. Styling drop shadows
      5m 38s
    8. CSS gradients
      6m 32s
    9. Working with color: Lab
      4m 26s
    10. Working with color: Solution
      4m 20s
  8. 1m 58s
    1. Additional resources
      1m 58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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.


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.

Join now "Already a member? Log in

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 CSS: Core Concepts.

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


OK
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
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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.