In the exercise above, we created a CSS rule for the <p> tag element to define both the text size and the margins that surround text that is inside <p> tags. When you look at the index.html page, it may appear as though the CSS Layout already has a defined font—even though we haven’t set one. This is due to inheritance. When a CSS style is set, it will affect the element itself, as well as any other page elements within it. In this case, the <p> tag surrounds all of the text elements on the index.html page, causing the text to display differently when different values are set.
Let’s examine how inheritance works. To follow along with this next exercise, launch Dreamweaver and open index.html with the Document window set to Design view. Also, if it is not already open, access the CSS Styles panel by selecting Window > CSS Styles.
Notice that the text content in index.html is now displayed in red.
In this example of inheritance, the text is inheriting its color value from the body property. If you look at the CSS Styles panel again, you’ll see the body has its font property set to 100% Verdana, Arial, Helvetica, sans-serif. Since the body surrounds all of the other elements on the page, changes made to the CSS rule for the body will cause all other elements to inherit the same styles throughout the page.
<p> tags) remains black.In the example above, we’ve seen that when you add a color specifically to the p element, it overrides the inherited color of black that was applied to the body rule. This is the basis of specificity, where the CSS rule applied to a tag that is most specific to a page element overrides the inherited values of other CSS rules. In this case, the color setting on the p rule is more specific to the text than the color setting on the body rule. That is why settings made to the p tag override the inherited settings of the body tag. This is an important concept when working with CSS, and you can use this to great advantage when developing your style sheets. It is also critical that you understand how this works, because it will help you troubleshoot your CSS rules as you design them.
To learn more about inheritance, see the Developer Center article, Understanding Inheritance. To find out more about how specificity works and how to use it to your advantage, read Understanding Specificity.