试一试
| Items | Expenditure |
|---|---|
| Donuts | 3,000 |
| Stationery | 18,000 |
| Totals | 21,000 |
thead,
tfoot {
background-color: #2c5e77;
color: white;
}
tbody {
background-color: #e4f0f5;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
caption {
caption-side: bottom;
padding: 10px;
}
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
td {
text-align: center;
}
属性
此元素包含全局属性。
已弃用属性
以下属性已弃用,不应使用。它们仅在更新现有代码时作为参考和出于历史兴趣而在此处记录。
align 已弃用
指定每个表格主体单元格的水平对齐方式。可能的枚举值为 left、center、right、justify 和 char。当支持时,char 值将文本内容与 char 属性中定义的字符对齐,并与 charoff 属性定义的偏移量对齐。请改用 text-align CSS 属性,因为此属性已废弃。
bgcolor 已弃用
定义每个表格主体单元格的背景颜色。该值是 HTML 颜色;可以是带 # 前缀的6 位十六进制 RGB 代码,也可以是颜色关键字。不支持其他 CSS
char 已弃用
指定每个表格主体单元格内容与字符的对齐方式。通常用于对齐数字或货币值,例如句点(.)。如果 align 未设置为 char,则此属性将被忽略。
charoff 已弃用
指定表格主体单元格内容相对于 char 属性指定的对齐字符的偏移字符数。
valign 已弃用
指定每个表格主体单元格的垂直对齐方式。可能的枚举值为 baseline、bottom、middle 和 top。请改用 vertical-align CSS 属性,因为此属性已废弃。
用法说明
放置在任何如果
| 元素)。
html
CSS 请注意示例中的 CSS,其中为 | 元素)。 | 元素表示表格的主体部分,其中包含多行(|||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Student ID | Name | Major | Credits |
|---|---|---|---|
| 3741255 | Jones, Martha | Computer Science | 240 |
| 3971244 | Nim, Victor | Russian Literature | 220 |
| 4100332 | Petrov, Alexandra | Astrophysics | 260 |
CSS
CSS 与上一个示例几乎没有变化,除了添加了一些基本样式来突出显示表格头,以便列标题在表格主体数据中脱颖而出。与上面的示例一样,tbody 类型选择器用于设置主体单元格的样式。
csstbody {
background-color: #e4f0f5;
}
tbody > tr > td:last-of-type {
text-align: center;
}
thead {
border-bottom: 2px solid rgb(160 160 160);
background-color: #2c5e77;
color: white;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
th,
td {
border: 1px solid rgb(160 160 160);
padding: 8px 10px;
}
结果
多个主体
此示例通过引入多个主体部分,进一步扩展和增强了上面的示例中的表格。
使用多个
元素可以在表格中创建行分组。每个表格主体都可以有自己的标题行;但是,每个表格只能有一个 元素!因此,带有HTML
在上一个基本示例的表格基础上,添加了更多学生,并且不再在每一行中列出每个学生的专业,而是按专业对学生进行分组。请注意,每个专业都包含在自己的
块中,第一行(html
| Student ID | Name | Credits |
|---|---|---|
| Computer Science | ||
| 3741255 | Jones, Martha | 240 |
| 4077830 | Pierce, Benjamin | 200 |
| 5151701 | Kirk, James | 230 |
| Russian Literature | ||
| 3971244 | Nim, Victor | 220 |
| Astrophysics | ||
| 4100332 | Petrov, Alexandra | 260 |
| 8892377 | Toyota, Hiroko | 240 |
CSS
大多数 CSS 没有变化。然而,为直接包含在
中的标题单元格(与 中的标题单元格相对)添加了一种稍微更微妙的样式。这用于指示每个表格部分对应专业的标题。csstbody > tr > th {
border-top: 2px solid rgb(160 160 160);
border-bottom: 1px solid rgb(140 140 140);
background-color: #e4f0f5;
font-weight: normal;
}
tbody {
background-color: whitesmoke;
}
thead {
background-color: #2c5e77;
color: white;
}
table {
border-collapse: collapse;
border: 2px solid rgb(140 140 140);
font-family: sans-serif;
font-size: 0.8rem;
letter-spacing: 1px;
}
th,
td {
border: 1px solid rgb(160 160 160);
padding: 6px 8px;
text-align: left;
}
tbody > tr > td:last-of-type {
text-align: center;
}
结果
技术摘要
内容类别
无。
允许内容
零个或多个
标签省略
如果
允许父级
在必需的父
| 、 | 、、、 background-color:设置每个表格主体单元格背景颜色的 CSS 属性 border:控制表格主体单元格边框的 CSS 属性 text-align:水平对齐每个表格主体单元格内容的 CSS 属性 vertical-align:垂直对齐每个表格主体单元格内容的 CSS 属性 帮助改进 MDN 此页面对您有帮助吗? 是 否 了解如何贡献 本页面最后由MDN 贡献者于2025 年 8 月 6 日修改。 在 GitHub 上查看此页面 • 报告此内容问题 |
|---|