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

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

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

Intro to <strong>CSS</strong><br />

Some text editors are bad for authoring <strong>HTML</strong>. In particular, you want<br />

to avoid editors that support rich text editing, like Microsoft Word or<br />

TextEdit. These types of editors can save their files in formats other than<br />

plain text, which will break your <strong>HTML</strong>. If you are in the market for a<br />

good text editor, my favorite by far on the Mac is TextMate (http://<br />

macromates.<strong>com</strong>/), <strong>and</strong> I hear that there is a clone version for Windows<br />

called E Text Editor (http://www.e-texteditor.<strong>com</strong>/). If free is your thing,<br />

you can download Text Wrangler for Mac (http://www.barebones.<strong>com</strong>/<br />

products/TextWrangler/) or use the built-in Notepad on Windows.<br />

As you’ve seen, browsers render certain <strong>HTML</strong> elements <strong>with</strong> distinct styles (headings<br />

are large <strong>and</strong> bold, paragraphs are followed by a blank line, etc.). These styles are very<br />

basic <strong>and</strong> are primarily intended to help the reader underst<strong>and</strong> the structure <strong>and</strong> meaning<br />

of the document.<br />

To go beyond this simple structure-based rendering, you can use Cascading Style Sheets<br />

(<strong>CSS</strong>). <strong>CSS</strong> is a stylesheet language that is used to define the visual presentation of an<br />

<strong>HTML</strong> document. You can use <strong>CSS</strong> to define simple things like the text color, size, <strong>and</strong><br />

style (bold, italic, etc.), or <strong>com</strong>plex things like page layout, gradients, opacity, <strong>and</strong> much<br />

more.<br />

Example 1-4 shows a <strong>CSS</strong> rule that instructs the browser to display any text in the body<br />

element using the color red. In this example, body is the selector (what is affected by the<br />

rule) <strong>and</strong> the curly braces enclose the declaration (the rule itself). The declaration includes<br />

a set of properties <strong>and</strong> their values. In this example, color is the property, <strong>and</strong><br />

red is the value of the property.<br />

Example 1-4. A simple <strong>CSS</strong> rule<br />

body { color: red; }<br />

Property names are predefined in the <strong>CSS</strong> specification, which means that you can’t<br />

just make them up. Each property expects an appropriate value, <strong>and</strong> there can be lots<br />

of appropriate values <strong>and</strong> value formats for a given property.<br />

For example, you can specify colors <strong>with</strong> predefined keywords like red, or by using<br />

<strong>HTML</strong> color code notation. This uses a hexadecimal notation: three pairs of hexadecimal<br />

digits (0–F) representing (from left to right) Red, Green, <strong>and</strong> Blue values. Properties<br />

that expect measurements can accept values like 10px, 75%, <strong>and</strong> 1em. Example 1-5<br />

shows some <strong>com</strong>mon declarations. (The color code shown for background-color corresponds<br />

to the <strong>CSS</strong> “gray”.)<br />

Example 1-5. Some <strong>com</strong>mon <strong>CSS</strong> declarations<br />

body {<br />

color: red;<br />

background-color: #808080;<br />

6 | Chapter 1: Getting Started

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

Saved successfully!

Ooh no, something went wrong!