SlideShare a Scribd company logo
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.
Document trees are made from
     HTML elements.
The document tree is just like
     your family tree.
An ancestor refers to any
element that is connected but
further up the document tree.


                          Ancestor
A descendant refers to any
       element that is connected but
      lower down the document tree.



                              Descendant


Descendants
A parent is an element that is
connected & directly above an
element in the document tree.


                         Parent
A child is an element that is
connected & directly below an
element in the document tree.



                          Child
A sibling is an element that
       shares the same parent as
            another element.



                               Parent


Siblings
Next, a bit
  about
CSS rules
We also need to understand the
 basics of CSS rules before
    exploring inheritance.
CSS rules tell browsers
how to render specific elements
      on an HTML page.
CSS rules are made up of
  five components.
The selector "selects" the
elements on an HTML page that
    are affected by the rule.



  p { color: red; }


Selector
The declaration block is a
    container that consists of
anything between (and including)
          the brackets.

 p { color: red; }


    Declaration block
The declaration tells a browser
how to render any element on a
     page that is selected.



 p { color: red; }


       Declaration
The property is the aspect of that
element that you are choosing to
              style.



  p { color: red; }


      Property
The value is the exact style you
  wish to set for the property.




 p { color: red; }


             Value
Now…
   what is
inheritance?
Inheritance is where specific CSS
 properties are passed down to
      descendant elements.
To see inheritance in action, we
will use the HTML code below:



<p>
      Lorem <em>ipsum</em> dolor
      sit amet consect etuer.
</p>
Note that the <em> element sits
  inside the <p> element.
We will also use this CSS code.
Note that the <em> element has
      not been specified.



 p { color: red; }
In a browser, the <p> and <em>
 elements will both be colored
              red.




    <em> element
But why is the <em> element
colored red when it has not been
             styled?
Because the <em> element has
inherited the color property from
        the <p> element.
Why is
inheritance
  helpful?
Inheritance is designed to make
      it easier for authors.
Otherwise we would need to
  specify properties for all
  descendant elements.


p { color: red; }
p em { color: red; }
CSS files would be much larger
 in size, harder to create and
 maintain as well as slower to
           download.
Are all CSS
properties
inherited?
No. All CSS properties are
      not inherited!
If every CSS property was
inherited, it would make things
  much harder for authors.
Authors would have to turn off
 unwanted CSS properties for
    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?



 p { border: 1px solid red; }
The <em> inside the <p> would
   also have a red border.




    <em> element
Luckily, borders are not
inherited, so the <em> would not
       have a red border.




     <em> element
Generally speaking, only
properties that make our job
    easier are inherited!
So, which
properties are
  inherited?
The following CSS properties are
           inherited…
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
Yikes! That is a lot of properties.
To simply things, let’s take a look
         at some of the
   key groups of properties.
Text-related properties that are
           inherited:
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
List-related properties that are
           inherited:
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
And, importantly, the
color property is inherited:
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
Is font-size
 inherited?
The simple answer is “yes”.
However, font-size is inherited in
 a different way to many other
           properties.
Rather than the actual value
being inherited, the calculated
       value is inherited.
Before explaining how font-size
 inheritance works, we need to
          look at why
    font-size is not directly
            inherited.
Let’s start with the
same sample of HTML code we
         used earlier:


<p>
      Lorem <em>ipsum</em> dolor
      sit amet consect etuer.
</p>
As before the <em>
sits inside the <p>.
Now, a font-size is applied to the
<p> element only. The <em> has
       not been specified.



 p { font-size: 80%; }
If the font-size value of 80% were
         to be inherited, the
    <em> would be sized to 80%
        of the <p> element…
and the rendered document
   would look like this:




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




    <em> element
So how does inheritance work for
          font-size?
Let’s look at three examples
           in action.
We will use the same HTML
     code as before:



<p>
      Lorem <em>ipsum</em> dolor
      sit amet consect etuer.
</p>
Which produces the same
document tree as before.
Example 1:
  Pixels
The <p> element has been given
      a font-size of 14px.
Note: pixels are not recommended for sizing fonts due to accessibility
    issues associated with older browsers such as IE5 and IE6.




 p { font-size: 14px; }
