Colors and Backgrounds
Colors and Backgrounds
learningwebdesign.com/colornames.html
Numeric Color Values
For more control, define colors numerically using one of
these color models:
– RGB (combination of red, green, and blue light
values)
– RGBa (RGB plus alpha transparency)
– HSL (hue, saturation, and luminosity)
– HSLa (HSL plus alpha transparency)
RGB Color
The RGB color model mixes color with red, green, and
blue light.
Each channel can have 256 shades, for millions of color
options.
RGB Values in Style Rules
There are four formats for providing RGB color values:
– RGB values (0 to 255): rgb(200,178,230)
– Percentage values: rgb(78%,70%,90%)
– Hexadecimal values: #C8B2E6
– Condensed hexadecimal values (for double-digits
only): #F06 is the same as #FF0066
Hexadecimal RGB Values
Red, green, and blue values converted to hexadecimal
and preceded by the # symbol.
RGBa Color
• RGB + an alpha channel for transparency
• The first three values are RGB. The fourth is the
transparency level from 0 (transparent) to 1 (opaque).
HSL and HSLa
• Colors described by values for hue (°), saturation (%),
and luminosity (%):
hsl(180,50%,75%)
• Hue specifies the position on a color wheel (in degrees)
that has red at 0°, green at 120°, and blue at 240°.
• HSL is less commonly used than RGB, but some find it
more intuitive.
• HSLa adds an alpha value for transparency.
Foreground Color
color
Values: number (0 to 1)
Example:
h1 {color: gold; background: white; opacity: .25;}
Values:
repeat, no-repeat, repeat-x, repeat-y, space, round
Values:
Length, percentage, left, center, right, top, bottom
Values:
Length, percentage, auto, cover, contain
Values:
background-color background-image background-repeat
background-attachment background-position background-
clip background-origin background-size
body {
background:
url(image1.png) left top no-repeat,
url(image2.png) center center no-repeat,
url(image3.png) right bottom no-repeat;
}
Gradient Fills
• A gradient is a transition from one color to another.
Example:
#banner {
background-image: linear-gradient(180deg, aqua, green);
}
Linear Gradient
The linear-gradient() notation provides the angle of a
gradient line and the colors the line passes through.
It is specified in degrees (deg) or keywords (to top, to
right, to bottom, to left).
Radial Gradients
The radial-gradient() notation provides the color values
and optional size, shape, and position information.
Gradient Vendor Prefixes
Because the gradient spec has changed over time, gradients require
significant prefixing and alternate values:
background: #ffff00; /* Old browsers */
background: -moz-linear-gradient(top, #ffff00 0%, #00ff00 100%);
/* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffff00), color-
stop(100%,#00ff00));
/* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #ffff00 0%,#00ff00 100%);
/* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #ffff00 0%,#00ff00 100%);
/* Opera 11.10+ */
background: -ms-linear-gradient(top, #ffff00 0%,#00ff00 100%);
/* IE10+ */
background: linear-gradient(to bottom, #ffff00 0%,#00ff00 100%);
/* W3C Standard */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffff00',
endColorstr='#00ff00',GradientType=0 );
/* IE6-9 */
Link Pseudo-classes
:link Applies style to unvisited (unclicked) links
:visited Applies style to visited links
• Use the rel attribute to say it’s a style sheet. Use href
to provide the URL of the .css file (relative to the current
document):
<head>
<title>Titles are required.</title>
<link rel="stylesheet" href="/path/stylesheet.css">
</head>
Attaching a Style Sheet
with an @import rule
An @import rule imports the contents of an external style
sheet into another style sheet (either a .css document or
embedded with style):
<head>
<style>
@import url("/path/stylesheet.css");
p { font-face: Verdana;}
</style>
<title>Titles are required.</title>
</head>