Which syntax helps in specifying the colour of the text? Give reason.
Syntax 1: Color: white
Syntax 2: Color: rgb(128,0,0)
Syntax 3: Color: #ff00ff
Answers
Answer:
Defining Colors in CSS
We've already seen some properties in CSS that take color values. Here's an example:
p {
color: red;
}
There are several different ways to specify colors in CSS.
Color Keywords
RGB
RGBA
HSL
HSLA
Hexadecimal
Color Keywords
The first and easiest way to specify a color is using one of the 140 predefined color keywords specified in CSS.
The original 17 are listed below. An unsightly bunch...
Color Keyword Hex Value
black #000000
gray #808080
silver #c0c0c0
white #ffffff
maroon #800000
red #ff0000
purple #800080
fuchsia #ff00ff
green #008000
Color Keyword Hex Value
lime #00ff00
olive #808000
yellow #ffff00
navy #000080
blue #0000ff
teal #008080
aqua #0000ff
orange #ffa500
Clearly most of these colors are unsuitable for normal web design. Color keywords are most useful for testing and demonstration purposes (like in these pages).
All 140 color keywords are listed here in alphabetical order. You can also find more helpful listings at these sites:
147 Colors
HTML Color Codes: Color Names
You can also view a printout of all the swatches on pages 304-305 in Learning Web Design.
RGB Color Values
Most of you have probably heard about CMYK values for print design. RGB, which stands for red, green, and blue is the color model that monitors use. Since in web design we're primarily concerned with what web pages look like on screens, RGB is the color model we use.
RGB colors have three values that represent: red, green, and blue
Each value can be a number between 0 and 255 or a percentage from 0 to 100%
A value of 0 means none of that color is being used
A value of 255 or 100% means all of that color is being used
A 0 for all three color values will be black
A 255 or 100% for all three color values will be white
The CSS syntax for using RGB colors is a little different than we've seen before. In the example below, we are styling:
A black paragraph
A white h1
A purple unordered list
p { color: rgb(0, 0, 0); } /* black */
h1 { color: rgb(255, 255, 255); } /* white */
ul { color: rgb(128, 80, 200); } /* purple */
/* Percentages work too */
h1 { color: rgb(100%, 100%, 100%); } /* white */
RGBA Color Values
RGBA is all the rage.
Seriously though, it's just like RGB, except with the addition of a fourth value: the alpha channel.
The alpha value represents the level of transparency that the rgb color should have. It can be a value from 0 to 1 or a percentage from 0 to 100%. Note that you must specify RGBA instead of RGB.
See RGBA example
HSL
The HSL color model is one of the least used, but gaining traction because can be more intuitive to use when working with shades and color adjustments.
HSL stands for: hue, saturation, and lightness
Hue
A picture of the HSL color wheel
Hue is defined by a color wheel. Each color represents a degree on the wheel. This page shows a nice visual animation of the color wheel:
HSL Color wheel demo
Saturation
The boxes below demonstrate varying levels of saturation (color intensity) for the hue at 240°. The lightness value is set at 50% for each box.
100%75%50%25%0%
Lightness
Here's the same hue (240°) shown at various lightness levels with 100% saturation.
100%75%50%25%0%
HSL Color Grid Example
This grid shows varying saturation and lightness together.
hsl(240, 100%, 90%)hsl(240, 100%, 75%)hsl(240, 100%, 50%)hsl(240, 100%, 25%)hsl(240, 100%, 10%)hsl(240, 75%, 90%)hsl(240, 75%, 75%)hsl(240, 75%, 50%)hsl(240, 75%, 25%)hsl(240, 75%, 10%)hsl(240, 50%, 90%)hsl(240, 50%, 75%)hsl(240, 50%, 50%)hsl(240, 50%, 25%)hsl(240, 50%, 10%)hsl(240, 25%, 90%)hsl(240, 25%, 75%)hsl(240, 25%, 50%)hsl(240, 25%, 25%)hsl(240, 25%, 10%)hsl(240, 0%, 90%)hsl(240, 0%, 75%)hsl(240, 0%, 50%)hsl(240, 0%, 25%)hsl(240, 0%, 10%)
HSLA
HSLA is simply the HSL color model with the addition of an alpha channel. This works exactly the same way as the alpha channel in RGBA.
h1 {
background-color: hsla(240, 25%, 50%, .5);
}
Example of HSLA
Example of HSLA (on different background color)
Hexadecimal Color Values