Read CSS: The Definitive Guide, 3rd Edition Online

Authors: Eric A. Meyer

Tags: #COMPUTERS / Web / Page Design

CSS: The Definitive Guide, 3rd Edition (12 page)

BOOK: CSS: The Definitive Guide, 3rd Edition
6.83Mb size Format: txt, pdf, ePub
ads
Summary

Perhaps the most fundamental aspect of Cascading Style Sheets is the cascade
itself—the process by which conflicting declarations are sorted out and from which the
final document presentation is determined. Integral to this process is the specificity
of selectors and their associated declarations, and the mechanism of inheritance.

In the next chapter, we will look at the many types of units that are used to give
property values their meaning. Once we have completed that discussion, the fundamentals
will be out of the way, and you'll be ready to learn about the properties used to style
documents.

Chapter 4. Values and Units

In this chapter, we'll tackle the elements that are the basis for almost everything you
can do with CSS: the
units
that affect the colors, distances, and
sizes of a whole host of properties. Without units, you couldn't declare that a paragraph
should be purple, or that an image should have 10 pixels of blank space around it, or that
a heading's text should be a certain size. By understanding the concepts put forth here,
you'll be able to learn and use the rest of CSS much more quickly.

Numbers

There are two types of numbers in CSS:
integers
("whole" numbers) and
reals
fractional numbers). These number types serve
primarily as the basis for other value types, but, in a few instances, raw numbers can
be used as a value for a property.

In CSS2.1, a real number is defined as an integer that is optionally followed by a
decimal and fractional numbers. Therefore, the following are all valid number values:
15.5, -270.00004, and 5. Both integers and reals may be either positive or negative,
although properties can (and often do) restrict the range of numbers they will
accept.

Percentages

A
percentage value
is a calculated real number followed by a
percentage sign (
%
). Percentage values are nearly
always relative to another value, which can be anything, including the value of another
property of the same element, a value inherited from the parent element, or a value of
an ancestor element. Any property that accepts percentage values
will define any restrictions on the range of allowed
percentage values, and will also define the degree to which the percentage is relatively
calculated.

Color

One of the first questions every starting web author
asks is, "How do I set colors on my page?" Under HTML, you have two choices: you could
use one of a small number of colors with names, such as
red
or
purple
, or employ a vaguely cryptic
method using hexadecimal codes. Both of these methods for describing colors remain in
CSS, along with some other—and, I think, more intuitive—methods.

Named Colors

Assuming that you're content to pick from a small, basic set of colors, the
easiest method is simply to use the name of the color you want. CSS calls these color
choices, logically enough,
named colors
.

Contrary to what some browser makers might have you believe, you have a limited
palette of valid named-color keywords. For example, you're not going to be able to
choose "mother-of-pearl" because it isn't a defined color. As of CSS2.1, the CSS
specification defines 17 color names.
These are the 16 colors defined in HTML 4.01 plus
orange:

aqua
olive
black
orange
blue
purple
fuchsia
red
gray
silver
green
teal
lime
white
maroon
yellow
navy

So, let's say you want all first-level headings to be maroon. The best declaration
would be:

h1 {color: maroon;}

Simple and straightforward, isn't it?
Figure
4-1
shows a few more examples:

h1 {color: gray;}
h2 {color: silver;}
h3 {color: black;}

Figure 4-1. Naming colors

Of course, you've probably seen (and maybe even used) color names other than the
ones listed earlier. For example, if you specify:

h1 {color: lightgreen;}

It's likely that all of your
h1
elements will
indeed turn light green, despite
lightgreen
not
being on the list of named colors in CSS2.1. It works because most web browsers
recognize as many as 140 color names,
including the standard 17. These extra colors are
defined in the CSS3 Color specification, which is not covered in this book. The 17
standard colors (as of this writing) are likely to be more reliable than the longer
list of 140 or so colors because the color values for these 17 are defined by CSS2.1.
The extended list of 140 colors given in CSS3 is based on the standard X11 RGB values
that have been in use for decades, so they are likely to be very well supported.

Fortunately, there are more detailed and precise ways to specify colors in CSS.
The advantage is that, with these methods, you can specify any color in the color
spectrum, not just 17 (or 140) named colors.

Colors by RGB

