SlideShare ist ein Scribd-Unternehmen logo
1 von 95
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!

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Intro to HTML and CSS basics
Intro to HTML and CSS basicsIntro to HTML and CSS basics
Intro to HTML and CSS basics
 
Web 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style SheetsWeb 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style Sheets
 
CSS selectors
CSS selectorsCSS selectors
CSS selectors
 
Cascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) helpCascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) help
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 
Introduction to CSS Borders - Lesson 4
Introduction to CSS Borders - Lesson 4Introduction to CSS Borders - Lesson 4
Introduction to CSS Borders - Lesson 4
 
Html table tags
Html table tagsHtml table tags
Html table tags
 
CSS
CSSCSS
CSS
 
CSS
CSS CSS
CSS
 
Bootstrap 3
Bootstrap 3Bootstrap 3
Bootstrap 3
 
Css
CssCss
Css
 
Css
CssCss
Css
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Introduction to Javascript
Introduction to JavascriptIntroduction to Javascript
Introduction to Javascript
 
Beginners css tutorial for web designers
Beginners css tutorial for web designersBeginners css tutorial for web designers
Beginners css tutorial for web designers
 
Flex box
Flex boxFlex box
Flex box
 
CSS for Beginners
CSS for BeginnersCSS for Beginners
CSS for Beginners
 
CSS ppt
CSS pptCSS ppt
CSS ppt
 
PHP HTML CSS Notes
PHP HTML CSS  NotesPHP HTML CSS  Notes
PHP HTML CSS Notes
 

Ähnlich wie CSS INHERITANCE

Ähnlich wie CSS INHERITANCE (20)

Getting started with css
Getting started with cssGetting started with css
Getting started with css
 
Css
CssCss
Css
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 
Btk creative-web-03
Btk creative-web-03Btk creative-web-03
Btk creative-web-03
 
Css Complete Notes
Css Complete NotesCss Complete Notes
Css Complete Notes
 
Css layouts-continued
Css layouts-continuedCss layouts-continued
Css layouts-continued
 
CSS.pptx
CSS.pptxCSS.pptx
CSS.pptx
 
CSS Notes in PDF, Easy to understand. For beginner to advanced. ...
CSS Notes in PDF, Easy to understand. For beginner to advanced.              ...CSS Notes in PDF, Easy to understand. For beginner to advanced.              ...
CSS Notes in PDF, Easy to understand. For beginner to advanced. ...
 
CSS
CSSCSS
CSS
 
Intro to CSS
Intro to CSSIntro to CSS
Intro to CSS
 
Line Height
Line HeightLine Height
Line Height
 
WEB DEVELOPMENT
WEB DEVELOPMENTWEB DEVELOPMENT
WEB DEVELOPMENT
 
Css1 properties
Css1 propertiesCss1 properties
Css1 properties
 
CSS LINE HEIGHT
CSS LINE HEIGHTCSS LINE HEIGHT
CSS LINE HEIGHT
 
01 Introduction To CSS
01 Introduction To CSS01 Introduction To CSS
01 Introduction To CSS
 
Lecture-7.pptx
Lecture-7.pptxLecture-7.pptx
Lecture-7.pptx
 
Css advanced – session 5
Css advanced – session 5Css advanced – session 5
Css advanced – session 5
 
Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3Web Design Course: CSS lecture 3
Web Design Course: CSS lecture 3
 
Basics Of Css And Some Common Mistakes
Basics Of Css And Some Common MistakesBasics Of Css And Some Common Mistakes
Basics Of Css And Some Common Mistakes
 
5. Web Technology CSS Advanced
5. Web Technology CSS Advanced 5. Web Technology CSS Advanced
5. Web Technology CSS Advanced
 

Mehr von Anuradha

Sql server 2012 - always on deep dive - bob duffy
Sql server 2012 - always on deep dive - bob duffySql server 2012 - always on deep dive - bob duffy
Sql server 2012 - always on deep dive - bob duffyAnuradha
 
J query visual-cheat-sheet-1.4.2
J query visual-cheat-sheet-1.4.2J query visual-cheat-sheet-1.4.2
J query visual-cheat-sheet-1.4.2Anuradha
 
