html table background,HTML Table Background

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;

}

HeaderHeader
Table cellTable cell
Table cellTable cell
Table cellTable 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;

}

HeaderHeader
Table cellTable cell
Table cellTable cell
Table cellTable 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:

8616b88d268cb28e319b52f1501f910d.gif

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;

}

HeaderHeader
Table cellTable cell
Table cellTable cell
Table cellTable 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;

}

HeaderHeader
Table cellTable cell
Table cellTable cell
Table cellTable 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;

}

HeaderHeaderHeader
Table cellTable cellTable cell
Table cellTable cellTable cell
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值