SlideShare a Scribd company logo
1 of 30
WEBD 162
Specificity and CSS Selectors
CSS Rule Recap
• CSS Rule consists of:
• Selector { Declaration Block }
• Declaration Block consists of zero or more Declarations
• Declaration consists of Property: Value pair ending in semicolon ;
• Example:
body {
color: rgb(91,91,91);
background-color: ivory;
font-family: Verdana, Geneva, sans-serif;
}
Specificity
• CSS rules for an element may appear multiple times
within a web page:
• inline, embedded, external
• If there are multiple instances of a CSS rule, which one
applies?
• The rule with the highest specificity weight
• If all specificity weights the same, the rule that appears last
• http://www.smashingmagazine.com/2007/07/27/css-
specificity-things-you-should-know
Selector and Specificity
• Read selectors from right to left
• selector
• Which element is going to be affected by the rule?
• The rightmost part of selector is the type of element
• Everything to the left specifies which set of elements that are affected
Types of selectors
• A contextual selector specifies the context of a matching
element
• The type of element
• The position an element is in relation to other elements
• An attribute selector uses an element's attributes and
attribute values to target a rule to a specific element
• Common attributes used for targeting are:
• id – can only appear only once in the HTML document
• class – may appear multiple times in the HTML document
Contextual selectors
Selector Description
* Matches any element
elem Matches the element elem located anywhere in the
document
elem1, elem2 Matches any of the elements elem1, elem2, etc.
parent descendent Matches the descendent element that is nested within
the parent element at some level
parent > child Matches the child element that is a child of the parent
element
elem1 + elem2 Matches elem2 that is immediately preceded by the
sibling element elem1
elem1 - elem2 Matches elem2 that follows sibling element elem1
Attribute selectors
Selector Selects
elem#id Element elem with the ID value id
#id Any element with the ID value id
elem.class All elem elements with the class attribute value class
.class All elements with the class value class
elem[att] All elem elements containing the att attribute
elem[att="text"] All elem elements whose att attribute equals text
elem[att~="text"] All elem elements whose att attribute contains the word text
elem[att|="text"] All elem elements whose att attribute is a hyphen-separated
list of words beginning with text
elem[att^="text"] All elem elements whose att attribute begins with text
[CSS3]
elem[att$="text"] All elem elements whose att attribute ends with text [CSS3]
elem[att*="text"] All elem elements whose att attribute contains the value text
[CSS3]
How to calculate specificity of a rule
Weight Multiply by
1 # of element names or pseudo elements
10 # of classes or pseudo class
100 id selector
1000 If an in-line rule
Special cases:
• Universal attribute * = 0
• !important overrides all
• If two rules have the same specificity weight, the last one applies
• Visual Specificity Calculator: http://specificity.keegan.st/
Example:Hierarchy of Page Elements
body
article
header
h1 p p
p p p
aside
h1 p
“Code” for Context Selector example
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Contextual Selector: *
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: *
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects all elements (wildcard)
Contextual Selector: p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element
Contextual Selector: article p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: article p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element that is a
descendant of an article
element
Contextual Selector: article > p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: article > p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element that is a
child of an article element
Contextual Selector: h1, p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: h1, p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any h1 or p element
Contextual Selector: h1 + p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: h1 + p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects a p element that
immediately follow an h1
element
Contextual Selector: article h1 + p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: article h1 + p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects a p element that
immediately follows any h1
element that is a descendant of
an article element
Contextual Selector: h1 - p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: h1 - p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any p element that is a
sibling that follows after any h1
element
Contextual Selector: article h1 - p
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: article h1 - p
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red
Affects any element that is a
child of an article element
Contextual Selector: article > *
body
article
header
h1 p p
p p p
aside
h1 p
Affected elements indicated by red shadow
Contextual Selector: article > *
<body>
<article>
<header>
<h1></h1>
<p></p>
<p></p>
</header>
<p></p>
<p></p>
<p></p>
</article>
<aside>
<h1></h1>
<p></p>
</aside>
</body>
Affected elements in red

More Related Content

What's hot (20)

HTML Fundamentals
HTML FundamentalsHTML Fundamentals
HTML Fundamentals
 
