04.06.2013 Views

Head First HTML with CSS

Head First HTML with CSS

Head First HTML with CSS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

calculating specificity<br />

Welcome to the “What’s my specificity game”<br />

To calculate the specificity you start <strong>with</strong> a set of three numbers, like this:<br />

476 Chapter 11<br />

0 0 0<br />

And then we just tally up various things from the selector, like this:<br />

Does the selector<br />

have any ids? One<br />

point each.<br />

Does the selector<br />

have any classes<br />

or pseudo-classes?<br />

One point each.<br />

0 0 0<br />

For instance, the selector “h1” has one element in it, so you get:<br />

Read this as the<br />

number one.<br />

0 0 1<br />

As another example, the selector “h1.blue” has one element and<br />

one class, so you’d get:<br />

Read this as the<br />

number eleven.<br />

Sharpen your pencil<br />

h1.greentea<br />

p img<br />

a:link<br />

0 1 1<br />

Does the selector have<br />

any element names?<br />

One point for each.<br />

After you’ve tallied up all the ids, classes, and elements, the bigger the specificity<br />

number, the more specific the rule. So, since “h1.blue” has a specificity of 11, it<br />

is more specific than “h1”, which has a specificity number of 1.<br />

In the old days we<br />

used four numbers,<br />

but that was before<br />

X<strong>HTML</strong>... aren’t you<br />

glad you’re learning<br />

this now?<br />

Both “h1” and “h1.blue” have one<br />

element, so they both get a “1” in<br />

the right most number column.<br />

Try your hand at calculating the specificity of these selectors using the rules above:<br />

ol li p<br />

.green<br />

#elixirs h1<br />

“h1.blue” also has one class, so it gets<br />

a “1” in the middle number column.<br />

em<br />

span.cd<br />

#sidebar<br />

Neither have ids in their<br />

selectors, so they both get a<br />

“0” in the left number column

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!