SlideShare a Scribd company logo
CSS Specificity By Manu Goel
Contents CSS Specificity Exercise 1 Answer General Priority Specificity Hierarchy Calculating Specificity Excercise 1 (Revisited)
Contents The Trump Card Conclusion References & Worthy Readings Thank You
CSS Specificity Specificity  determines, which CSS rule is applied  by the browsers
Excercise 1 Anchor with class “spl” will be green or red? <style> ul#blogroll a { color: red; } a.spl  { color: green; } </style> <ul id=&quot;blogroll&quot;> <li><a href=&quot;b1.html&quot;>Blog 1</a></li> <li><a href=&quot;b2.html&quot;>Blog 2</a></li> <li><a href=&quot;b3.html&quot; class=&quot;spl&quot;>Blog 3</a></li> <li><a href=&quot;b4.html&quot;>Blog 4</a></li> </ul>
Answer RED
General Priority User Stylesheet Author Stylesheet Inline CSS declaration using STYLE attribute Internal CSS, inside STYLE tags External CSS file
Specificity Hierarchy pre, :before, :after, :first-line, etc .class, [attribute], :active, :focus, etc #whatever style=“color: blue;” color: blue !important; Example 1 Elements, pseudo-elements 10 Classes, attributes, pseudo-classes 100 IDs 1000 Inline styles n/a (Highest) !important Specificity Category
Calculating Specificity element selector (1) Sith power (specificity): 0, 0, 1 class selector (10) Sith power (specificity): 0, 1, 0 id selector (100) Sith power (specificity): 1, 0, 0
 
Excercise 1 (Revisited) <style> ul#blogroll a { color: red; }  Specificity : 1,0,2 a.spl   { color: green; }  Specificity : 0,1,1 </style> <ul id=&quot;blogroll&quot;> <li><a href=&quot;b1.html&quot;>Blog 1</a></li> <li><a href=&quot;b2.html&quot;>Blog 2</a></li> <li><a href=&quot;b3.html&quot; class=&quot;spl&quot;>Blog 3</a></li> <li><a href=&quot;b4.html&quot;>Blog 4</a></li> </ul>
The Trump Card !important  is not the solution to specificity woes This shortcut can lead to really messed up CSS in big projects
Conclusion Minimize use of !important [ Understanding Specificity ,  Selector Specificity ] Use id to make a rule more specific Minimize the number of selectors [ Understanding Specificity ]
Conclusion Use the least specific selector combination required to style an element Use LVHA for link styling link-visited-hover-active [ Link Specificity ]  If in confusion use [ CSS specificity calculator ]
References & Worthy Readings http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/ http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html http://www.molly.com/2005/10/06/css2-and-css21-specificity-clarified/ http://juicystudio.com/article/selector-specificity.php http://www.htmldog.com/guides/cssadvanced/specificity/ http://meyerweb.com/eric/css/link-specificity.html http://www.w3.org/TR/REC-CSS2/cascade.html#specificity http://monc.se/kitchen/38/cascading-order-and-inheritance-in-css http://iamacamera.org/default.aspx?id=95 http://www.rebelinblue.com/specificity.php http://www.brunildo.org/test/IEASpec.html
Thank You

More Related Content

PPTX
Specificity and CSS Selectors
PPT
Css lecture notes
PDF
Div tag presentation
PDF
HTML and CSS crash course!
PPTX
Css tables
PDF
HTML/CSS Crash Course (april 4 2017)
PPTX
Form using html and java script validation
Specificity and CSS Selectors
Css lecture notes
Div tag presentation
HTML and CSS crash course!
Css tables
HTML/CSS Crash Course (april 4 2017)
Form using html and java script validation

What's hot (20)

PDF
Intro to HTML and CSS basics
PPTX
Basic Html Knowledge for students
PPTX
PPTX
Html n CSS
PPTX
Html training slide
PDF
CSS Dasar #9 : Inheritance
ODP
CSS Basics
PDF
CSS3 Media Queries
PPTX
ASP.NET State management
PPT
Introduction to Cascading Style Sheets (CSS)
PPTX
Introduction to HTML
PPTX
Css Basics
PPT
CSS Introduction
PPTX
PPTX
PPTX
Html form tag
PPTX
Html5 and-css3-overview
PPT
Cascading Style Sheets(CSS)
PDF
Frontend Crash Course: HTML and CSS
PPT
Introduction to CSS
Intro to HTML and CSS basics
Basic Html Knowledge for students
Html n CSS
Html training slide
CSS Dasar #9 : Inheritance
CSS Basics
CSS3 Media Queries
ASP.NET State management
Introduction to Cascading Style Sheets (CSS)
Introduction to HTML
Css Basics
CSS Introduction
Html form tag
Html5 and-css3-overview
Cascading Style Sheets(CSS)
Frontend Crash Course: HTML and CSS
Introduction to CSS
Ad

Viewers also liked (13)

PPTX
Css specificity inheritance and the cascade things you should know
PPT
PDF
My 10 Mobile Automation Questions
PDF
Firefox OS Intro
PDF
Web Audio API in 15 min
PDF
Mobile Automators - Headlines Aug '15
PDF
Magneto - Android Test Automation
PPT
DoAT - mobile web-app development
PPS
CSS Exercise 1: 18 Examples
PDF
HTML practicals
PPT
cascading style sheet ppt
PPT
PDF
Html / CSS Presentation
Css specificity inheritance and the cascade things you should know
My 10 Mobile Automation Questions
Firefox OS Intro
Web Audio API in 15 min
Mobile Automators - Headlines Aug '15
Magneto - Android Test Automation
DoAT - mobile web-app development
CSS Exercise 1: 18 Examples
HTML practicals
cascading style sheet ppt
Html / CSS Presentation
Ad

