3.4 SummaryPerhaps 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
|
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
|
4.1
|
4.2 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
|
4.3
|
|
aqua |
fuchsia |
lime |
olive |
red |
white |
|
black |
gray |
maroon |
orange |
silver |
yellow |
|
blue |
green |
navy |
purple |
teal |
So, let's say you want all
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;}
Of course, you've probably seen (and maybe even used) color names besides the ones listed earlier. For example, if you specify:
h1 {color: lightgreen;}
it's likely that all of your
h1
elements will indeed
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 8-bit color spectrum, not just 17 (or 140) named colors.
Computers create colors by combining different levels of red, green, and blue, which is why color in computers is often referred to as
RGB color
. In fact, if you were to
Given the way colors are created on a monitor, it makes sense that for maximum control, you should have direct access to those color guns, determining your own mixture of the beams. 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 way.
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
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 colored a
h1 {color: rgb(75%,0%,0%);}
This makes the red component of the color lighter than
maroon
, but
h1 {color: rgb(75%,50%,50%);}
The
h1 {color: rgb(191,127,127);}
The easiest way to visualize how these values
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);}
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
h2 {color: rgb(25.5%,40%,98.6%);}
A
Values that fall outside the allowed range for each notation are "clipped" to the nearest range edge, meaning that a value that exceeds 100 % or is 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% red, 37.5% green, and 60% blue. Multiply each of these percentages by 255, and you get 63.75, 95.625, and 153. Round thse 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
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. This hexadecimal
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—they'll be rendered identically by most user
For hexadecimal numbers that are
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 .
Table 4-1
|
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%,100%,0%) |
rgb(0,255,0) |
#00FF00 |
#0F0 |
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 |
The "web-safe" colors are those colors that
With hexadecimal notation, any triplet using the values
00
,
33
,
66
,
99
,
CC
, and
FF
is