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.

Using nesting to make sure your tags match<br />

Your first payoff for understanding how elements are nested is that<br />

you can avoid mismatching your tags. (And there’s gonna be more<br />

payoff later, just wait.)<br />

What does “mismatching your tags” mean and how could that<br />

happen? Take a look at this example:<br />

I’m so going to blog this<br />

p<br />

Here’s how this <strong>HTML</strong> looks,<br />

is nested inside .<br />

So far, so good, but it’s also easy to get sloppy and write some <strong>HTML</strong><br />

that looks more like this:<br />

I’m so going to blog this<br />

Given what you now know about nesting, you know the element<br />

needs to be nested fully <strong>with</strong>in, or contained in, the element.<br />

So what?<br />

em<br />

GOOD: here the element is<br />

nested inside the .<br />

It’s okay to mess up your nesting if you like playing Russian roulette. If you write <strong>HTML</strong><br />

<strong>with</strong>out properly nesting your elements, your pages may work on some browsers but not<br />

on others. By keeping nesting in mind, you can avoid mismatching your tags and be sure<br />

that your <strong>HTML</strong> will work in all browsers. This is going to become even more important<br />

as we get more into “industrial strength <strong>HTML</strong>” in later chapters.<br />

p<br />

p<br />

em<br />

WRONG: the tag<br />

ends before the <br />

tag! The element<br />

is supposed to be inside<br />

the element.<br />

em<br />

building blocks<br />

SAFETY FIRST<br />

Properly<br />

nest<br />

your<br />

elements<br />

BAD: here the element has leaked outside of the <br />

element, which means it’s not properly nested inside it.<br />

you are here 111

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

Saved successfully!

Ooh no, something went wrong!