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.

The cascade<br />

For this exercise, you need to “be the browser”. Let’s say you’ve got an <br />

element on a page and you want to know the font-size property for it.<br />

Here’s how you do it:<br />

Step one:<br />

Gather all your style sheets together.<br />

For this step you need ALL the styles sheets: the style sheets the<br />

Web page author has written, any style sheets that the reader has<br />

added to the mix, and the browser’s default styles. (Remember,<br />

you’re the browser now, so you have access to all this stuff!)<br />

Step two:<br />

Find all the declarations that match.<br />

We’re looking specifically for the font-size property, so look at all the<br />

declarations for font-size that have a selector which could possibly select<br />

the element. Go through all the style sheets and pull out any rules that<br />

match and also have a font-size property.<br />

Step three:<br />

Now take all your matches, and sort them.<br />

Now that you’ve got all the matching rules together, sort them in the order of<br />

author, reader, browser. In other words, if you wrote them as the author of the<br />

page, then they are more important than if the reader wrote them. And, in<br />

turn, the reader’s styles are more important than the browser’s default styles.<br />

Step four:<br />

Now sort all the declarations by how specific they are.<br />

Remember, we talked about this a little, way back in Chapter 8. You can<br />

intuitively think about a rule being more specific if it more accurately selects<br />

an element; for instance, the descendant selector “blockquote h1” is more<br />

specific than just the “h1” selector because it only selects s inside of<br />

s. But there is a little recipe you can follow to calculate exactly<br />

how specific a selector is, and we’ll do that on the next page.<br />

Step five:<br />

Finally, sort any conflicting rules in the order they appear in their<br />

individual style sheets.<br />

Now you just need to take the list, and order any conflicting rules so that the ones<br />

appearing later (closer to the bottom) of their respective style sheets are more important.<br />

That way, if you put a new rule in your style sheet, it can override any rules before it.<br />

That’s it! The first rule in the sorted list is the winner, and its font-size property is<br />

the one to use. Now let’s see how you determine how specific a selector is.<br />

divs and spans<br />

Remember we<br />

mentioned that the<br />

reader could put<br />

!important on their<br />

<strong>CSS</strong> properties, and if<br />

they do that, those<br />

properties come first<br />

when you sort.<br />

you are here 475

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

Saved successfully!

Ooh no, something went wrong!