Quick Tip: Understanding CSS Specificity
The “C” in CSS stands for cascading. This means that that style rules that show up later in the stylesheet will override rules which appear earlier. But this isn’t always the case. There’s something else you have to take into consideration, as well: specificity. In this quick tip, I’ll show you how to do just that.
Specificity Rules
CSS Specificity is basically this: the more specific your selector is, the more precedence it will have. To figure out which selectors are worth more, use this system:
- Give each HTML element selector 1 point. Example: p
- Give each class selector 10 points. Example: .column
- Give each id selector 100 point. Example: #wrap
- Add up the points for each piece of the selector to get the full value of the selector.
For Example
#test { background: red; } /* specificity : 100 */
.item p { background: green; } /* specificity : 10 + 1 = 11 */
p { background: orange; } /* specificity : 1 */
body #wrap p { background: yellow; } /* specificity : 1 + 100 + 1 = 102 */
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
- http://www.ifadey.com Fawad Hassan
- http://conecode.com Chris
- http://www.daniel-petrie.com Daniel Petrie
- http://www.purplecoffee.co.uk Gareth Evans
- w1sh
- dru
- http://www.dawn-designs.fr p0ulpe
- http://twitter.com/mikeyisfresh Mike
- http://kemitchell.com K
- http://www.dawn-designs.fr p0ulpe
- http://www.nickbrowndesign.com Nick Brown
- Umar Mirza
- mookyong
- http://tegarmaji.com Tegar
- jmarreros
- http://xpressabhi.com abhishek
- leo rapirap
- Richard Cochrane
- http://www.khwebdesign.net Kent
- http://www.pinoyscreencast.com techie.biox
- Sosby
- Amit
- http://www.kieru.com Rob
- Kabita
- Mohamed Zahran
- Gee
- http://www.xcellence-it.com/ Xcellence IT
- Peter
- Cristina Sturm
- http://www.robnixondesigns.com Rob
- http://www.ravi.uxdsign.com Ravikumar V.
- http://www.jaytillery.com Jay
- http://www.idesignb.com Bryan
- Renowned Media
- wayno
- http://www.chrisleah.co.uk Chris
- http://www.russelluresti.com RussellUresti
- http://www.russelluresti.com RussellUresti
- James
- http://www.rhysthomas.co.uk Rhys
- Dru
- Jota Carlos
- http://codendesign.blogspot.com nXqd
- Nuruzzaman Sheikh
- http://whatiscss.michaelfokken.com/ Michael Fokken
- http://codepsd.si CodePsd.com
- http://webtutorialplus.blogspot.com/ Henry
- Cleveland
- http://www.testshoot.com/ TestShoot
