html table border属性,HTML Table Border

HTML Table Border

This page contains HTML table border code - HTML codes for specifying or changing the border of your tables within your blog or web page.

HTML table borders are specified using Cascading Style Sheets (CSS). To set the border of an HTML table, use the CSS border property.

Typical Table Border

Here's a common way to set borders on a table:

table {

border-collapse: collapse;

}

td, th {

border: 1px solid orange;

}

This provides that "grid" like effect, where the border surrounds each cell as well as the whole table.

Like this:

Run

Stack editor

Unstack editor

table {

border-collapse: collapse;

}

th, td {

border: 1px solid orange;

padding: 10px;

text-align: left;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell

Notice that I used border-collapse: collapse; against the table element. This collapses the border so that you don't see any space between the cells and the outside of the table.

Without Collapsing the Border

Here it is without collapsing the border. I've also applied the border against the table element in order to demonstrate the effect:

Run

Stack editor

Unstack editor

table, th, td {

border: 1px solid orange;

}

th, td {

padding: 10px;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell

You can see that I've also added padding to the th and td selectors but not to the table itself. If we include the padding against the table, we'd end up with extra padding between the outer cells and the outside of the table.

So we'd end up with this:

Run

Stack editor

Unstack editor

table, th, td {

border: 1px solid orange;

padding: 10px;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell

There's nothing wrong with that if that's what you want. However, if you don't want padding between the table and its cells, you'll need to apply the padding to just the cells.

Bottom Border

The above examples use the CSS border property to set the borders. This is a shorthand property to set border width, style, and color on all sides of the table.

If you don't want the border to go all around the table (or if you want a different border on each side of the table), you can use any of the following properties: border-top, border-right, border-bottom, and border-left.

Here's an example of setting the border to only appear at the bottom of each table cell.

Run

Stack editor

Unstack editor

table.bottomBorder {

border-collapse: collapse;

}

table.bottomBorder td,

table.bottomBorder th {

border-bottom: 1px solid yellowgreen;

padding: 10px;

text-align: left;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell

Border and Alternating Background Colors

A common usage of tables is for each row to have alternating background colors.

You can apply borders against these tables just like any other table:

Run

Stack editor

Unstack editor

table {

border-collapse: collapse;

}

th, td {

border: 1px solid #ccc;

padding: 10px;

text-align: left;

}

tr:nth-child(even) {

background-color: #eee;

}

tr:nth-child(odd) {

background-color: #fff;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell

No Border on Table Headers

You can also remove the border from the th element.

You can either remove the border from the styles by using border: none; against the th selector (but it has to follow the border declaration), or just not apply the border in the first place.

Here's an example of the later:

Run

Stack editor

Unstack editor

table {

border-collapse: collapse;

}

td {

border: 1px solid #ccc;

}

th, td {

padding: 10px;

text-align: left;

}

tr:nth-child(even) {

background-color: #eee;

}

tr:nth-child(odd) {

background-color: #fff;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell

Rounded Corners

Here's an example of adding a border with curved/rounded corners to the table. In the CSS3 specification, rounded corners are specified using the border-radius property.

Note that we need to remove the border-collapse property for this work.

I also set the border-spacing property to zero, so that the cell borders continue smoothly without being interrupted by a space. Remove this property and click Run to see what I mean.

Run

Stack editor

Unstack editor

Example

table.roundedCorners {

border: 1px solid DarkOrange;

border-radius: 13px;

border-spacing: 0;

}

table.roundedCorners td,

table.roundedCorners th {

border-bottom: 1px solid DarkOrange;

padding: 10px;

}

table.roundedCorners tr:last-child > td {

border-bottom: none;

}

Table HeaderTable HeaderTable Header
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell
Table cellTable cellTable cell

The Border Properties

CSS provides quite a number of border related properties to assist you in creating borders. These properties can be applied to any HTML element, not just tables.

For a full list of border properties, go to CSS Properties and filter by "border".

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值