HTML Table Background
This page contains HTML table background code. This code enables you to modify the background of your HTML tables. For example, you can change the background color or add a background image to your tables.
Setting All Background Styles
There is a shorthand CSS property that allows you to set all your background styles in one go. This property is the background property.
This is the property that most web developers use for setting their table backgrounds, as it's quicker to use and it helps to minimize code. This property allows you to set the background color and a background image properties using one property. You can see it in action with the following example.
If you have difficulty understanding this property, have a look at the other examples on this page. They should make things a bit clearer for you.
Run
Stack editor
Unstack editor
Example.myTableBg {
width: 100%;
text-align: left;
background: white url(/pix/samples/celebrate.gif) repeat;
border-collapse: collapse;
}
.myTableBg td, .myTableBg th {
border:2px solid black;
padding:5px;
}
Header | Header |
---|---|
Table cell | Table cell |
Table cell | Table cell |
Table cell | Table cell |
Background Color
The following example demonstrates how background color can be applied to the table using the background-color property. Check out HTML Table Background Color to see more examples of adding background color to a table.
Run
Stack editor
Unstack editor
Example.myTableBg2 {
width: 100%;
text-align: left;
background-color: lemonchiffon;
}
Header | Header |
---|---|
Table cell | Table cell |
Table cell | Table cell |
Table cell | Table cell |
Table Background Image
Here's an example of adding a background image to your table by using the CSS background-image property.
The background image in this example automatically repeats across the full width and height of the table. In other words, if the background image is smaller than the table, it will be displayed again and again until it reaches the edge of the table.
Here's the actual image that we will use in the following example:
As you can see, this image is smaller than the width of the table, so it repeated across the full width and height of the table.
In this example, we also add a thin border to our table. Here's more about HTML table borders.
Run
Stack editor
Unstack editor
Example.myTableBg3 {
width:100%;
min-width: 260px;
min-height:200px;
text-align: center;
background-image:url('/pix/samples/bg1.gif');
border:1px solid black;
}
Header | Header |
---|---|
Table cell | Table cell |
Table cell | Table cell |
Table cell | Table cell |
Non-Repeating Background Image
Sometimes you might not want the background image to repeat. Sometimes you might want it to appear once only. For this effect, you can use the CSS background-repeat property. You can also use the background-position property to define where the image is positioned within the table. For example, you could position the image in the center of the table, or at the top-left or bottom-right etc.
Run
Stack editor
Unstack editor
Example.myTableBg4 {
width:100%;
min-width: 260px;
min-height:200px;
text-align: center;
background-image:url('/pix/samples/celebrate.gif');
background-repeat: no-repeat;
background-position: center center;
border: 1px solid black;
}
Header | Header |
---|---|
Table cell | Table cell |
Table cell | Table cell |
Table cell | Table cell |
Background Image on Table Cells
You could also add a background image to each individual table cell. You could use a different image for each cell or the same image. If you use the same image, it would give it that "repeat" effect and the "repeat" could be precisely aligned with each table cell (but it doesn't have to be). You could also achieve this effect using a repeating background image on the whole table, but you'd need to ensure that each cell is exactly the same size as the image, and you might need to play around with the table border.
Here's an example of adding a background image to each cell using a CSS class with an embedded style sheet:
Run
Stack editor
Unstack editor
Example.cellImage {
width: 100%;
text-align: center;
border: 1px solid black;
}
.cellImage td {
width:120px;
height:70px;
padding:5px;
background-image: url(/pix/samples/celebrate.gif);
background-position: center center;
background-repeat: no-repeat;
}
Header | Header | Header |
---|---|---|
Table cell | Table cell | Table cell |
Table cell | Table cell | Table cell |