css inheritance - a simple step by step tutorial · css inheritance. let’s start with the...

95
CSS INHERITANCE

Upload: others

Post on 20-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

CSSINHERITANCE

Page 2: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Let’s startwith the

documenttree

Page 3: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Before we explore inheritance, weneed to understand the

document tree.

Page 4: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

All HTML documents are trees.

Page 5: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Document trees are made fromHTML elements.

Page 6: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The document tree is just likeyour family tree.

Page 7: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

An ancestor refers to anyelement that is connected butfurther up the document tree.

Ancestor

Page 8: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

A descendant refers to anyelement that is connected but

lower down the document tree.

Descendant

Descendants

Page 9: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

A parent is an element that isconnected & directly above anelement in the document tree.

Parent

Page 10: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

A child is an element that isconnected & directly below anelement in the document tree.

Child

Page 11: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

A sibling is an element thatshares the same parent as

another element.

Parent

Siblings

Page 12: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Next, a bitabout

CSS rules

Page 13: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

We also need to understand thebasics of CSS rules before

exploring inheritance.

Page 14: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

CSS rules tell browsershow to render specific elements

on an HTML page.

Page 15: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

CSS rules are made up offive components.

Page 16: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The selector "selects" theelements on an HTML page that

are affected by the rule.

p { color: red; }

Selector

Page 17: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The declaration block is acontainer that consists of

anything between (and including)the brackets.

p { color: red; }

Declaration block

Page 18: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The declaration tells a browserhow to render any element on a

page that is selected.

p { color: red; }

Declaration

Page 19: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The property is the aspect of thatelement that you are choosing to

style.

p { color: red; }

Property

Page 20: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The value is the exact style youwish to set for the property.

p { color: red; }

Value

Page 21: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Now…what is

inheritance?

Page 22: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Inheritance is where specific CSSproperties are passed down to

descendant elements.

Page 23: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

To see inheritance in action, wewill use the HTML code below:

<p>Lorem <em>ipsum</em> dolor sit amet consect etuer.

</p>

Page 24: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Note that the <em> element sitsinside the <p> element.

Page 25: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

We will also use this CSS code.Note that the <em> element has

not been specified.

p { color: red; }

Page 26: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

In a browser, the <p> and <em>elements will both be colored

red.

<em> element

Page 27: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

But why is the <em> elementcolored red when it has not been

styled?

Page 28: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Because the <em> element hasinherited the color property from

the <p> element.

Page 29: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Why isinheritance

helpful?

Page 30: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Inheritance is designed to makeit easier for authors.

Page 31: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Otherwise we would need tospecify properties for alldescendant elements.

p { color: red; }p em { color: red; }

Page 32: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

CSS files would be much largerin size, harder to create andmaintain as well as slower to

download.

Page 33: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Are all CSSpropertiesinherited?

Page 34: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

No. All CSS properties arenot inherited!

Page 35: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

If every CSS property wasinherited, it would make things

much harder for authors.

Page 36: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Authors would have to turn offunwanted CSS properties for

descendant elements.

Page 37: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

For example, what would happenif the border property was

inherited by default…

Page 38: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

and we then applied a border tothe <p> element?

p { border: 1px solid red; }

Page 39: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The <em> inside the <p> wouldalso have a red border.

<em> element

Page 40: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Luckily, borders are notinherited, so the <em> would not

have a red border.

<em> element

Page 41: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Generally speaking, onlyproperties that make our job

easier are inherited!

Page 42: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

So, whichproperties are

inherited?

Page 43: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The following CSS properties areinherited…

Page 44: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 45: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Yikes! That is a lot of properties.

Page 46: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

To simply things, let’s take a lookat some of the

key groups of properties.

Page 47: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Text-related properties that areinherited:

Page 48: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 49: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

List-related properties that areinherited:

Page 50: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 51: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

And, importantly, thecolor property is inherited:

Page 52: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

azimuth, border-collapse, border-spacing,caption-side, color, cursor, direction, elevation,

empty-cells, font-family, font-size, font-style,font-variant, font-weight, font, letter-spacing,

line-height, list-style-image, list-style-position,list-style-type, list-style, orphans, pitch-range,pitch, quotes, richness, speak-header, speak-numeral, speak-punctuation, speak, speech-

rate, stress, text-align, text-indent, text-transform, visibility, voice-family, volume, white-

space, widows, word-spacing

Page 53: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Is font-sizeinherited?

Page 54: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The simple answer is “yes”.However, font-size is inherited ina different way to many other

properties.

Page 55: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Rather than the actual valuebeing inherited, the calculated

value is inherited.

Page 56: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Before explaining how font-sizeinheritance works, we need to

look at whyfont-size is not directly

inherited.

Page 57: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Let’s start with thesame sample of HTML code we

used earlier:

<p>Lorem <em>ipsum</em> dolor sit amet consect etuer.

</p>

Page 58: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