This pixel value (14px) overrides
  the browsers default font-size
 value (approx 16px). This new
      value is inherited by
         descendants.
So, the <em> element inherits the
                14px value.

element          value          calcuated value

default font size approx 16px

<body>           unspecified    approx 16px

<p>              14px           14px

<em>             unspecified    inherited value = 14px
Example 2:
Percentage
The <p> element has been given
      a font-size of 85%.



 p { font-size: 85%; }
The browsers default font-size
 (16px) and the percentage value
    (85%) are used to create a
  calculated value (16px x 85% =
13.6px). This calculated value is
   inherited by descendants.
So, the <em> element inherits the
          13.6px calculated value.

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
Example 3:
   EMs
The <p> element has been given
     a font-size of .85em.
Note: Avoid using EMs for font-size values under 1em as IE5 renders these
        values in pixels instead of EMs (.8em is rendered as 8px).




   p { font-size: .85em; }
The browsers default font-size
(16px) and the EM value (.85em)
 are used to create a calculated
 value (16px x .85em = 13.6px).
    This calculated value is
  inherited by descendants.
So, the <em> element inherits the
          13.6px calculated value.

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
Those examples were too simple.
   What about more complex
   examples using different
         elements?
Example 4:
All elements have been specified
    using percentage values.



 body { font-size: 85%; }
 h1 { font-size: 200%; }
 h2 { font-size: 150%; }
The browsers default font-size
 (16px) and the body percentage
value (85%) are used to create a
 calculated value (16px x 85% =
 13.6px). This calculated value is
inherited by descendants unless
   new values are specified.
The font-size inheritance in
                     action
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
Using inheritance
  for efficiency
Authors can use inheritance to
     write efficient CSS.
For example, you can set the
color, font-size and font-family on
        the body element.


 body {
     color: #222;
     font-family: arial,
     helvetica, sans-serif;
     font-size: 90%;
 }
These properties will be inherited
  by all descendant elements.
You can then override the
properties as needed, specifying
       new color values...
body {
    color: #222;
    font-family: arial,
    helvetica, sans-serif;
    font-size: 90%;
}

h1, h2, h3 { color: green; }
h4, h5, h6 { color: black; }
new font-family values...
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;
}
and new font-size values as
         needed.
}

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%; }
Now, go forth and
inherit the world!
We’re done!

More Related Content

What's hot (20)

PPTX
HTML Semantic Tags
Bruce Kyle
 
PDF
CSS Notes in PDF, Easy to understand. For beginner to advanced. ...
syedfaisal759877
 
PDF
CSS3, Media Queries, and Responsive Design
Zoe Gillenwater
 
PPTX
HTML5 & CSS3
Ian Lintner
 
PPT
Javascript arrays
Hassan Dar
 
PPT
Html & Css presentation
joilrahat
 
PDF
Introduction into ES6 JavaScript.
boyney123
 
PPTX
Event In JavaScript
ShahDhruv21
 
PPTX
Css selectors
Parth Trivedi
 
PPTX
Html coding
Briana VanBuskirk
 
PPTX
Images and Tables in HTML
Aarti P
 
PPTX
Css Complete Notes
EPAM Systems
 
PDF
Introduction to HTML5
Gil Fink
 
PPTX
CSS Animations & Transitions
Edward Meehan
 
PPTX
Html5 tutorial for beginners
Singsys Pte Ltd
 
PDF
Intro to HTML & CSS
Syed Sami
 
PPTX
Cascading style sheet
Michael Jhon
 
PDF
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 
HTML Semantic Tags
Bruce Kyle
 
CSS Notes in PDF, Easy to understand. For beginner to advanced. ...
syedfaisal759877
 
CSS3, Media Queries, and Responsive Design
Zoe Gillenwater
 
HTML5 & CSS3
Ian Lintner
 
Javascript arrays
Hassan Dar
 
Html & Css presentation
joilrahat
 
Introduction into ES6 JavaScript.
boyney123
 
Event In JavaScript
ShahDhruv21
 
Css selectors
Parth Trivedi
 
Html coding
Briana VanBuskirk
 
