目录
1. Basic Structure of a Table with thead, tbody, and tfoot
2. Why Use thead, tbody, and tfoot?
Improved Accessibility
Better Styling with CSS
Print-Friendly Tables
JavaScript & DOM Manipulation
3. Rules and Best Practices
首页 web前端 html教程 如何使用thead,tbody和tfoot构建HTML表

如何使用thead,tbody和tfoot构建HTML表

Aug 05, 2025 pm 07:56 PM

使用<thead>、

<tbody>和 <tfoot>能提升表格的可读性、可访问性和可维护性;1. 表格应包含<thead>用于表头、 <tbody>用于主体数据、 <tfoot>用于汇总信息,且<tfoot>在代码中应位于 <tbody>之前以确保正确渲染;2. 这些元素有助于屏幕阅读器解析、支持CSS样式控制、实现打印时表头重复以及简化JavaScript操作;3. 每个表格只能有一个<thead>和一个 <tfoot>,但可包含多个 <tbody>以分组数据,应始终显式使用<th>定义表头并避免省略 <tbody>,从而增强语义化和专业性。

How to use thead, tbody, and tfoot to structure an HTML table

Using <thead>, <code><tbody>, and <code><tfoot> properly helps structure an HTML table for better readability, accessibility, and styling. These elements divide a table into logical sections: the header, body, and footer. Here's how to use them effectively.<img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/175439496521131.jpeg" class="lazy" alt="How to use thead, tbody, and tfoot to structure an HTML table"><hr> <h3 id="strong-Basic-Structure-of-a-Table-with-thead-tbody-and-tfoot-strong"><strong>1. Basic Structure of a Table with thead, tbody, and tfoot</strong></h3> <p>Always wrap your table content in <code><table>, and use the following elements to define its sections:<ul><li><code><thead>: Contains the header row(s), typically with column labels.<li><code><tbody>: Contains the main data rows.<li><code><tfoot>: Contains summary or totals row(s), usually at the bottom.<pre class='brush:php;toolbar:false;'>&lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;Name&lt;/th&gt; &lt;th&gt;Age&lt;/th&gt; &lt;th&gt;Salary&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tfoot&gt; &lt;tr&gt; &lt;td&gt;Total&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;td&gt;$120,000&lt;/td&gt; &lt;/tr&gt; &lt;/tfoot&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;John Doe&lt;/td&gt; &lt;td&gt;30&lt;/td&gt; &lt;td&gt;$70,000&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Jane Smith&lt;/td&gt; &lt;td&gt;28&lt;/td&gt; &lt;td&gt;$50,000&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt;</pre><blockquote><p>? <strong>Note:</strong> Even though <code><tfoot> appears before <tbody> in the code, browsers will render it at the bottom of the table automatically.

How to use thead, tbody, and tfoot to structure an HTML table

2. Why Use thead, tbody, and tfoot?

Improved Accessibility

Screen readers can better interpret table structure when these elements are used. Users can navigate headers and footers separately from data.

Better Styling with CSS

You can target specific sections easily:

How to use thead, tbody, and tfoot to structure an HTML table
thead th {
  background-color: #003366;
  color: white;
}

tbody tr:nth-child(even) {
  background-color: #f2f2f2;
}

tfoot td {
  font-weight: bold;
  background-color: #ddd;
}

Print-Friendly Tables

When a table spans multiple printed pages, browsers can repeat the <thead> on each page automatically.

JavaScript & DOM Manipulation

Easier to select and manipulate data:

// Example: Add a new row to the table body
const tableBody = document.querySelector('tbody');
const newRow = `<tr><td>New Person</td><td>35</td><td>$60,000</td></tr>`;
tableBody.insertAdjacentHTML('beforeend', newRow);

3. Rules and Best Practices

  • Only one <thead> and one <tfoot> per table, but multiple <tbody> sections if needed (e.g., for grouping data).
  • Place <tfoot> before <tbody> in the HTML to ensure proper rendering.
  • Use <th> in <thead> for column headers (not just <td>).
  • Don’t skip <tbody>—browsers often add it automatically, but it’s better to include it explicitly for clarity.

Example with multiple <tbody> sections:

<table>
  <thead>
    <tr><th>Department</th><th>Employee</th><th>Salary</th></tr>
  </thead>
  <tbody>
    <tr><td>Engineering</td><td>Alice</td><td>$90,000</td></tr>
    <tr><td>Engineering</td><td>Bob</td><td>$85,000</td></tr>
  </tbody>
  <tbody>
    <tr><td>Marketing</td><td>Carol</td><td>$60,000</td></tr>
    <tr><td>Marketing</td><td>Dave</td><td>$55,000</td></tr>
  </tbody>
</table>

This allows styling or scripting each department group separately.


Basically, using <thead>, <tbody>, and <tfoot> isn’t required for a table to work—but it makes your tables more semantic, maintainable, and professional. Don’t skip them in real projects.

以上是如何使用thead,tbody和tfoot构建HTML表的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Laravel 教程
1602
29
PHP教程
1504
276
在HTML中实现图像的本机懒负荷 在HTML中实现图像的本机懒负荷 Jul 12, 2025 am 12:48 AM