As before the <em>sits inside the <p>.

Page 59: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Now, a font-size is applied to the<p> element only. The <em> has

not been specified.

p { font-size: 80%; }

Page 60: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

If the font-size value of 80% wereto be inherited, the

<em> would be sized to 80%of the <p> element…

Page 61: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

and the rendered documentwould look like this:

<em> element

Page 62: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

However, this is not the case!The <em> is the same size as the

<p>.

<em> element

Page 63: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

So how does inheritance work forfont-size?

Page 64: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Let’s look at three examplesin action.

Page 65: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

We will use the same HTMLcode as before:

<p>Lorem <em>ipsum</em> dolor sit amet consect etuer.

</p>

Page 66: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Which produces the samedocument tree as before.

Page 67: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Example 1:Pixels

Page 68: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The <p> element has been givena font-size of 14px.

Note: pixels are not recommended for sizing fonts due to accessibilityissues associated with older browsers such as IE5 and IE6.

p { font-size: 14px; }

Page 69: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

This pixel value (14px) overridesthe browsers default font-size

value (approx 16px). This newvalue is inherited by

descendants.

Page 70: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

element value calcuated value

default font size approx 16px

<body> unspecified approx 16px

<p> 14px 14px

<em> unspecified inherited value = 14px

So, the <em> element inherits the14px value.

Page 71: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Example 2:Percentage

Page 72: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The <p> element has been givena font-size of 85%.

p { font-size: 85%; }

Page 73: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The browsers default font-size(16px) and the percentage value

(85%) are used to create acalculated value (16px x 85% =

13.6px). This calculated value isinherited by descendants.

Page 74: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

element value calcuated value

default font size approx 16px

<body> unspecified approx 16px

<p> 85% 16px x 85% = 13.6px

<em> unspecified inherited value = 13.6px

So, the <em> element inherits the13.6px calculated value.

Page 75: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Example 3:EMs

Page 76: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The <p> element has been givena font-size of .85em.

Note: Avoid using EMs for font-size values under 1em as IE5 renders thesevalues in pixels instead of EMs (.8em is rendered as 8px).

p { font-size: .85em; }

Page 77: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The browsers default font-size(16px) and the EM value (.85em)are used to create a calculatedvalue (16px x .85em = 13.6px).

This calculated value isinherited by descendants.

Page 78: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

element value calcuated value

default font size approx 16px

<body> unspecified approx 16px

<p> .85em 16px x .85em = 13.6px

<em> unspecified inherited value = 13.6px

So, the <em> element inherits the13.6px calculated value.

Page 79: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Those examples were too simple.What about more complexexamples using different

elements?

Page 80: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Example 4:

Page 81: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

All elements have been specifiedusing percentage values.

body { font-size: 85%; }h1 { font-size: 200%; }h2 { font-size: 150%; }

Page 82: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

The browsers default font-size(16px) and the body percentagevalue (85%) are used to create acalculated value (16px x 85% =13.6px). This calculated value isinherited by descendants unless

new values are specified.

Page 83: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

element value calculated font-size

default font size approx 16px

<body> 85% 16px x 85% = 13.6px

<h1> 200% inherited value 13.6px x 200% = 27.2px

<h2> 150% inherited value 13.6px x 150% = 20.4px

<p> unspecified inherited value = 13.6px

<em> unspecified inherited value = 13.6px

The font-size inheritance inaction

Page 84: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Using inheritancefor efficiency

Page 85: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Authors can use inheritance towrite efficient CSS.

Page 86: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

For example, you can set thecolor, font-size and font-family on

the body element.

body { color: #222;font-family: arial, helvetica, sans-serif;font-size: 90%;

}

Page 87: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

These properties will be inheritedby all descendant elements.

Page 88: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

You can then override theproperties as needed, specifying

new color values...

Page 89: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

body { color: #222;font-family: arial, helvetica, sans-serif;font-size: 90%;

}

h1, h2, h3 { color: green; }h4, h5, h6 { color: black; }

Page 90: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

new font-family values...

Page 91: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

body { color: #222;font-family: arial, helvetica, sans-serif;font-size: 90%;

}

h1, h2, h3 { color: green; }h4, h5, h6 { color: black; }

h1, h2, h3, h4, h5, h6 { font-family: georgia, times, serif;

}

Page 92: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

and new font-size values asneeded.

Page 93: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

}

h1, h2, h3 { color: green; }h4, h5, h6 { color: black; }

h1, h2, h3, h4, h5, h6 { font-family: georgia, times, serif;

}

h1 { font-size: 200%; }h2 { font-size: 150%; }h3 { font-size: 125%; }#footer { font-size: 90%; }

Page 94: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

Now, go forth andinherit the world!

Page 95: CSS Inheritance - a simple step by step tutorial · CSS INHERITANCE. Let’s start with the document tree. Before we explore inheritance, we need to understand the document tree

We’re done!