HTML 表格元素








学习要点

  1. 表格元素汇总
  2. 构建表格解析

一、表格元素汇总

表格的基本构成最少需要三个元素:< table >、< tr >、< td >,其他的一些作为可选铺助存在。

元素名称说明
table表示表格
thead表示标题行
tbody表示表格主体
tfoot表示表脚
tr表示一行单元格
th表示标题行单元格
td表示单元格
col表示一列
colgroup表示一组列
caption表示表格标题

二、构建表格解析


1、table、tr、td构建基础表格

张三未婚
李四已婚
<table border="1">
    <tr>
        <td>张三</td>
        <td></td>
        <td>未婚</td>
    </tr>
    <tr>
        <td>李四</td>
        <td></td>
        <td>已婚</td>
    </tr>
</table>

解释:< table > 元素表示一个表格的声明,< tr >元素表示表格的一行,< td > 元素表示行数据。

2、< th > 为表格添加标题单元格

姓名性别婚姻
张三未婚
李四已婚
<table border="1">
    <tr>
        <th>姓名</th>
        <th>性别</th>
        <th>婚姻</th>
    </tr>
    <tr>
        <td>张三</td>
        <td></td>
        <td>未婚</td>
    </tr>
    <tr>
        <td>李四</td>
        <td></td>
        <td>已婚</td>
    </tr>
</table>

姓名性别婚姻
张三未婚
李四已婚
<table border="1" style="width:300px;">
    <tr>
        <th>姓名</th>
        <th>性别</th>
        <th>婚姻</th>
    </tr>
    <tr>
        <td>张三</td>
        <td></td>
        <td>未婚</td>
    </tr>
    <tr>
        <td>李四</td>
        <td></td>
        <td>已婚</td>
    </tr>
</table>

姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px">
    <tr>
        <th>姓名</th>
        <th>性别</th>
        <th>婚姻</th>
    </tr>
    <tr>
        <td>张三</td>
        <td></td>
        <td>未婚</td>
    </tr>
    <tr>
        <td>李四</td>
        <td></td>
        <td>已婚</td>
    </tr>
    <tr>
        <td colspan="3">统计:共两人</td>
    </tr>
</table>

基本情况姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px">
    <tr>
        <th rowspan="4">基本情况</th>
        <th>姓名</th>
        <th>性别</th>
        <th>婚姻</th>
    </tr>
    <tr>
        <td>张三</td>
        <td></td>
        <td>未婚</td>
    </tr>
    <tr>
        <td>李四</td>
        <td></td>
        <td>已婚</td>
    </tr>
    <tr>
        <td colspan="3">统计:共两人</td>
    </tr>
</table>

解释:< th > 元素主要是添加标题行的单元格,实际作用就是将内部文字居中且加粗。这里使用一个通用属性style,主要用于CSS样式设置,以后会涉及到。< th > < td > 均属于单元格,包行两个合并属性:colspan(横向合并)、rowspan(纵向合并)等。

3、< thead > 添加表头

姓名性别婚姻
<table border="1">
    <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
</table>

解释:< thead > 元素就是限制和规范了表格的表头部分。尤其是以后动态生成表头,它的位置是不固定的,使用此元素可以限定在开头位置。

4、< tfoot > 添加表表脚

姓名性别婚姻
统计:共两人
<table border="1">
    <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
    <tfoot>
        <tr>
            <td colspan="3">统计:共两人</td>
        </tr>
    </tfoot>
</table>

解释:< tfoot > 元素为表格生成表脚,限制在表格的底部。

5、< tbody > 添加表主体

姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px;">
    <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>张三</td>
            <td></td>
            <td>未婚</td>
        </tr>
        <tr>
            <td>李四</td>
            <td></td>
            <td>已婚</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3">统计:共两人</td>
        </tr>
    </tfoot>
</table>

解释:< tbody > 元素主要是包含住非表头表脚的主体部分,有助于表格格式的清晰,更加有助于后续CSS和JavaScript的控制。

6、 < caption > 添加表格标题

这个是表格信息
姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px;">
    <caption>这个是表格信息</caption>
    <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>张三</td>
            <td></td>
            <td>未婚</td>
        </tr>
        <tr>
            <td>李四</td>
            <td></td>
            <td>已婚</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3">统计:共两人</td>
        </tr>
    </tfoot>
</table>

解释:< caption > 元素给表格添加一个标题。

7、设置行和< colgroup > 设置列

设置行
姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px;">
    <caption style="color:red;">设置行</caption>
    <thead>
        <tr style="background: red;">
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>张三</td>
            <td></td>
            <td>未婚</td>
        </tr>
        <tr>
            <td>李四</td>
            <td></td>
            <td>已婚</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <td colspan="3">统计:共两人</td>
        </tr>
    </tfoot>
</table>

colgroup设置列
姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px;">
    <caption style="color:red;">colgroup设置列</caption>
    <colgroup style="background: red;" span="1"></colgroup>
    <colgroup style="background: yellow;" span="1"></colgroup>
    <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>张三</td>
            <td></td>
            <td>未婚</td>
        </tr>
        <tr>
            <td>李四</td>             
            <td></td>
            <td>已婚</td>
        </tr>
    </tbody>
    <tfoot>
        <tr style="background: balck;">
            <td colspan="3">统计:共两人</td>
        </tr>
    </tfoot>
</table>

解释:< colgroup > 元素是为了处理某个列,span属性定义处理哪些列。1表示第一列,2表示前两列。如果要单独设置第二列,那么需要声明两个,先处理第一个,将列点移入第二位,再处理第二个即可。

8、< col > 更灵活的设置列

colgroup设置列
姓名性别婚姻
张三未婚
李四已婚
统计:共两人
<table border="1" style="width:300px;">
    <caption style="color:red;">colgroup设置列</caption>
    <colgroup>
        <col style="background:red;" span="1">
        <col>
        <col style="background:yellow;" span="1">
    </colgroup>
    <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>婚姻</th>
        </tr>
    </thead>
        <tbody>
        <tr>
            <td>张三</td>
            <td></td>
            <td>未婚</td>
        </tr>
        <tr>
            <td>李四</td>
            <td></td>
            <td>已婚</td>
        </tr>
    </tbody>
    <tfoot>
        <tr style="background: balck;">
            <td colspan="3">统计:共两人</td>
        </tr>
    </tfoot>
</table>

解释:< col > 元素表示单独一列,一个表示一列,控制更加灵活。如果设置了span,则控制多列。


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiaobukuang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值