Similar to Css Specificity (20)

PPT
Html ppt
PPT
Cascading Style Sheets
PPT
HTML & CSS
PPT
Html basics IML 140 (weeks 2-3)
PPT
Tables and Forms in HTML
PPTX
Html tags
PPT
Html tutorial
PPT
Understandable Content and Controls
PPTX
PPTX
HTML and CSS workshop
ODP
Htmltag.ppt
PPT
HTML Tutorial: Chapters 1 & 2
PPT
Kml Basics Chpt 2 Placemarks
PPT
Introduction to html
PPT
Introduction to html
PPT
Html Ppt
ODP
HTML5: 5 Quick Wins
PPT
XML Training Presentation
Html ppt
Cascading Style Sheets
HTML & CSS
Html basics IML 140 (weeks 2-3)
Tables and Forms in HTML
Html tags
Html tutorial
Understandable Content and Controls
HTML and CSS workshop
Htmltag.ppt
HTML Tutorial: Chapters 1 & 2
Kml Basics Chpt 2 Placemarks
Introduction to html
Introduction to html
Html Ppt
HTML5: 5 Quick Wins
XML Training Presentation

More from manugoel2003 (10)

PPT
Subversion (SVN)
PPT
Zend Framework
PPT
Drupal CMS
PPT
Securing Your Web Server
PPS
PHP Security
ODP
Introduction to jQuery
ODP
Drupal Best Practices
PPT
OOP in JavaScript
PPT
CiviCRM
PPT
PHP Documentor
Subversion (SVN)
Zend Framework
Drupal CMS
Securing Your Web Server
PHP Security
Introduction to jQuery
Drupal Best Practices
OOP in JavaScript
CiviCRM
PHP Documentor

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Big Data Technologies - Introduction.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Encapsulation theory and applications.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
Digital-Transformation-Roadmap-for-Companies.pptx
NewMind AI Weekly Chronicles - August'25 Week I
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
cuic standard and advanced reporting.pdf
A Presentation on Artificial Intelligence
Big Data Technologies - Introduction.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Per capita expenditure prediction using model stacking based on satellite ima...
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Encapsulation theory and applications.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
CIFDAQ's Market Insight: SEC Turns Pro Crypto

Css Specificity

  • 1. CSS Specificity By Manu Goel
  • 2. Contents CSS Specificity Exercise 1 Answer General Priority Specificity Hierarchy Calculating Specificity Excercise 1 (Revisited)
  • 3. Contents The Trump Card Conclusion References & Worthy Readings Thank You
  • 4. CSS Specificity Specificity determines, which CSS rule is applied by the browsers
  • 5. Excercise 1 Anchor with class “spl” will be green or red? <style> ul#blogroll a { color: red; } a.spl { color: green; } </style> <ul id=&quot;blogroll&quot;> <li><a href=&quot;b1.html&quot;>Blog 1</a></li> <li><a href=&quot;b2.html&quot;>Blog 2</a></li> <li><a href=&quot;b3.html&quot; class=&quot;spl&quot;>Blog 3</a></li> <li><a href=&quot;b4.html&quot;>Blog 4</a></li> </ul>
  • 7. General Priority User Stylesheet Author Stylesheet Inline CSS declaration using STYLE attribute Internal CSS, inside STYLE tags External CSS file
  • 8. Specificity Hierarchy pre, :before, :after, :first-line, etc .class, [attribute], :active, :focus, etc #whatever style=“color: blue;” color: blue !important; Example 1 Elements, pseudo-elements 10 Classes, attributes, pseudo-classes 100 IDs 1000 Inline styles n/a (Highest) !important Specificity Category
  • 9. Calculating Specificity element selector (1) Sith power (specificity): 0, 0, 1 class selector (10) Sith power (specificity): 0, 1, 0 id selector (100) Sith power (specificity): 1, 0, 0
  • 10.  
  • 11. Excercise 1 (Revisited) <style> ul#blogroll a { color: red; } Specificity : 1,0,2 a.spl { color: green; } Specificity : 0,1,1 </style> <ul id=&quot;blogroll&quot;> <li><a href=&quot;b1.html&quot;>Blog 1</a></li> <li><a href=&quot;b2.html&quot;>Blog 2</a></li> <li><a href=&quot;b3.html&quot; class=&quot;spl&quot;>Blog 3</a></li> <li><a href=&quot;b4.html&quot;>Blog 4</a></li> </ul>
  • 12. The Trump Card !important is not the solution to specificity woes This shortcut can lead to really messed up CSS in big projects
  • 13. Conclusion Minimize use of !important [ Understanding Specificity , Selector Specificity ] Use id to make a rule more specific Minimize the number of selectors [ Understanding Specificity ]
  • 14. Conclusion Use the least specific selector combination required to style an element Use LVHA for link styling link-visited-hover-active [ Link Specificity ] If in confusion use [ CSS specificity calculator ]
  • 15. References & Worthy Readings http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/ http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html http://www.molly.com/2005/10/06/css2-and-css21-specificity-clarified/ http://juicystudio.com/article/selector-specificity.php http://www.htmldog.com/guides/cssadvanced/specificity/ http://meyerweb.com/eric/css/link-specificity.html http://www.w3.org/TR/REC-CSS2/cascade.html#specificity http://monc.se/kitchen/38/cascading-order-and-inheritance-in-css http://iamacamera.org/default.aspx?id=95 http://www.rebelinblue.com/specificity.php http://www.brunildo.org/test/IEASpec.html