Programming css: how to write excellent code
Home
 
THE IMPORTANCE OF INHERITANCE IN CSS

 

 

Many styles can be inherited from an element to its descendant elements. Any inherited style will be applied to an element unless the property in question is explicitly set through a rule whose selector matches the element. For example, consider these rules on a css quiz which is valid also for a myspace css:

body {color: black;}
p {color: green;}

Assumed this, the color of any paragraph will be green, despite any conflict between styles, while the color of all other elements will be black. Note that this overriding of inherited styles takes effect no matter what specificity or importance was attached to the original rule. For example:

div#summary {color: black !important;}
p {color: green;}

 


Any paragraphs within a div whose id attribute has a value of summary will still be green, because the explicitly assigned style overrides the inherited style. However, all properties (except for page) can be given a value of inherit. This directs the user agent to determine the value of the property for the parent element, and use that value for the current element. Thus, p{color:inherit;} will set the color of any paragraph to be the same color as its parent.
This has the advantages of upgrading the inheritance mechanism such that a style can be explicitly assigned to inherit, instead of relying on the normal inheritance mechanism.

There are a few properties in CSS which are considered shorthand properties; that is, they represent a much larger collection of properties. For example, margin is a shorthand for the properties margin-top, margin-right, margin-bottom, and margin-left. The following two rules will have exactly the same effect:

p {margin: 1em;}
p {margin-top: 1em;
margin-right: 1em;
margin-bottom: 1em;
margin-left: 1em;}

Because of this, authors must be sure to avoid conflicts between properties and shorthands, or even between two shorthand properties. For example, consider the following two rules as matching the same element:

pre.example {margin: 1em;}
pre {margin-left: 3em;}

Due to the operation of the cascade, any pre element with a class of example will have a margin 1em wide, including the left margin. The shorthand’s effects have masked out the value assigned in the pre rule. Another good example involves text-decoration, which is a shorthand for no properties at all but acts much as a shorthand property does. Consider the following rules:

h2 {text-decoration: overline;}
h2, h3 {text-decoration: underline;}

Given these rules, all H2 elements will be underlined but not overlined. The given values of text-decoration do not combine, as each combination of keywords is its own unique value. If it is desirable to decorate H2 elements with both an underline and an overline.

Musiknoter
Notböcker, Musiknoter, Noter. Vi erbjuder Sveriges största utbud av noter. 

Kattshop och Hundbutik
För katter och kattägare. Även hundartiklar. Halsband, sängar, matskålar, leksaker, böcker, tidningar, hundkläder… ja det mesta kan Du finna här. 

Camboriu Online Shopping - Onde Comprar e Vender de Tudo em Balneário Cambo
A maior Comunidade de compra e venda online de Balneário Camboriú. 

 

 

 

 

 

 

 

 



Copyright ADVANCED CSS WEB SOLUTIONS    |   All rights reserved