Things like color, cursor, font properties, letter-spacing, line-height, list-style, text-align, text-indent, and visibility are inherited from the parent element. For example, if the font-size for a <p> is 12px and there is an <a> link in that paragraph, then it also will appear as 12px. Things like border, padding, margin, and a whole lot more, are not inherited.
The following text is from the W3C specification. The first line must be read slowly because it is the key to understanding how the page is styled. As we write CSS, we are specifying a Selector and then Property/Value pairs. The browser, however, applies Values to an element identified in the Selector.
To find the value for an element/property combination, user agents must apply the following sorting order:
Apart from the "!important" setting on individual declarations, this strategy gives author's style sheets higher weight than those of the reader. User agents must give the user the ability to turn off the influence of specific author style sheets, e.g., through a pull-down menu.
While the W3C description is certainly accurate, it takes a lot of brain power to digest and remember it all. A practical summary reads more like (in descending order of importance):
<element style="...">
)The above list is an over-simplification and it is not entirely correct. For more information, check out the specification.
Each CSS property is given a specificity (how powerful the value is) when the browser styles the web page. There are four "levels" of power when determining the final specificity. Think of it like magic levels.
Let’s look at some examples:
In this example there are three tag selectors (muggles), namely div, article, and p. There is one class selector (Hogwarts students), no id’s, and no style attributes. This is often expressed in the format [0,0,1,3]. | div article p.bookTitle { font-weight:normal; } |
The specificity in this case is one id (wizard). [0,1,0,0] | #bookOne { font-weight:bold; } |
Let’s say our HTML is:
<div> <article> <p id="bookOne" class="bookTitle">Harry Potter and the Sorcerer's Apprentice</p> </article> </div>
We have a conflict for font-weight properties. On one hand, because the paragraph is a decendent of an article and div, and it has the class bookTitle, it should have a font-weight of normal. On the other hand, the paragraph also has an id of bookOne that gets the bold value. In this case, one experienced wizard is stronger than any number of Hogwarts students and muggles combined.