Adobe
Products
Creative Suite
Photoshop Family
Acrobat Family
Flash Platform
Digital Marketing Suite
Digital Publishing Suite
More products
Solutions
Digital marketing solutions
Digital media solutions
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Store
Adobe Store for home and home office
Education Store for students, educators, and staff
Business Store for small and medium businesses
Other ways to buy
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections   Search  
Solutions Company
Help Learning
Sign in Welcome, My orders My Adobe
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Dreamweaver Developer Center /

Understanding inheritance

by Adrian Senior

Adrian Senior
  • www.webade.co.uk
  • www.communitymx.com
  • www.ukcsstraining.co.uk

Content

  • Introducing the document tree
  • Setting a default color
  • Streamlining your CSS file

Created

24 July 2006

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
CSS design Dreamweaver 8 inheritance website

Requirements

Prerequisite knowledge

Basic knowledge of using Dreamweaver and CSS.

User level

Beginning

Required products

  • Dreamweaver (Download trial)

Sample files

  • inheritance_samples.zip (3 KB)

Note: This article was written for Dreamweaver 8. However, the content in general is still valid for Dreamweaver CS5, even though screenshots may differ.

Certain properties in a cascading style sheet (CSS) inherit (read assume) the values of their parent. For example, if you have an h1 element that is colored black and that h1 element has a nested span within it, then by default the span will also be black—it inherits the color value from its h1 parent.

In this article you will discover how you can use inheritance to optimize your CSS file.

Introducing the document tree

A document tree is very much like a human family tree—it contains parents, children, and ancestors that relate to each other as they would in your family tree. In the document tree, however, these relationships are represented by elements, such as lists, title elements, and paragraphs.

The sample files that accompany this article include a page called tree1.html; open this page in Dreamweaver Design view. It contains an unordered list, an h2 element, and a paragraph that contains an em element (see Figure 1).

Sample file tree1.html in Dreamweaver Design view
Figure 1. Sample file tree1.html in Dreamweaver Design view

Figure 2 shows the structure of the tree1.html document drawn as a document tree. You can see that each element on the page bears a relationship with the body element, while the p, ul, and h2 elements reside in their own family tree branches and bear no relationship to each other. The elements that show below each of these elements are related to their parent—em is a child of p and the li elements are children of the ul element. The h2 element is a bit of a lonely old soul having no relatives at all.

Document tree view 1
Figure 2. Document tree view 1

You can demonstrate how color is inherited by setting a color property and value against the body. If no color values have been set, then generally your text color will default to black. This is represented by the colored (black) boxes.

Setting a default color

If you created a rule in your style sheet that looked like the following rule (in bold), then all elements within your page would inherit that color value:

