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".

### 回答1: HTMLtable 元素的 border 属性用于设置表格边框的宽度。其可能的值为:数字(如:1) 或 "thin"、"medium"、"thick"。默认值为 "medium"。 例:<table border="1"> 这将在表格周围设置1个像素的边框 ### 回答2: HTML是一种标记语言,用于在Web浏览器中呈现网站。 在HTML中,表格元素是一种非常常用的元素类型,并且在表格中使用“border属性是非常常见的。 “border属性用于指定表格和表格单元格的边框,可以设置为一个值或一个CSS样式。 如果设置为一个值,该值将被应用于表格和单元格的所有四个边框。 如果您希望为不同的边框指定不同的值,则可以使用CSS样式表来设置。 例如,以下是一个简单的HTML表格,其中“border属性设置为“1”: <table border="1"> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> <tr> <td>Cell 3</td> <td>Cell 4</td> </tr> </table> 在这个例子中,所有的单元格和表格都有一个像素的边框。 如果您想为每个单元格指定不同的边框,您可以将“border属性设置为“0”,然后使用CSS来设置每个单元格的边框。例如: <style> table { border-collapse: collapse; } td { border: 1px solid black; } </style> <table border="0"> <tr> <td>Cell 1</td> <td>Cell 2</td> </tr> <tr> <td>Cell 3</td> <td>Cell 4</td> </tr> </table> 在这个例子中,单元格的边框是黑色实线。 总体而言,“border属性HTML表格中一个非常实用的属性,可以用来控制边框的外观、大小和颜色。使用“border属性可以让HTML表格更具可读性和易于浏览。 ### 回答3: HTML中的table元素是用来定义表格的标签,而border属性则用于控制表格边框的显示效果。在最初的版本中,HTML中的table元素并没有border属性,表格的边框只能通过CSS来控制。但随着HTML的不断发展,border属性被添加到了table元素中,从而使得开发者在定义表格时更加方便。 border属性有三种取值,分别是0、1和大于等于2的整数。当border取值为0时,表格边框不显示;当border取值为1时,表格边框显示,但仅限于表格外边框;当border取值大于等于2时,表格内外的边框都会显示。此时,边框的大小由值决定。 除了border属性,还有一些其他的属性可以用来控制表格边框的显示效果,例如cellspacing、cellpadding、frame和rules等。其中,cellspacing和cellpadding属性用来控制单元格之间的间隔和单元格内容与单元格边框之间的距离,frame属性用来控制表格的外观,包括显示表格边框的位置,还可以控制表格边框的样式。rules属性用来控制单元格的边框是否显示,可取值为all、rows、cols和none。 总之,HTML中的table元素和border属性是控制表格边框显示的重要标签和属性,可以用来定义各种样式的表格,从而让网页更加美观、易读、易用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值