Images and Tables in HTML
Aarti P
 
Css Complete Notes
EPAM Systems
 
Introduction to HTML5
Gil Fink
 
CSS Animations & Transitions
Edward Meehan
 
Html5 tutorial for beginners
Singsys Pte Ltd
 
Intro to HTML & CSS
Syed Sami
 
Cascading style sheet
Michael Jhon
 
Node.js Tutorial for Beginners | Node.js Web Application Tutorial | Node.js T...
Edureka!
 

Similar to CSS INHERITANCE (20)

PPTX
Getting started with css
Shivani Thakur Daxini
 
PDF
Introduction to CSS
Larry King
 
PDF
Btk creative-web-03
Lukas Oppermann
 
PPT
CSS ppt
Sanmuga Nathan
 
PDF
cascading stylesheet_cssppt-100604051600-phpapp02.pdf
hinalsomani93
 
PPT
Css layouts-continued
huia
 
PPT
CSS for Beginners
Amit Kumar Singh
 
PPTX
CSS.pptx
PushpaLatha551681
 
PPT
CSS
bjornh
 
PDF
Intro to CSS
Randy Oest II
 
PDF
Introduction to CSS3
Seble Nigussie
 
PDF
Line Height
Russ Weakley
 
PPTX
WEB DEVELOPMENT
Gourav Kaushik
 
DOC
Css1 properties
MP Bhoj University
 
PDF
CSS LINE HEIGHT
Anuradha
 
PDF
01 Introduction To CSS
crgwbr
 
PPTX
Lecture-7.pptx
vishal choudhary
 
PPT
Css advanced – session 5
Dr. Ramkumar Lakshminarayanan
 
PDF
Web Design Course: CSS lecture 3
Gheyath M. Othman
 
Getting started with css
Shivani Thakur Daxini
 
Introduction to CSS
Larry King
 
Btk creative-web-03
Lukas Oppermann
 
cascading stylesheet_cssppt-100604051600-phpapp02.pdf
hinalsomani93
 
Css layouts-continued
huia
 
CSS for Beginners
Amit Kumar Singh
 
CSS
bjornh
 
Intro to CSS
Randy Oest II
 
Introduction to CSS3
Seble Nigussie
 
Line Height
Russ Weakley
 
WEB DEVELOPMENT
Gourav Kaushik
 
Css1 properties
MP Bhoj University
 
CSS LINE HEIGHT
Anuradha
 
01 Introduction To CSS
crgwbr
 
Lecture-7.pptx
vishal choudhary
 
Css advanced – session 5
Dr. Ramkumar Lakshminarayanan
 
Web Design Course: CSS lecture 3
Gheyath M. Othman
 
Ad

More from Anuradha (20)

PPTX
Sql server 2012 - always on deep dive - bob duffy
Anuradha
 
PDF
J query visual-cheat-sheet-1.4.2
Anuradha
 
PDF
Inspiration
Anuradha
 
PPS
Peace Now
Anuradha
 
PDF
Go Kiss The World
Anuradha
 
PDF
CSS CASCADE
Anuradha
 
PPT
Insearchofmahatma 090802094040 Phpapp02
Anuradha
 
PPS
Mahatma
Anuradha
 
PDF
Presentation Alphabet
Anuradha
 
PPS
Dirtywindows
Anuradha
 
PPS
Dear Son Dear Daughter Slide Show
Anuradha
 
PPS
Beautiful Motivation
Anuradha
 
PPS
Great Leaders
Anuradha
 
PPS
A Present
Anuradha
 
PPS
Six Keys To Success
Anuradha
 
PPS
Friendship And Love
Anuradha
 
PPS
Dr Apj Kalam
Anuradha
 
PPS
Attitude Is Everything
Anuradha
 
XLS
Age Character Calculation
Anuradha
 
PDF
Apictureisworthathousandwords
Anuradha
 
Sql server 2012 - always on deep dive - bob duffy
Anuradha
 
J query visual-cheat-sheet-1.4.2
Anuradha
 
Inspiration
Anuradha
 
Peace Now
Anuradha
 
Go Kiss The World
Anuradha
 
CSS CASCADE
Anuradha
 