CSS
CSSCSS
CSS
 
Introduction of Html/css/js
Introduction of Html/css/jsIntroduction of Html/css/js
Introduction of Html/css/js
 
1 03 - CSS Introduction
1 03 - CSS Introduction1 03 - CSS Introduction
1 03 - CSS Introduction
 
Html introduction
Html introductionHtml introduction
Html introduction
 
Html basics
Html basicsHtml basics
Html basics
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
Web Development using HTML & CSS
Web Development using HTML & CSSWeb Development using HTML & CSS
Web Development using HTML & CSS
 
presentation in html,css,javascript
presentation in html,css,javascriptpresentation in html,css,javascript
presentation in html,css,javascript
 
Css Ppt
Css PptCss Ppt
Css Ppt
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 
Javascript
JavascriptJavascript
Javascript
 
Basic html
Basic htmlBasic html
Basic html
 
HTML
HTMLHTML
HTML
 
Html / CSS Presentation
Html / CSS PresentationHtml / CSS Presentation
Html / CSS Presentation
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Intro Html
Intro HtmlIntro Html
Intro Html
 
How to learn HTML in 10 Days
How to learn HTML in 10 DaysHow to learn HTML in 10 Days
How to learn HTML in 10 Days
 

Similar to CSS Selectors and Specificity

Similar to CSS Selectors and Specificity (20)

Angular JS
Angular JSAngular JS
Angular JS
 
Basic css
Basic cssBasic css
Basic css
 
Web 101
Web 101Web 101
Web 101
 
Basic Details of HTML and CSS.pdf
Basic Details of HTML and CSS.pdfBasic Details of HTML and CSS.pdf
Basic Details of HTML and CSS.pdf
 
Web 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style SheetsWeb 2 | CSS - Cascading Style Sheets
Web 2 | CSS - Cascading Style Sheets
 
TAppWeb Training Material
TAppWeb Training MaterialTAppWeb Training Material
TAppWeb Training Material
 
Css from scratch
Css from scratchCss from scratch
Css from scratch
 
Css
CssCss
Css
 
Web Programming& Scripting Lab
Web Programming& Scripting LabWeb Programming& Scripting Lab
Web Programming& Scripting Lab
 
Cascading Style Sheets By Mukesh
Cascading Style Sheets By MukeshCascading Style Sheets By Mukesh
Cascading Style Sheets By Mukesh
 
Basic-CSS-tutorial
Basic-CSS-tutorialBasic-CSS-tutorial
Basic-CSS-tutorial
 
Basic css-tutorial
Basic css-tutorialBasic css-tutorial
Basic css-tutorial
 
Basic-CSS-tutorial
Basic-CSS-tutorialBasic-CSS-tutorial
Basic-CSS-tutorial
 
Web 102 INtro to CSS
Web 102  INtro to CSSWeb 102  INtro to CSS
Web 102 INtro to CSS
 
TApp Web training material_rev4
TApp Web training material_rev4TApp Web training material_rev4
TApp Web training material_rev4
 
Ifi7174 lesson2
Ifi7174 lesson2Ifi7174 lesson2
Ifi7174 lesson2
 
Web Programming-css.pptx
Web Programming-css.pptxWeb Programming-css.pptx
Web Programming-css.pptx
 
Css1
Css1Css1
Css1
 
How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) Works
 
Jquery for Beginners
Jquery for BeginnersJquery for Beginners
Jquery for Beginners
 

Recently uploaded

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 

Recently uploaded (20)

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 

