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.

Example:

CodeResult

Table headerTable header
Table cell 1Table cell 2
Table cell 3Table cell 4
Table cell 5Table cell 6

Background Color

The following example demonstrates how background color can be applied to the table, a table row, and also an individual table cell by using the background-color property. Check out HTML Table Background Color to see more examples of adding background color to a table. In particular, it shows how to use a CSS class to define your table styles.

CodeResult

Table headerTable header
Table cell 1Table cell 2

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:

Background Image

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.

CodeResult

Table headerTable header
Table cell 1Table cell 2

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.

CodeResult

Table headerTable header
Table cell 1Table cell 2
Table cell 3Table cell 4
Table cell 5Table cell 6

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:

CodeResult

Table headerTable header
Table cell 1Table cell 2
Table cell 3Table cell 4
Table cell 5Table cell 6

Using Classes

Some of the above examples use inline style sheets and the others use embedded style sheets to set the CSS properties. This is because it's easier for demonstration purposes. I strongly encourage you to use a CSS class defined in an external style sheet whenever possible to set your styles. This can reduce coding time and make your code easier to maintain.

Enjoy this page?