HTML <th> 标签介绍
作者: 字符空间 发布时间: 2025-12-03 阅读: 53
<th> 标签是HTML中用于定义表格表头单元格的标签,它必须包含在 <tr> 标签内,用于标识表格列或行的标题。
基本语法
<table>
<tr>
<th>表头1</th>
<th>表头2</th>
</tr>
<tr>
<td>数据1</td>
<td>数据2</td>
</tr>
</table>
标签特性
| 特性 | 说明 |
|---|
| 父标签 | <tr> |
| 内容类型 | 流内容(通常为文本) |
| 必需属性 | 无 |
| 可选属性 | colspan, rowspan, scope, headers, abbr等 |
| 默认样式 | 粗体、居中显示 |
使用实例
1. 基本表格表头
2. 行表头和列表头组合
th 标签属性(HTML4,现在建议使用CSS)
| 属性 | 值 | 说明 |
|---|
| colspan | 数字 | 表头单元格横跨的列数 |
| rowspan | 数字 | 表头单元格横跨的行数 |
| scope | col, row, colgroup, rowgroup | 定义表头单元格的作用范围 |
| headers | ID列表 | 关联的其他表头单元格 |
| abbr | 文本 | 表头内容的缩写 |
| align | left, center, right, justify | 水平对齐方式 |
| valign | top, middle, bottom, baseline | 垂直对齐方式 |
| bgcolor | 颜色值 | 背景颜色 |
行列合并实例
列合并表头(colspan)
行合并表头(rowspan)
| 2024年 | 第一季度 | ¥100,000 |
|---|
| 第二季度 | ¥120,000 |
|---|
| 第三季度 | ¥150,000 |
|---|
行列同时合并
可访问性属性详解
scope 属性
| 值 | 说明 | 示例 |
|---|
| col | 表头用于列 | scope="col" |
| row | 表头用于行 | scope="row" |
| colgroup | 表头用于列组 | scope="colgroup" |
| rowgroup | 表头用于行组 | scope="rowgroup" |
可访问性示例
| 产品名称 | 价格 | 库存 | 销量 |
|---|
| 笔记本电脑 | ¥5,999 | 50 | 120 |
|---|
| 智能手机 | ¥3,299 | 100 | 200 |
|---|
复杂表格结构实例
| 部门 | 2024年季度销售额(万元) | 年度总计 |
|---|
| Q1 | Q2 | Q3 | Q4 |
|---|
| 销售部 | 150 | 180 | 200 | 220 | 750 |
|---|
| 市场部 | 80 | 90 | 100 | 110 | 380 |
|---|
thead、tbody、tfoot 中的 th
| 员工编号 | 姓名 | 出勤天数 | 迟到次数 |
|---|
| 001 | 张三 | 22 | 1 |
| 002 | 李四 | 21 | 0 |
| 部门平均 | 21.5 | 0.5 |
|---|
th 与 td 的区别
| 比较项 | <th> | <td> |
|---|
| 语义 | 表格标题/表头 | 表格数据 |
| 默认样式 | 粗体、居中 | 正常字体、左对齐 |
| 可访问性 | 建议使用scope属性 | 通常不需要scope |
| 屏幕阅读器 | 读出为"标题" | 读出为"数据" |
abbr 属性应用
| 所在城市名称 | 常住人口数量(万人) | 地区生产总值(亿元) |
|---|
| 北京市 | 2184 | 40269 |
JavaScript 操作 th 示例
// 获取所有th元素
var headers = document.querySelectorAll("th");
// 为th添加排序功能
headers.forEach(function(header, index) {
header.addEventListener("click", function() {
sortTable(index);
});
header.style.cursor = "pointer";
});
// 排序函数示例
function sortTable(columnIndex) {
var table = document.getElementById("dataTable");
// 排序逻辑...
}
常见错误用法
| 错误示例 | 问题 | 正确写法 |
|---|
| <th></th> | 空表头单元格(无障碍问题) | <th> </th> 或添加描述 |
| <table><th>...</th></table> | th不在tr内 | <table><tr><th>...</th></tr></table> |
| 过度使用th | 所有单元格都用th,失去语义 | 只在表头位置使用th |
CSS样式示例(仅作展示)
/* 基本表头样式 */
th {
padding: 12px 15px;
border: 1px solid #ddd;
background-color: #4CAF50;
color: white;
font-weight: bold;
text-align: left;
}
/* 斑马纹表格的表头 */
thead th {
position: sticky;
top: 0;
z-index: 10;
}
/* 悬停效果 */
th:hover {
background-color: #45a049;
}
/* 响应式表格 */
@media screen and (max-width: 600px) {
th {
display: block;
width: 100%;
}
}
最佳实践
- 为每个表格使用适当的表头(th)
- 使用scope属性提高可访问性
- 为复杂的表格使用headers属性
- 避免空的th单元格,或使用 填充
- 在thead中使用th定义列标题
- 在tbody中使用th定义行标题(如果需要)
- 使用abbr属性为长表头提供简短描述
响应式表格设计
<table>
<thead>
<tr>
<th>产品名称</th>
<th>价格</th>
<th>库存</th>
<th class="mobile-hide">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td data-label="产品名称">笔记本电脑</td>
<td data-label="价格">¥5,999</td>
<td data-label="库存">50</td>
<td class="mobile-hide" data-label="描述">高性能商务本</td>
</tr>
</tbody>
</table>
<th> 标签是创建语义化、可访问性良好的表格的关键元素。正确使用th不仅能够改善表格的外观,还能大大提高屏幕阅读器用户的使用体验。
❤️收藏
👍点赞
用户评论
发表评论