Go Kiss The World
Go Kiss The WorldGo Kiss The World
Go Kiss The WorldAnuradha
 
Insearchofmahatma 090802094040 Phpapp02
Insearchofmahatma 090802094040 Phpapp02Insearchofmahatma 090802094040 Phpapp02
Insearchofmahatma 090802094040 Phpapp02Anuradha
 
Presentation Alphabet
Presentation AlphabetPresentation Alphabet
Presentation AlphabetAnuradha
 
Dirtywindows
DirtywindowsDirtywindows
DirtywindowsAnuradha
 
Dear Son Dear Daughter Slide Show
Dear Son Dear Daughter Slide ShowDear Son Dear Daughter Slide Show
Dear Son Dear Daughter Slide ShowAnuradha
 
Beautiful Motivation
Beautiful MotivationBeautiful Motivation
Beautiful MotivationAnuradha
 
Great Leaders
Great LeadersGreat Leaders
Great LeadersAnuradha
 
Six Keys To Success
Six Keys To SuccessSix Keys To Success
Six Keys To SuccessAnuradha
 
Friendship And Love
Friendship And LoveFriendship And Love
Friendship And LoveAnuradha
 
Dr Apj Kalam
Dr Apj KalamDr Apj Kalam
Dr Apj KalamAnuradha
 
Attitude Is Everything
Attitude Is EverythingAttitude Is Everything
Attitude Is EverythingAnuradha
 
Age Character Calculation
Age Character CalculationAge Character Calculation
Age Character CalculationAnuradha
 
Apictureisworthathousandwords
ApictureisworthathousandwordsApictureisworthathousandwords
ApictureisworthathousandwordsAnuradha
 

Mehr von Anuradha (20)

Sql server 2012 - always on deep dive - bob duffy
Sql server 2012 - always on deep dive - bob duffySql server 2012 - always on deep dive - bob duffy
Sql server 2012 - always on deep dive - bob duffy
 
J query visual-cheat-sheet-1.4.2
J query visual-cheat-sheet-1.4.2J query visual-cheat-sheet-1.4.2
J query visual-cheat-sheet-1.4.2
 
Inspiration
InspirationInspiration
Inspiration
 
Peace Now
Peace NowPeace Now
Peace Now
 
Go Kiss The World
Go Kiss The WorldGo Kiss The World
Go Kiss The World
 
CSS CASCADE
CSS CASCADECSS CASCADE
CSS CASCADE
 
Insearchofmahatma 090802094040 Phpapp02
Insearchofmahatma 090802094040 Phpapp02Insearchofmahatma 090802094040 Phpapp02
Insearchofmahatma 090802094040 Phpapp02
 
Mahatma
MahatmaMahatma
Mahatma
 
Presentation Alphabet
Presentation AlphabetPresentation Alphabet
Presentation Alphabet
 
Dirtywindows
DirtywindowsDirtywindows
Dirtywindows
 
Dear Son Dear Daughter Slide Show
Dear Son Dear Daughter Slide ShowDear Son Dear Daughter Slide Show
Dear Son Dear Daughter Slide Show
 
Beautiful Motivation
Beautiful MotivationBeautiful Motivation
Beautiful Motivation
 
Great Leaders
Great LeadersGreat Leaders
Great Leaders
 
A Present
A PresentA Present
A Present
 
Six Keys To Success
Six Keys To SuccessSix Keys To Success
Six Keys To Success
 
Friendship And Love
Friendship And LoveFriendship And Love
Friendship And Love
 
Dr Apj Kalam
Dr Apj KalamDr Apj Kalam
Dr Apj Kalam
 
Attitude Is Everything
Attitude Is EverythingAttitude Is Everything
Attitude Is Everything
 
Age Character Calculation
Age Character CalculationAge Character Calculation
Age Character Calculation
 
Apictureisworthathousandwords
ApictureisworthathousandwordsApictureisworthathousandwords
Apictureisworthathousandwords
 

Kürzlich hochgeladen

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Kürzlich hochgeladen (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

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!