Computers create colors by combining
different levels of red, green, and blue, a combination that is often referred to as
RGB color
. In fact, if you were to open up an old CRT
computer monitor and dig far enough into the projection tube, you would discover
three "guns." (I don't recommend trying to find the guns, though, if you're worried
about voiding the monitor's warranty.) These guns shoot out electron beams at varying
intensities at each point on the screen. Then, the brightness of each beam combines
at those points on your screen, forming all of the colors you see. Each point is
known as a
pixel
, which is a term we'll return to later in this
chapter. Even though most monitors these days don't use electron guns, their color
output is still based on RGB mixtures.

Given the way colors are created on a monitor, it makes sense that you should have
direct access to those colors, determining your own mixture of the three for maximum
control. That solution is complex, but possible, and the payoffs are worth it because
there are very few limits on which colors you can produce. There are four ways to
affect color in this manner.

Functional RGB
colors

There are two color value types that use
functional RGB notation
as opposed to
hexadecimal notation. The generic syntax for this type of color value is
rgb(color)
, where
color
is expressed using a triplet of either percentages or integers.
The percentage values can be in the range
0%

100%
, and the integers can be
in the range
0

255
.

Thus, to specify white and black, respectively, using
percentage notation, the values would
be:

rgb(100%,100%,100%)
rgb(0%,0%,0%)

Using
the integer-triplet notation, the same colors would be represented
as:

rgb(255,255,255)
rgb(0,0,0)

Assume
you want your
h1
elements to be a shade of red
that lies between the values for red and maroon.
red
is equivalent to
rgb(100%,0%,0%)
, whereas
maroon
is
equal to
(50%,0%,0%)
. To get a color between
those two, you might try
this:

h1 {color: rgb(75%,0%,0%);}

This
makes the red component of the color lighter than
maroon
, but darker than
red
. If, on
the other hand, you want to create a pale red color, you would raise the green and
blue
levels:

h1 {color: rgb(75%,50%,50%);}

The
closest equivalent color using integer-triplet notation
is:

h1 {color: rgb(191,127,127);}

The
easiest way to visualize how these values correspond to color is to create a table
of gray values. Besides, grayscale printing is all we can afford for this book, so
that's what we'll do in
Figure
4-2
:

p.one {color: rgb(0%,0%,0%);}
p.two {color: rgb(20%,20%,20%);}
p.three {color: rgb(40%,40%,40%);}
p.four {color: rgb(60%,60%,60%);}
p.five {color: rgb(80%,80%,80%);}
p.six {color: rgb(0,0,0);}
p.seven {color: rgb(51,51,51);}
p.eight {color: rgb(102,102,102);}
p.nine {color: rgb(153,153,153);}
p.ten {color: rgb(204,204,204);}

Figure 4-2. Text set in shades of gray

Of course, since we're dealing in shades of gray, all three RGB values
are the same in each statement. If any one of them were different from the others,
then a color would start to emerge. If, for example,
rgb(50%,50%,50%)
were modified to be
rgb(50%,50%,60%)
, the result would be a medium-dark color with just
a hint of blue.

It is possible to use fractional numbers in percentage
notation. You might, for some reason, want to specify that a color be exactly 25.5
percent red, 40 percent green, and 98.6 percent
blue:

h2 {color: rgb(25.5%,40%,98.6%);}

A
user agent that ignores the decimal points (and some do) should round the value to
the nearest integer, resulting in a declared value of
rgb(26%,40%,99%)
. In integer triplets, of course, you are limited to
integers.

Values that fall outside the allowed range for each notation
are "clipped" to the nearest range edge, meaning that a value that is greater than
100%
or less than
0%
will default to those allowed extremes. Thus, the following
declarations would be treated as if they were the values indicated in the
comments:

P.one {color: rgb(300%,4200%,110%);}   /*  100%,100%,100%  */
P.two {color: rgb(0%,-40%,-5000%);} /* 0%,0%,0% */
p.three {color: rgb(42,444,-13);} /* 42,255,0 */

Conversion
between percentages and integers may seem arbitrary, but there's no need to guess
at the integer you want—there's a simple formula for calculating them. If you know
the percentages for each of the RGB levels you want, then you need only apply them
to the number 255 to get the resulting values. Let's say you have a color of 25
percent red, 37.5 percent green, and 60 percent blue. Multiply each of these
percentages by 255, and you get 63.75, 95.625, and 153. Round these values to the
nearest integers, and
voilà
:
rgb(64,96,153)
.

Of course, if you already know the
percentage values, there isn't much point in converting them into integers.
Integer notation is more useful for people who use programs such as Photoshop,
which can display integer values in the "Info" dialog, or for those who are so
familiar with the technical details of color generation that they normally think
in values of 0–255. Then again, such people are probably more familiar with
thinking in hexadecimal notation, which is our next topic.

Hexadecimal RGB colors

CSS allows you to define a color using the same
hexadecimal color notation
so familiar to HTML web authors:

h1 {color: #FF0000;}   /* set H1s to red */
h2 {color: #903BC0;} /* set H2s to a dusky purple */
h3 {color: #000000;} /* set H3s to black */
h4 {color: #808080;} /* set H4s to medium gray */

Computers have been using "hex notation" for quite some time now, and
programmers are typically either trained in its use or pick it up through
experience. Their familiarity with hexadecimal notation likely led to its use in
setting colors in old-school HTML. The practice was simply carried over to CSS.

Here's how it works: by stringing together three hexadecimal numbers in the
range
00
through
FF
, you can set a color. The generic syntax for this notation is
#RRGGBB
. Note that there are no spaces,
commas, or other separators between the three numbers.

Hexadecimal notation is mathematically equivalent to the integer-pair notation
discussed in the previous section. For example,
rgb(255,255,255)
is precisely equivalent to
#FFFFFF
, and
rgb(51,102,128)
is
the same as
#336680
. Feel free to use whichever
notation you prefer—it will be rendered identically by most user agents. If you
have a calculator that converts between decimal and hexadecimal, making the jump
from one to the other should be pretty simple.

For hexadecimal numbers that are composed of three matched pairs of digits, CSS
permits a shortened notation. The generic syntax of this notation is
#RGB
:

h1 {color: #000;}   /* set H1s to black */
h2 {color: #666;} /* set H2s to dark gray */
h3 {color: #FFF;} /* set H3s to white */

As you can see from the markup, there are only three digits in each color
value. However, since hexadecimal numbers between
00
and
FF
need two digits each, and
you have only three total digits, how does this method work?

The answer is that the browser takes each digit and replicates it. Therefore,
#F00
is equivalent to
#FF0000
,
#6FA
would be the same as
#66FFAA
, and
#FFF
would come out
#FFFFFF
, which is the same as
white
.
Obviously, not every color can be represented in this manner. Medium gray, for
example, would be written in standard hexadecimal notation
as
#808080
. This cannot be expressed in shorthand; the closest equivalent
would be
#888
, which is the same as
#888888
.

Bringing the colors
together

Table 4-1
presents an overview of
some of the colors we've discussed. These color keywords might not be recognized
by browsers and, therefore, they should be defined with either RGB or hex-pair
values (just to be safe). In addition, there are some shortened hexadecimal values
that do not appear at all. In these cases, the longer (six-digit) values cannot be
shortened because they do not replicate. For example, the value
#880
expands to
#888800
, not
#808000
(otherwise
known as
olive
). Therefore, there is no
shortened version of
#808000
, and the
appropriate entry in the table is blank.

Table 4-1. Color equivalents

Color

Percentage

Numeric

Hexadecimal

Short hex

red

rgb(100%,0%,0%)

rgb(255,0,0)

#FF0000

#F00

orange

rgb(100%,40%,0%)

rgb(255,102,0)

#FF6600

#F60

yellow

rgb(100%,100%,0%)

rgb(255,255,0)

#FFFF00

#FF0

green

rgb(0%,50%,0%)

rgb(0,128,0)

#008000

 

blue

rgb(0%,0%,100%)

rgb(0,0,255)

#0000FF

#00F

aqua

rgb(0%,100%,100%)

rgb(0,255,255)

#00FFFF

#0FF

black

rgb(0%,0%,0%)

rgb(0,0,0)

#000000

#000

fuchsia

rgb(100%,0%,100%)

rgb(255,0,255)

#FF00FF

#F0F

gray

rgb(50%,50%,50%)

rgb(128,128,128)

#808080

 

lime

rgb(0%,100%,0%)

rgb(0,255,0)

#00FF00

#0F0

maroon

rgb(50%,0%,0%)

rgb(128,0,0)

#800000

 

navy

rgb(0%,0%,50%)

rgb(0,0,128)

#000080

 

olive

rgb(50%,50%,0%)

rgb(128,128,0)

#808000

 

purple

rgb(50%,0%,50%)

rgb(128,0,128)

#800080

 

silver

rgb(75%,75%,75%)

rgb(192,192,192)

#C0C0C0

 

teal

rgb(0%,50%,50%)

rgb(0,128,128)

#008080

 

white

rgb(100%,100%,100%)

rgb(255,255,255)

#FFFFFF

#FFF

BOOK: CSS: The Definitive Guide, 3rd Edition
6.83Mb size Format: txt, pdf, ePub
ads

Other books

i f38de1664e17c992 by Your User Name
Swingin' in the Rain by Eileen Davidson
Screams From the Balcony by Bukowski, Charles
Eighty Days Blue by Vina Jackson
Love: Classified by Jones, Sally-Ann