Changing Background Colors
Print This Post
One CommentRelated Reading |
This section of your free online web design course is going to teach you how to add a background color to your HTML documents, HTML tables, table cells and even your texts! We’re going to start off by adding a background color to the whole web page.
HTML Document Background Colors
In order to change the background color of your HTML document, we are going to add a special attribute, called “bgcolor”, to the <body> tag, and assign a color value to this attribute.
Copy & paste the below HTML code to create a blank HTML document with a black background:
| <html> <head> </head> </body> |
The output of the above HTML code is below:
We can set the value for the “bgcolor” attribute as a color hex code as well. Let’s set it to “#99CCFF”, which is a web safe color (Sidenote: If you do not know what I am talking about, go to Lesson 1-04 to learn all about web safe colors and color hex codes):
| <html> <head> </head> </body> |
The above HTML code will produce a blank HTML document with “#99CCFF” as the background color:
HTML Table Background Colors
We’re going to continue our free online web design course by learning how to add a background color to tables. It is very similar to adding a background color to an HTML document, as the same attribute (bgcolor) is used to assign a background color to a table. We’ll insert the bgcolor attribute inside the <table> tag just like we did with the body tag above:
| <html> <head> </head> <table bgcolor=”#FFCC00″> </body> |
In the above HTML code, we have created a table with a background color of “#FFCC00″ inside an HTML document which has a background color of “#99CCFF”. The output of this code is below:
|
As we did not define a table border in the source code, our table doesn’t have borders, but the background color still makes it visible.
HTML Table Cell Background Colors
Next in Online Web Design Course, we’re going to learn how to add a background color to individual cells of a table. This is achieved by using the “bgcolor” attrribute with the <td> tags. Below is an example HTML code:
| <html> <head> </head> <table bgcolor=”#FFCC00″> </body> |
As you can see, we have added the “bgcolor” attribute to both of the <td> tags. Let’s see what the above HTML code looks like in a browser:
|
Hmmm… Looks like we have “borders”, although we never assigned a “border” attribute to our table. What may be wrong?
Those are not real borders, but the background color of our table leaking out from some “cracks”. The reason we see the background color of our table is that the default “cellspacing” attribute of our table is set to “2″. We see the background color through these “2-pixel-spaces” between cells. If we set the “cellspacing” attribute to “0″, we’ll not see through these spaces anymore, so our fake border will be removed. Below is the HTML code for that:
| <html> <head> </head> <table bgcolor=”#FFCC00″ cellspacing=”0″> </body> |
And we’ll get a borderless table with two different-color cells:
|
Sidenote: In order to get around the border problem, we could simply remove the background color for our table (and making it transparent), instead of adding a cellspacing attribute! Like this:
| <html> <head> </head> <table> </body> |
The above HTML code will create the same effect, except that there will still be a 2-pixel-space between the two cells:
|
Text Background Colors
The last part of this section of our free online web design course is going to explore how to add a background color to texts. To achieve this effect, we’re going to use Cascading Style Sheets (or shortly “CSS”), which we’ll cover in much greater depth later on in our online web design course.
In order to add a background color to texts, we are going to use the <span> tag and the “style” attribute together. The <span> tag is going to help us determine the starting and ending points of our effect, and the “style” attribute is going to do the actual work of adding a background color. Have a look at the below HTML code to get a clearer picture:
| <html> <head> </head> This is plain text <span style=”background-color:#CCCCFF”> <br><br> </body> |
The above HTML code will produce the below output:
| This is plain text
This is plain text again |
The “style” attribute is genereally used as style=”property:value”. In the above example, our property is “background-color” and our value is “#CCCCFF”, which is the hex code of the color I have selected as background color for our text.
If you enjoyed this lesson, please link to it
We’ve learned how to add background colors. How about background images? Next lesson of Online Web Design Course is going to teach you how to add background images to your HTML documents!









the code for: Text Background Colors did not work while I was using Firefox. Why?