CSS Selectors and Specificity

  • 1. WEBD 162 Specificity and CSS Selectors
  • 2. CSS Rule Recap • CSS Rule consists of: • Selector { Declaration Block } • Declaration Block consists of zero or more Declarations • Declaration consists of Property: Value pair ending in semicolon ; • Example: body { color: rgb(91,91,91); background-color: ivory; font-family: Verdana, Geneva, sans-serif; }
  • 3. Specificity • CSS rules for an element may appear multiple times within a web page: • inline, embedded, external • If there are multiple instances of a CSS rule, which one applies? • The rule with the highest specificity weight • If all specificity weights the same, the rule that appears last • http://www.smashingmagazine.com/2007/07/27/css- specificity-things-you-should-know
  • 4. Selector and Specificity • Read selectors from right to left • selector • Which element is going to be affected by the rule? • The rightmost part of selector is the type of element • Everything to the left specifies which set of elements that are affected
  • 5. Types of selectors • A contextual selector specifies the context of a matching element • The type of element • The position an element is in relation to other elements • An attribute selector uses an element's attributes and attribute values to target a rule to a specific element • Common attributes used for targeting are: • id – can only appear only once in the HTML document • class – may appear multiple times in the HTML document
  • 6. Contextual selectors Selector Description * Matches any element elem Matches the element elem located anywhere in the document elem1, elem2 Matches any of the elements elem1, elem2, etc. parent descendent Matches the descendent element that is nested within the parent element at some level parent > child Matches the child element that is a child of the parent element elem1 + elem2 Matches elem2 that is immediately preceded by the sibling element elem1 elem1 - elem2 Matches elem2 that follows sibling element elem1
  • 7. Attribute selectors Selector Selects elem#id Element elem with the ID value id #id Any element with the ID value id elem.class All elem elements with the class attribute value class .class All elements with the class value class elem[att] All elem elements containing the att attribute elem[att="text"] All elem elements whose att attribute equals text elem[att~="text"] All elem elements whose att attribute contains the word text elem[att|="text"] All elem elements whose att attribute is a hyphen-separated list of words beginning with text elem[att^="text"] All elem elements whose att attribute begins with text [CSS3] elem[att$="text"] All elem elements whose att attribute ends with text [CSS3] elem[att*="text"] All elem elements whose att attribute contains the value text [CSS3]
  • 8. How to calculate specificity of a rule Weight Multiply by 1 # of element names or pseudo elements 10 # of classes or pseudo class 100 id selector 1000 If an in-line rule Special cases: • Universal attribute * = 0 • !important overrides all • If two rules have the same specificity weight, the last one applies • Visual Specificity Calculator: http://specificity.keegan.st/
  • 9. Example:Hierarchy of Page Elements body article header h1 p p p p p aside h1 p
  • 10. “Code” for Context Selector example <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body>
  • 11. Contextual Selector: * body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 13. Contextual Selector: p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 15. Contextual Selector: article p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 16. Contextual Selector: article p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects any p element that is a descendant of an article element
  • 17. Contextual Selector: article > p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 18. Contextual Selector: article > p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects any p element that is a child of an article element
  • 19. Contextual Selector: h1, p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 20. Contextual Selector: h1, p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects any h1 or p element
  • 21. Contextual Selector: h1 + p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 22. Contextual Selector: h1 + p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects a p element that immediately follow an h1 element
  • 23. Contextual Selector: article h1 + p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 24. Contextual Selector: article h1 + p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects a p element that immediately follows any h1 element that is a descendant of an article element
  • 25. Contextual Selector: h1 - p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 26. Contextual Selector: h1 - p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects any p element that is a sibling that follows after any h1 element
  • 27. Contextual Selector: article h1 - p body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 28. Contextual Selector: article h1 - p <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red Affects any element that is a child of an article element
  • 29. Contextual Selector: article > * body article header h1 p p p p p aside h1 p Affected elements indicated by red shadow
  • 30. Contextual Selector: article > * <body> <article> <header> <h1></h1> <p></p> <p></p> </header> <p></p> <p></p> <p></p> </article> <aside> <h1></h1> <p></p> </aside> </body> Affected elements in red

Editor's Notes

  1. >: Stylin’ p.38 *: Stylin’ p.45 +: Stylin’ p.45-46 Figure 2.15, the <p> immediately after the <h1> has the rule applied []: Stylin’ p.46
  2. >: Stylin’ p.38 *: Stylin’ p.45 +: Stylin’ p.45-46 Figure 2.15, the <p> immediately after the <h1> has the rule applied []: Stylin’ p.46
  3. Specificity.html DW Code Navigator: ALT-CMD-CLICK (Mac) DW Webkit engine (Open Source, same base as Safari)