<style type="text/css"> body {color: #990000;} </style>

Figure 3 shows the changes to the appearance of the document tree based on the tree1.html file after the previous rule has been set.

All elements are now colored in a red.
Figure 3. All elements are now colored in a red.

After a color has been set on the body element, it becomes necessary to set a color against an individual element in order to override the color value set on the body element. If you wanted to make the color of your h2 element green, then you would need to set that color against that element. You would, of course, do this by adding a new rule to your style sheet for the h2 element, for example:

h2 {color: green;}

When a value is specifically set against an element, it will override the inherited color. This enables your h2 element to show green as you requested. This does not affect any other elements as your h2 element does not have any children (see Figure 4). You can see this in the file tree2.html, which is also included in the sample files that accompany this article.

The h2 element is now green.
Figure 4. The h2 element is now green.

Let's make a change to the color of the p element; currently it inherits the default color of the body:

p {color: blue;}
the p element color is set to blue.
Figure 5. the p element color is set to blue.

Open tree3.html and you will see that after you add the previous rule to your style sheet, the p element and the em element (which is a child of the p element) both become blue. This is specificity at work; the color value set against the p element is more specific than the color value set against the body element, so the color value against the p wins out. The em element within the p element then inherits the color from the p element, which is its parent element.

While all this color changing has been going on, your ul and li elements have remained unchanged. They are still showing the default color value that you declared on the body element. The reason these elements are still red is that they are not related to either the h2 or the p elements, so they remain totally unaware of the changes that have taken place elsewhere in your document. To affect the color of the ul and li elements you would need to specifically set a color value for them within their own rule in your style sheet.

See if you can change the color of the li elements to orange using the same technique that you have used to change the color of the p and h2 elements.

Many values you set in your CSS file for styling text will be inherited, although the padding, margin, and positional properties remain specific to the element they are assigned to. This means you can use inheritance to set line-height, font-weight, font-size, and font-family properties to optimize your CSS file, as you will see in the following section.

Streamlining your CSS file

In the past, you may have created redundancy in your style sheet by setting values that are simply not required. Often such values could have been omitted and you could have let inheritance do all the work for you. Using inheritance benefits you in several ways—it reduces the size of your CSS file which in turn makes it easier to read, easier to debug, and easier to maintain. Let's look at an example of how you can optimize your CSS file by using inheritance to do the donkey work for you. The following style sheet contains a lot of redundant code:

body { color: black; font-family: Arial, Helvetica, sans-serif; line-height: 120%; font-size: 100.01% } p { color: black; font-family: Arial, Helvetica, sans-serif; line-height: 120%; font-size: 80% } h2 { color: black; font-family: Arial, Helvetica, sans-serif; line-height: 120%; font-size: 120% }

Note: The odd font-size value of 100.01% set on the body element is to work around scaling issues at smaller sizes in some browsers.

The following optimized style sheet takes advantage of inheritance, and the file size is therefore drastically reduced:

body { color: black; font-family: Arial, Helvetica, sans-serif; line-height: 120%; font-size: 100.01% } p { font-size: 80% } h2 { font-size: 120% }

In this example, the properties in the h2 and p rules have been drastically reduced in size. As you have now allowed the properties that inherit to do their job, you only had to adjust the text size in the p and h2 elements, each of which is scaling off the default body size. All the other values for p and h2 will inherit from the body rule; and you have already seen how you can override default values from the body element if you need to.

Font scaling can be a little tricky at times, especially if you are unaware of how the font size is calculated. In the previous example, you have a default size on the body and then you have scaled the p element to 80%, which means the p element will be 80% of the default size set on the body rule. 80% is the equivalent of 0.8em and is a good default size for your paragraph text.

If you had a div element in your page (as shown in the sample file tree4.html) and you had set the following font-size value for that div, then you would be in a little bit of trouble:

div {font-size: 80%;}

With the font size of 80% set on the div any p elements within it that div will have the font size decreased. The p element was originally scaling off the body which has a default value of 100.01%. Now the containing element, the div, has a default value of 80%; your p element will scale to 80% of 80%. This is quite a bit smaller than you would want.

You should try to avoid setting the font size in more than one location. If you do find it necessary to set a font size on a containing element such as a div, then you may find it necessary to increase the font-size value on elements contained within that div.

div p {font-size: 100%;}

By using a descendant selector to increase your font size, you can increase the size of the text within any given element, in this case a div. A descendant selector will override the p element that you set earlier, because it is more specific.

Where to go from here

Now that you understand how you can use inheritance to your advantage you should read the "Understanding Specificity" article. This article explains how your rules are applied and how one rule is deemed to be more specific than another.

More Like This

  • Dreamweaver CS5 Missing Manual excerpts: Behaviors, site management, and templates
  • Using and customizing jQuery Mobile themes
  • Building scalable websites with Dreamweaver
  • Creating your first website – Part 2: Creating the page layout
  • Creating your first website – Part 6: Publishing your website
  • Creating your first website – Part 4: Adding the main image text
  • Creating your first website – Part 3: Adding content to pages
  • Getting to know your CSS selectors – Part 2: CSS3 pseudo-classes and pseudo-elements
  • HTML5 and CSS3 in Dreamweaver CS5.5 – Part 1: Building the web page
  • HTML5 and CSS3 in Dreamweaver CS5.5 – Part 3: Using CSS media queries to target different screen resolutions

Tutorials and samples

Tutorials

  • Mobile app with PhoneGap – Part 7: Submitting to the Apple App Store
  • PhoneGap and Dreamweaver: Releasing on iOS
  • Mobile app with PhoneGap – Part 5: Submitting to Android Market
  • Mobile app with PhoneGap – Part 4: Creating a release build for Android

Samples

  • Responsive design with jQuery marquee
  • Customizable starter design for jQuery Mobile
  • Customizable starter design for HTML5 video
  • Customizable starter design for multiscreen development

Dreamweaver user forum

More
02/07/2012 Any testing server experts out there?
02/07/2012 Another quick CSS layout question - vertical align:middle
01/25/2012 Dreamweaver and CMS
02/07/2012 Cannot insert a background image in an editable ID style

Dreamweaver Cookbook

More
11/07/2011 Simple social networking share buttons
09/20/2011 Registration form that will generate email for registrant to validate
08/21/2011 Spry Accordion - Vertical Text - Auto Start on Page Load - Mouse Over Pause
08/17/2011 Using cfdump anywhere you like

Products

  • Creative Suite
  • Photoshop Family
  • Acrobat Family
  • Flash Platform
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Mobile apps

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • Adobe Store
  • For students and educators
  • For small and medium businesses
  • For enterprises
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • Pacific - English
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).

Ad Choices

Reviewed by TRUSTe: site privacy statement