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.
ColorValuesAbout Color Values
Colors values can be specified using several ways.
Color Names
There are 17 keyword color names that are specified in CSS2 . 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
Short hexadecimal color values take the format #rgb, where r is the amount of red in the color, g is the amount of green in the color, and b is the amount of blue in the color. Short hexadecimal color values can be converted to regular hexadecimal color values by repeating each character. For example, #f60 is the same as #ff6600.
<div style="color: #f00">color:#f00</div>
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 stick to the regular hexadecimal notation (i.e, #rrggbb) as it has the best support and provides the most flexibility.
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");
}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>
Colors and Backgrounds Conclusion
In this lesson of the CSS tutorial, you have learned to use CSS background and color properties.