Insearchofmahatma 090802094040 Phpapp02
Anuradha
 
Mahatma
Anuradha
 
Presentation Alphabet
Anuradha
 
Dirtywindows
Anuradha
 
Dear Son Dear Daughter Slide Show
Anuradha
 
Beautiful Motivation
Anuradha
 
Great Leaders
Anuradha
 
A Present
Anuradha
 
Six Keys To Success
Anuradha
 
Friendship And Love
Anuradha
 
Dr Apj Kalam
Anuradha
 
Attitude Is Everything
Anuradha
 
Age Character Calculation
Anuradha
 
Apictureisworthathousandwords
Anuradha
 
Ad

Recently uploaded (20)

PDF
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
PDF
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
PDF
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
Practical Applications of AI in Local Government
OnBoard
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PPTX
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
PDF
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
“MPU+: A Transformative Solution for Next-Gen AI at the Edge,” a Presentation...
Edge AI and Vision Alliance
 
Quantum AI Discoveries: Fractal Patterns Consciousness and Cyclical Universes
Saikat Basu
 
ArcGIS Utility Network Migration - The Hunter Water Story
Safe Software
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Practical Applications of AI in Local Government
OnBoard
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Database Benchmarking for Performance Masterclass: Session 2 - Data Modeling ...
ScyllaDB
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
CapCut Pro Crack For PC Latest Version {Fully Unlocked} 2025
pcprocore
 