原生懒加载是一种浏览器内置功能,通过在标签中添加loading="lazy"属性实现延迟加载图片。1.它无需JavaScript或第三方库,直接在HTML中使用;2.适合用于页面下方非首屏显示的图片、图片画廊滚动加载项和大型图片资源;3.不适合首屏图片或display:none的图片;4.使用时应设置合适的占位空间以避免布局抖动;5.应结合srcset和sizes属性优化响应式图片加载;6.需要考虑兼容性问题,部分旧浏览器不支持,可通过特性检测并结合JavaScript方案作

使用HTML SRCSET和大小属性实现响应式图像 使用HTML SRCSET和大小属性实现响应式图像 Jul 12, 2025 am 12:15 AM

srcset和sizes是HTML实现响应式图片的关键属性。srcset提供多个图片源及其宽度或像素密度,如400w、800w,浏览器据此选择合适图片;sizes则定义图片在不同屏幕宽度下的显示宽度,如(max-width:600px)100vw,50vw,使浏览器更精准匹配图片尺寸。实际使用中需准备多尺寸图片、命名清晰、配合媒体查询设计布局,并测试设备表现,避免忽略sizes或单位错误,从而节省带宽并提升性能。

使用HTML A标签创建用于导航的超链接 使用HTML A标签创建用于导航的超链接 Jul 11, 2025 am 03:03 AM

使用HTML的标签可通过href属性实现页面跳转、新窗口打开、页面内定位及邮件电话链接功能。1.基本用法:通过href指定目标地址,如访问网页;2.新窗口打开:添加target="_blank"和rel="noopener"属性;3.页面内跳转:结合id与#符号实现锚点定位;4.邮件电话链接:使用mailto:或tel:协议触发系统应用。

HTML TextArea和输入类型文本有哪些差异和用例? HTML TextArea和输入类型文本有哪些差异和用例? Jul 12, 2025 am 02:48 AM

与的主要区别在于,textarea支持多行文本输入,而inputtext仅限单行。1.使用inputtype="text"适用于短小、单行的用户输入,如用户名、邮箱等,可设置maxlength限制字符数,浏览器提供自动填充功能,更易跨浏览器统一样式;2.使用textarea用于需要多行输入的场景,如评论框、反馈表单,支持换行和段落,可通过CSS控制大小或禁用调整功能。两者均支持占位符、必填等表单特性,但textarea通过rows和cols定义尺寸,input则使用size属

'`vs.` `在html中 '`vs.` `在html中 Jul 19, 2025 am 12:41 AM

是块级元素,用于划分大块内容区域;是内联元素,适合包裹小段文字或内容片段。具体区别如下:1.独占一行,可设置宽高、内外边距,常用于布局结构如头部、侧边栏等;2.不换行,仅占据内容宽度,用于局部样式控制如变色、加粗等;3.使用场景上,适用于整体区域的排版与结构组织,而用于不影响整体布局的小范围样式调整;4.嵌套时,可包含任何元素,而内部不应嵌套块级元素。

如何使用HTML,CSS和JS创建简单的弹出式或模态? 如何使用HTML,CSS和JS创建简单的弹出式或模态? Jul 12, 2025 am 02:42 AM

要实现一个基础的弹窗效果,需按照以下步骤操作:1.结构:用HTML创建触发按钮、遮罩层和弹窗内容区域;2.样式:通过CSS设置默认隐藏、居中布局、遮罩背景及关闭按钮样式;3.交互:使用JavaScript绑定点击事件控制弹窗显示与隐藏,并可扩展ESC键关闭功能;4.优化:添加CSS动画提升用户体验。整个过程无需第三方库,适合快速实现基本弹窗功能。

使用HTML模板标签使用可重复使用的内容片段 使用HTML模板标签使用可重复使用的内容片段 Jul 11, 2025 am 03:19 AM

标签是HTML5引入的一个用于定义可重用内容片段的标签,它不会立即渲染,但可通过JavaScript动态插入页面。其使用流程包括:1.定义模板;2.克隆内容;3.插入DOM。例如通过document.getElementById获取模板,调用cloneNode(true)克隆并插入页面。结合数据动态填充时,可通过操作克隆后的DOM元素实现内容绑定,适用于构建商品列表、用户卡片等组件。使用时需注意不能直接访问模板子元素、避免ID冲突、处理样式作用域,并可配合WebComponents创建封装组件。

初学者的基本HTML标签 初学者的基本HTML标签 Jul 27, 2025 am 03:45 AM

要快速入门HTML,只需掌握几个基础标签即可搭建网页骨架。1.页面结构必备、和,其中是根元素,包含元信息,是内容展示区域。2.标题使用到,级别越高数字越小,正文用标签分段,避免跳级使用。3.链接使用标签并配合href属性,图片使用标签并包含src和alt属性。4.列表分为无序列表和有序列表,每个条目用表示且必须嵌套在列表中。5.初学者不必强记所有标签,边写边查更高效,掌握结构、文本、链接、图片和列表即可制作基础网页。

See all articles