80 likes | 282 Vues
CS 081 – HTML and Web Programming – Lecture 4. Specificity. Conflicting Styles 1. div { color: red; } div { color: blue; } Is the div red or blue?. Specificity Rule 1. If any rules that have the same specificity. The last rule will apply. Conflicting Styles 2.
E N D
CS 081 – HTML and Web Programming – Lecture 4 Specificity
Conflicting Styles 1 div { color: red; } div{ color: blue; } Is the div red or blue?
Specificity Rule 1 • If any rules that have the same specificity. The last rule will apply.
Conflicting Styles 2 div p{ color: red; } p { color: blue; } Assume a p with a parent div. Is the p red or blue?
Specificity Rule 2 The p is red because its selector is more specific.
Calculating Specificity • Tag selectors have value 1 • Class selectors have value 10 • Id selectors have value 100 • Inline Style selectors have value 1000 • Depreciated (Avoid) • To calculate specificity you add up the values: • Combo, Descendant, Child, and Sibling Selectors have no value.
Examples p > p p{color:blue;} Specificity 3 p pp {color:red;} Specificity 3 p+p{color:green;}Specificity 2 #red.green Specificity 110 .green #redSpecificity 110 #red p > p .green p.red Specificity 123 body#pool Specificity 101 #body#pool Specificity 200
Specificity • Typically you would use the least number of selectors needed to select a particular element. • If later on that rule gets accidentally overridden, then you need to increase specificity.