From Chatbot to Destroyer of Endpoints - Can ChatGPT Automate EDR Bypasses (1...
Priyanka Aash
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 

CSS INHERITANCE

  • 2. Let’s start with the document tree
  • 3. Before we explore inheritance, we need to understand the document tree.
  • 4. All HTML documents are trees.
  • 5. Document trees are made from HTML elements.
  • 6. The document tree is just like your family tree.
  • 7. An ancestor refers to any element that is connected but further up the document tree. Ancestor
  • 8. A descendant refers to any element that is connected but lower down the document tree. Descendant Descendants
  • 9. A parent is an element that is connected & directly above an element in the document tree. Parent
  • 10. A child is an element that is connected & directly below an element in the document tree. Child
  • 11. A sibling is an element that shares the same parent as another element. Parent Siblings
  • 12. Next, a bit about CSS rules
  • 13. We also need to understand the basics of CSS rules before exploring inheritance.
  • 14. CSS rules tell browsers how to render specific elements on an HTML page.
  • 15. CSS rules are made up of five components.
  • 16. The selector "selects" the elements on an HTML page that are affected by the rule. p { color: red; } Selector
  • 17. The declaration block is a container that consists of anything between (and including) the brackets. p { color: red; } Declaration block
  • 18. The declaration tells a browser how to render any element on a page that is selected. p { color: red; } Declaration
  • 19. The property is the aspect of that element that you are choosing to style. p { color: red; } Property
  • 20. The value is the exact style you wish to set for the property. p { color: red; } Value
  • 21. Now… what is inheritance?
  • 22. Inheritance is where specific CSS properties are passed down to descendant elements.
  • 23. To see inheritance in action, we will use the HTML code below: <p> Lorem <em>ipsum</em> dolor sit amet consect etuer. </p>
  • 24. Note that the <em> element sits inside the <p> element.
  • 25. We will also use this CSS code. Note that the <em> element has not been specified. p { color: red; }
  • 26. In a browser, the <p> and <em> elements will both be colored red. <em> element
  • 27. But why is the <em> element colored red when it has not been styled?
  • 28. Because the <em> element has inherited the color property from the <p> element.
  • 29. Why is inheritance helpful?
  • 30. Inheritance is designed to make it easier for authors.
  • 31. Otherwise we would need to specify properties for all descendant elements. p { color: red; } p em { color: red; }
  • 32. CSS files would be much larger in size, harder to create and maintain as well as slower to download.
  • 34. No. All CSS properties are not inherited!
  • 35. If every CSS property was inherited, it would make things much harder for authors.
  • 36. Authors would have to turn off unwanted CSS properties for descendant elements.
  • 37. For example, what would happen if the border property was inherited by default…
  • 38. and we then applied a border to the <p> element? p { border: 1px solid red; }
  • 39. The <em> inside the <p> would also have a red border. <em> element
  • 40. Luckily, borders are not inherited, so the <em> would not have a red border. <em> element
  • 41. Generally speaking, only properties that make our job easier are inherited!
  • 43. The following CSS properties are inherited…
  • 44. 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
  • 45. Yikes! That is a lot of properties.
  • 46. To simply things, let’s take a look at some of the key groups of properties.
  • 48. 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
  • 50. 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
  • 51. And, importantly, the color property is inherited:
  • 52. 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
  • 54. The simple answer is “yes”. However, font-size is inherited in a different way to many other properties.
  • 55. Rather than the actual value being inherited, the calculated value is inherited.
  • 56. Before explaining how font-size inheritance works, we need to look at why font-size is not directly inherited.
  • 57. Let’s start with the same sample of HTML code we used earlier: <p> Lorem <em>ipsum</em> dolor sit amet consect etuer. </p>
  • 58. As before the <em> sits inside the <p>.
  • 59. Now, a font-size is applied to the <p> element only. The <em> has not been specified. p { font-size: 80%; }
  • 60. If the font-size value of 80% were to be inherited, the <em> would be sized to 80% of the <p> element…
  • 61. and the rendered document would look like this: <em> element
  • 62. However, this is not the case! The <em> is the same size as the <p>. <em> element
  • 63. So how does inheritance work for font-size?
  • 64. Let’s look at three examples in action.
  • 65. We will use the same HTML code as before: <p> Lorem <em>ipsum</em> dolor sit amet consect etuer. </p>
  • 66. Which produces the same document tree as before.
  • 67. Example 1: Pixels
  • 68. The <p> element has been given a font-size of 14px. Note: pixels are not recommended for sizing fonts due to accessibility issues associated with older browsers such as IE5 and IE6. p { font-size: 14px; }
  • 69. This pixel value (14px) overrides the browsers default font-size value (approx 16px). This new value is inherited by descendants.
  • 70. So, the <em> element inherits the 14px value. element value calcuated value default font size approx 16px <body> unspecified approx 16px <p> 14px 14px <em> unspecified inherited value = 14px
  • 72. The <p> element has been given a font-size of 85%. p { font-size: 85%; }
  • 73. The browsers default font-size (16px) and the percentage value (85%) are used to create a calculated value (16px x 85% = 13.6px). This calculated value is inherited by descendants.
  • 74. So, the <em> element inherits the 13.6px calculated value. 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
  • 75. Example 3: EMs
  • 76. The <p> element has been given a font-size of .85em. Note: Avoid using EMs for font-size values under 1em as IE5 renders these values in pixels instead of EMs (.8em is rendered as 8px). p { font-size: .85em; }
  • 77. The browsers default font-size (16px) and the EM value (.85em) are used to create a calculated value (16px x .85em = 13.6px). This calculated value is inherited by descendants.
  • 78. So, the <em> element inherits the 13.6px calculated value. 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
  • 79. Those examples were too simple. What about more complex examples using different elements?
  • 81. All elements have been specified using percentage values. body { font-size: 85%; } h1 { font-size: 200%; } h2 { font-size: 150%; }
  • 82. The browsers default font-size (16px) and the body percentage value (85%) are used to create a calculated value (16px x 85% = 13.6px). This calculated value is inherited by descendants unless new values are specified.
  • 83. The font-size inheritance in action 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
  • 84. Using inheritance for efficiency
  • 85. Authors can use inheritance to write efficient CSS.
  • 86. For example, you can set the color, font-size and font-family on the body element. body { color: #222; font-family: arial, helvetica, sans-serif; font-size: 90%; }
  • 87. These properties will be inherited by all descendant elements.
  • 88. You can then override the properties as needed, specifying new color values...
  • 89. body { color: #222; font-family: arial, helvetica, sans-serif; font-size: 90%; } h1, h2, h3 { color: green; } h4, h5, h6 { color: black; }
  • 91. 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; }
  • 92. and new font-size values as needed.
  • 93. } 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%; }
  • 94. Now, go forth and inherit the world!