css - chitu.okoli.org€¦ · inheritance is where css properties are passed down from parent...

82
CSS INHERITANCE

Upload: others

Post on 20-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

CSSINHERITANCE

Page 2: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

What is inheritance?

Page 3: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Inheritance is where CSS properties are passed down from parent elements to descendant elements.

Page 4: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* Examples of CSS properties */!p { font-family: arial, sans-serif; }!p { font-size: 80%; }!p { font-weight: bold; }!p { text-align: right; }!p { color: red; }!p { line-height: 1.4; }

Page 5: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

In the following document tree diagram, the <body> element is the ancestor. The <p> and <em> elements are descendants.

Page 6: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

html

head body

title p

em

Parent

Descendant

Descendant

Ancestor

Page 7: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

To see inheritance in action, we will look at an <em> element inside a <p> element.

Page 8: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

html

head body

title p

em

Page 9: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

This would produce the following HTML markup:

Page 10: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

<!DOCTYPE html>!<html>!<head>!! <title>Simple HTML page</title>!</head>!<body>!<p>!! Lorem <em>ipsum</em> dolor etuer.!</p>!</body>!</html>

Page 11: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Lorem ipsum dolor etuer.

Page 12: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

What would happen if we styled the <p> element to “color: red”?

Page 13: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* basic CSS rule */!p { color: red; }

Page 14: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

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

Page 15: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Lorem ipsum dolor etuer.

Page 16: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

But why is the <em> element colored red when it hasn’t been given any style of its own?

Page 17: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Because the <em> element (the child element) has inherited the color property from the <p> element (the parent element).

Page 18: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Exercise 1: color red

Page 19: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Why is it helpful?

Page 20: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Inheritance is designed to make it easier for authors. Otherwise we would need to style all descendant elements as well as ancestors.

Page 21: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* if inheritance didn’t exist */!p { color: red; }!p em { color: red; }

Page 22: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

CSS files would be much larger in size, harder to create and harder to maintain.

Page 23: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

All properties?

Page 24: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Many CSS properties are not inherited. If every CSS property was inherited, it would make things much harder for authors.

Page 25: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Authors would have to turn off unwanted CSS properties for descendant elements.

Page 26: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

For example, what would happen if the border property was inherited by default, and we then applied a border to the <p> element?

Page 27: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* adding border to the <p> element */!p { border: 1px solid red; }

Page 28: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The <em> inside the <p> would also have a red border - even though we may not want this to occur.

Page 29: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Lorem ipsum dolor etuer.

Page 30: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Luckily, the border property is not inherited, so the <em> would not have a red border.

Page 31: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Generally speaking, only properties that make our job easier are inherited!

Page 32: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Exercise 2: border red

Page 33: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Which properties?

Page 34: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The following CSS 2.1 properties are inherited:

Page 35: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

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 36: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

This list might seem a bit overwhelming, so let’s focus on some of the key groups of properties.

Page 37: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

All text-related properties are inherited.

Page 38: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

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 39: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

All list-related properties are inherited.

Page 40: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

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 41: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

And, importantly, the color property is also inherited.

Page 42: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

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 43: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

font-size inheritance

Page 44: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The font-size property is inherited in a different way to many other properties. Rather than the actual value being inherited, the calculated value is inherited.

Page 45: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Before explaining how font-size inheritance works, we need to look at why font-size is not directly inherited.

Page 46: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

By default, the body element has a font-size of approximately 16px. What would happen if we set the <p> element to a font-size of 80%?

Page 47: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* setting the font-size */!p { font-size: 80%; }

Page 48: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The <p> element would be 80% x 16px which is a font-size of 12.8px.

Page 49: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

font-size table

element

<body>

<p>

percent

100%

80%

initial value

16px

16px

final value

16px

12.8px

Page 50: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

If the font-size value of 80% were to be inherited, the <em> would be sized to 80% of the <p> element and would be a font-size of 10.24px.

Page 51: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Lorem ipsum dolor etuer.

<em> element smaller than other content

Page 52: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

However, this is not the case! The <em> is the same size as the <p>. So how does inheritance work for font-size?

Page 53: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The answer is that the calculated value is inherited, rather than the value itself. Let’s look at three examples using the same HTML code as before:

Page 54: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Example 1: Pixels

Page 55: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

In the following example, the <p> element has been given a font-size of 14px.

Page 56: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* font-size in pixels */!p { font-size: 14px; }!

Page 57: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The pixel value (14px) overrides the browsers default font-size value (16px). This new calculated value (14px) is inherited by descendants.

Page 58: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

So, the <em> element will have a font-size of 14px.

Page 59: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

pixel example

element

<body>

<p>

<em>

value

unspecified

14px

unspecified

calculated value

16px

14px

inherited value = 14px

Page 60: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Example 2: percentage

Page 61: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

In the following example, the <p> element has been given a font-size of 80%.

Page 62: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* font-size in percents */!p { font-size: 80%; }!

Page 63: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The percentage value (80%) is applied to the browsers default font-size value (16px). This new calculated value (80% x 16px = 12.8px) is inherited by descendants.

Page 64: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

So, the <em> element will have a font-size of 12.8px.

Page 65: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

percentage example

element

<body>

<p>

<em>

value

unspecified

85%

unspecified

calculated value

16px

16px x 80% = 12.8px

inherited value = 12.8px

Page 66: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Example 3: em

Page 67: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

In the following example, the <p> element has been given a font-size of .7em.

Page 68: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* font-size in ems */!p { font-size: .7em; }!

Page 69: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

The em value (.7em) is applied the browsers default font-size value (16px). This new calculated value (.7 x 16px = 11.2px) is inherited by descendants.

Page 70: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

So, the <em> element will have a font-size of 11.2px.

Page 71: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

em example

element

<body>

<p>

<em>

value

unspecified

7em

unspecified

calculated value

16px

16px x 7em = 11.2px

inherited value = 11.2px

Page 72: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Using inheritance for efficiency

Page 73: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Authors can use inheritance to write efficient CSS. For example, you can set the font-size and font-family on the body element. These properties will be inherited by all descendant elements.

Page 74: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

In the following example, the <body> element has been given font-family and font-size values.

Page 75: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* inheritance example */!body!{ !! font-family: arial, helvetica, sans-serif;!! font-size: 90%;!}!

Page 76: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

You can then override the properties as needed. In the following example, the heading elements have been given a different font-family.

Page 77: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* inheritance example */!body!{ !! font-family: arial, helvetica, sans-serif;!! font-size: 90%;!}!!h1, h2, h3, h4, h5, h6!{ !! font-family: georgia, times, serif;!}!

Page 78: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

In the following example, the <h1> element has been given a unique font-size.

Page 79: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

/* inheritance example */!body!{ !! font-family: arial, helvetica, sans-serif;!! font-size: 90%;!}!!h1, h2, h3, h4, h5, h6!{ !! font-family: georgia, times, serif;!}!!h1 { font-size: 220%; }!!

Page 80: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Exercise 3: font-size and font-family

Page 81: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

Russ Weakley Max Design!Site: maxdesign.com.auTwitter: twitter.com/russmaxdesignSlideshare: slideshare.net/maxdesignLinkedin: linkedin.com/in/russweakley

Page 82: CSS - chitu.okoli.org€¦ · Inheritance is where CSS properties are passed down from parent elements to descendant elements

body highlight

labelLessonLesson2

quote url

code c-h c-g

Section hiTTitle S body smaller

Action