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 rgb values<br />

Specify color in red, green and blue values<br />

You can also specify a color as the amount of red, green, and blue. So,<br />

say you wanted to specify the orange color we looked at a couple of<br />

pages back, which consisted of 80% red, 40% green, and 0% blue.<br />

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

body {<br />

background-color: rgb(80%, 40%, 0%);<br />

}<br />

368 Chapter 9<br />

Begin <strong>with</strong> “rgb”, short<br />

for red, green, blue.<br />

You can also specify the red, green, and blue values as a numeric value<br />

between 0 and 255. So, instead of 80% red, 40% green, and 0% blue,<br />

you can use 204 red, 102 green, and 0 blue.<br />

Here’s how you use straight numeric values to specify your color:<br />

And then specify the percentages for<br />

red, green, and blue <strong>with</strong>in parentheses,<br />

and <strong>with</strong> a % sign after each one.<br />

body {<br />

background-color: rgb(204, 102, 0);<br />

}<br />

Q: Why are there two different ways<br />

to specify rgb values? Don’t percentages<br />

seem more straightforward?<br />

A: Sometimes they are more<br />

straightforward, but there is some sanity to<br />

using numbers between 0 and 255. This<br />

number is related to the number of values<br />

that can be held in one byte of information.<br />

So, for historical and technical reasons, 255<br />

is often used as a unit of measurement for<br />

specifying red, green, and blue values in a<br />

color. In fact, you might have noticed that<br />

We still start <strong>with</strong> “rgb”.<br />

there are no<br />

Dumb Questions<br />

photo-editing applications often allow you<br />

to specify color values from 0 to 255 (if not,<br />

you’ll see how to do that shortly).<br />

Q: I never see anyone use rgb or<br />

actual color names in their <strong>CSS</strong>. It seems<br />

everyone uses the #00fc9a type of color<br />

codes.<br />

A: Using rgb percents or numeric<br />

values are becoming more common, but you<br />

are right, “hex codes” are still the most<br />

80% Red 0% Blue<br />

40% Green<br />

Where did these numbers come from?<br />

80% of 255 is 204,<br />

40% of 255 is 102, and<br />

0% of 255 is 0.<br />

To specify numeric values and not<br />

percentages, just type in the value<br />

and don’t use a “%”.<br />

widely used because people consider them<br />

a convenient way to specify color.<br />

Q: Is it important that I look at<br />

something like rgb(100, 50, 200) and<br />

know what color it is?<br />

A: Not at all. The best way to know<br />

what rgb(100, 50, 200) looks like is to load<br />

it in your browser or use a photo-editing<br />

application to see it.

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

Saved successfully!

Ooh no, something went wrong!