Colors and Backgrounds
- To work with the color property.
- To work with the background-color property.
- To work with the background-image property.
- To work with the background-repeat property.
- To work with the background-attachment property.
- To work with the background-position property.
About Color Values
Colors values can be specified using several ways.
Color Names
There are 17 keyword color names that are specified in CSS2 (see footnote). Modern browsers support many additional color names - everything from papayawhip to darkorchid.
<div style="color: red">color: red</div>
Hexadecimal Color Values
Hexadecimal color values take the format #rrggbb, where rr is the amount of red in the color, gg is the amount of green in the color, and bb is the amount of blue in the color.
<div style="color: #ff0000">color: #ff0000</div>
Short Hexadecimal Color Values
Hexadecimal color values can be abbreviated when a color is represented by three pairs of hexadecimal characters. For example, with #ff6600, you can remove one character from each pair. The shorthand color values take the format #rgb, where r is the amount of red, g is the amount of green in the color, and b is the amount of blue. For example, #f60 is the same as #ff6600.
selector {
color: #f60;
}
Functional Notation
Functional notation takes the format rgb(n,n,n), where n is a number between 0 and 255 or percentage between 0% and 100%.
<div style="color: rgb(255,0,0)">color: rgb(255,0,0)</div> <div style="color: rgb(100%,0%,0%)">color: rgb(100%,0%,0%)</div>
Recommendation
Our recommendation is to use three-digit hexadecimal notation (i.e, #rgb) when you can get the color you want and to use the six-digit hexadecimal notation (i.e, #rrggbb) when you need to define the color more granularly.
Color
As you have seen in the examples above, the color property is used to set the foreground color of an element.
Code Sample: BackgroundsAndColors/Demos/Color.html
<html> <head> <title>Color</title> </head> <body> <h1>Color</h1> <div style="color: red">color: red</div> <div style="color: #ff0000">color: #ff0000</div> <div style="color: #f00">color: #f00</div> <div style="color: rgb(255,0,0)">color: rgb(255,0,0)</div> <div style="color: rgb(100%,0%,0%)">color: rgb(100%,0%,0%)</div> </body> </html>
The above code will render the following:
![]()
Background-color
The background-color property is used to specify the background color of an element. It can be applied to block elements and inline elements.
selector {
background-color:color;
}
Code Sample: BackgroundsAndColors/Demos/BackgroundColor.html
<html> <head> <title>Background Color</title> </head> <body style="background-color: #0066ff;"> <h1>Background Color</h1> <div style="height: 200px; width: 500px; background-color: #ff6600;"> background-color: #ff6600 </div> </body> </html>
Background-image
Examples of properties associated with background images are shown in BackgroundsAndColors/Demos/BackgroundImage.html, which is listed at the end of this section.
The background-image property is used to specify the background image of an element. It can be applied to block elements and inline elements.
selector {
background-image:url(url)
(see footnote);
}
Background-repeat
The background-repeat property is used with background-image to specify whether and how a background image should repeat. Possible values are listed below.
- no-repeat - does not tile
- repeat-x - tiles horizontally
- repeat-y - tiles vertically
selector {
background-image:url(url);
background-repeat:value;
}
Background-attachment
The background-attachment property is used with background-image to specify whether a background image should scroll as the content is scrolled or whether the content should scroll over it. Possible values are listed below.
- scroll
- fixed
selector {
background-image:url(url);
background-attachment:value;
}
According to the specification, background-attachment specifies whether a background image is fixed relative to the viewport (e.g, the browser window) or scrolls along with the document. Internet Explorer 6 fixes the background image relative to the container, which is incorrect.
Background-position
The background-position property is used with background-image to specify the location of a background image. Possible values are listed below.
- top
- right
- bottom
- left
- center
- any combination of the above (e.g, top center or bottom left)
selector {
background-image:url(url);
background-position:value;
}
The following page shows examples of different combinations of background properties.
Code Sample: BackgroundsAndColors/Demos/BackgroundImage.html
<html> <head> <title>Background Image</title> </head> <body> <h1>Background Image</h1> <div style="height:200px; width:500px;color:#ffffff; background-image:url(Images/block.gif)"> background-image:url(Images/block.gif) </div> <h2>Background Repeat</h2> <div style="height:200px; width:500px;color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat </div> <hr/> <div style="height:200px; width:500px;color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-x"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-x </div> <hr/> <div style="height:200px; width:500px;color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-y"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-y </div> <h2>Background Attachment</h2> <h3>background-attachment:fixed</h3> <div style="height:200px; width:500px;color:#ffffff; overflow:scroll; white-space:pre; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-attachment:fixed"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-attachment:fixed<br />---- Code Omitted ----background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-attachment:fixed </div> <h3>background-attachment:scroll</h3> <div style="height:200px; width:500px;color:#ffffff; overflow:scroll; white-space:pre background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-attachment:scroll"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-attachment:scroll<br />---- Code Omitted ----background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-attachment:scroll </div> <h2>Background Position</h2> <h3>background-position:right</h3> <div style="height:200px; width:500px; color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-y; background-position:right;"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-y; background-position:right </div> <h3>background-position:bottom</h3> <div style="height:200px; width:500px; color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-x; background-position:bottom;"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:repeat-x; background-position:bottom </div> <h3>background-position:center</h3> <div style="height:200px; width:500px; color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-position:center;"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-position:center </div> <h3>background-position:20% 20%</h3> <div style="height:200px; width:500px; color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-position:20% 20%;"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-position:20% 20%; </div> <h3>background-position:20px 20px</h3> <div style="height:200px; width:500px; color:#ffffff; background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-position:20px 20px;"> background-color:#ff6600; background-image:url(Images/block.gif); background-repeat:no-repeat; background-position:20px 20px; </div> </body> </html>
Exercise: Colors and Backgrounds
In this exercise, you will continue to work on Stories.html by applying styles to the links on the page.
- Open CssText/Exercises/Stories.html, which you were working on in the last lesson and save it in the BackgroundsAndColors/Exercises directory.
- Modify the color and background properties of the different elements on the page. You may do this using inline styles, an embedded style sheet and/or an external style sheet. You are also welcome to add tags to the page. The object of this exercise is to get used to working with backgrounds and colors in CSS.
- There are images in Exercises/Images that you may want to use. You're also welcome to use any other images you have access to.
- When you are done, open Stories.html in your browser to see the results. You are welcome to go back to the code and continue to work.
Colors and Backgrounds Conclusion
In this lesson of the CSS tutorial, you have learned to use CSS background and color properties.
Footnotes
-
The list can be found at http://www.w3.org/TR/CSS21/syndata.html#color-units.
-
Some documentation URL in parentheses and quotes (e.g, url(url). The W3C site documentation is unclear on which, if either, is preferred, but both seem to work equally well in practice.