04.07.2013 Views

Building iPhone Apps with HTML, CSS, and ... - Cdn.oreilly.com

Building iPhone Apps with HTML, CSS, and ... - Cdn.oreilly.com

Building iPhone Apps with HTML, CSS, and ... - Cdn.oreilly.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

font-size: 12px;<br />

font-style: italic;<br />

font-weight: bold;<br />

font-family: Arial;<br />

Selectors <strong>com</strong>e in a variety of flavors. If you wanted all of your hyperlinks (the a element)<br />

to display in italics, you would add the following to your stylesheet:<br />

a { font-style: italic; }<br />

If you wanted to be more specific <strong>and</strong> only italicize the hyperlinks that were contained<br />

somewhere <strong>with</strong>in an h1 tag, you would add the following to your stylesheet:<br />

h1 a { font-style: italic; }<br />

You can also define your own custom selectors by adding id <strong>and</strong>/or class attributes to<br />

your <strong>HTML</strong> tags. Consider the following <strong>HTML</strong> snippet:<br />

Hi there!<br />

Thanks for visiting my web page.<br />

I hope you like it.<br />

<br />

Pizza<br />

Beer<br />

Dogs<br />

<br />

If I added .loud { font-style: italic; } to the <strong>CSS</strong> for this <strong>HTML</strong>, Hi there! <strong>and</strong><br />

Pizza would show up italicized because they both have the loud class. The dot in front<br />

of the .loud selector is important. It’s how the <strong>CSS</strong> knows to look for <strong>HTML</strong> tags <strong>with</strong><br />

a class of loud. If you omit the dot, the <strong>CSS</strong> would look for a loud tag, which doesn’t<br />

exist in this snippet (or in <strong>HTML</strong> at all, for that matter).<br />

Applying <strong>CSS</strong> by id is similar. To add a yellow background fill to the highlight paragraph<br />

tag, you’d use this rule:<br />

#highlight { background-color: yellow; }<br />

Here, the # symbol tells the <strong>CSS</strong> to look for an <strong>HTML</strong> tag <strong>with</strong> the id highlight.<br />

To recap, you can opt to select elements by tag name (e.g., body, h1, p), by class name<br />

(e.g., .loud, .subtle, .error), or by id (e.g., #highlight, #login, #promo). And you can<br />

get more specific by chaining selectors together (e.g., h1 a, body ul .loud).<br />

There are differences between class <strong>and</strong> id. class attributes should be<br />

used when you have more than one item on the page <strong>with</strong> the same<br />

class value. Conversely, id values have to be unique to a page.<br />

When I first learned this, I figured I’d just always use class attributes<br />

so I wouldn’t have to worry about whether I was duping an id value.<br />

However, selecting elements by id is much faster than selecting them<br />

by class, so you can hurt your performance by overusing class selectors.<br />

Web Programming Crash Course | 7

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

Saved successfully!

Ooh no, something went wrong!