hand
_1_12_94
4
返回栏目
0k
3k
5k
1k
2k
0.2k
2k
1k
2k
3k
2k
3k
2k
3k
3k
0.3k
0k
2k
0k
1k
0.1k
0k
0k
2k
2k
3k
0.2k
3k
0k
2k
2k
2k
3k
2k
2k
0k
4k
2k
2k
0k
3k
3k
2k
2k
2k
1k
3k
1k
3k
2k
1k
0.8k
2k
0k
2k
2k
2k
2k
3k
0.4k
4k
2k
5k
2k
3k
2k
3k
3k
4k
2k
3k
2k
3k
0.7k
2k
0.8k
3k
2k
4k
2k
2k
2k
2k
2k
3k
3k
3k
3k
4k
3k
3k
0k
2k
2k
0k
3k
2k
3k
1k
2k
2k
3k
3k
3k
3k
5k
3k
3k
3k
4k
3k
5k
4k
4k
4k
4k
1k
2k
2k
2k
2k
2k
2k
1k
2k
3k
3k
3k
3k
3k
2k
3k
4k
2k
2k
3k
5k
3k
3k
3k
4k
3k
3k
2k
3k
5k
4k
3k
4k
4k
2k
3k
3k
1k
3k
4k
4k
2k
2k
2k
3k
2k
4k
2k
4k
2k
4k
1k
2k
1k
2k
2k
1k
2k
2k
2k
2k
2k
2k
1k
1k
4k
3k
2k
2k
3k
3k
6k
2k
8k
3k
7k
2k
3k
3k
4k
3k
5k
4k
3k
3k
2k
2k
3k
3k
2k
2k
2k
3k
2k
6k
4k
4k
4k
4k
3k
3k
2k
4k
2k
3k
3k
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
None
0k
返回前端 - HTML5栏目
作者:
贺及楼
成为作者
更新日期:2025-02-27 12:07:09
在 HTML5 中,表格是展示数据的常用方式之一。<colgroup>
标签作为一个强大的工具,能够帮助我们更高效地对表格中的列进行样式设置和管理。本文将详细介绍 <colgroup>
标签的使用方法和应用场景,并通过示例代码进行演示。
<colgroup>
标签概述<colgroup>
标签用于对表格中的列进行分组,通过该标签可以为一组列统一设置样式和属性。它通常与 <col>
标签配合使用,<col>
标签用于定义每一列的具体属性。
<table>
<colgroup>
<!-- 可以包含一个或多个 <col> 标签 -->
<col>
<col>
</colgroup>
<thead>
<tr>
<th>列标题 1</th>
<th>列标题 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>数据 1</td>
<td>数据 2</td>
</tr>
</tbody>
</table>
<colgroup>
标签的常见属性属性 | 描述 |
---|---|
span | 指定 <colgroup> 或 <col> 标签应该横跨的列数。默认值为 1。 |
<colgroup>
标签的样式应用示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Colgroup 示例 - 整列组背景颜色</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
colgroup col {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<table>
<colgroup>
<col span="2">
</colgroup>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>城市</th>
</tr>
</thead>
<tbody>
<tr>
<td>张三</td>
<td>25</td>
<td>北京</td>
</tr>
<tr>
<td>李四</td>
<td>30</td>
<td>上海</td>
</tr>
</tbody>
</table>
</body>
</html>
在这个示例中,<colgroup>
标签包含一个 <col>
标签,span="2"
表示该列组横跨两列。通过 CSS 选择器 colgroup col
为这两列设置了背景颜色。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Colgroup 示例 - 不同列不同样式</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th,
td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
colgroup col:nth-child(1) {
background-color: #ffcccc;
}
colgroup col:nth-child(2) {
background-color: #ccffcc;
}
colgroup col:nth-child(3) {
background-color: #ccccff;
}
</style>
</head>
<body>
<table>
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead>
<tr>
<th>学科</th>
<th>成绩</th>
<th>等级</th>
</tr>
</thead>
<tbody>
<tr>
<td>数学</td>
<td>90</td>
<td>A</td>
</tr>
<tr>
<td>语文</td>
<td>85</td>
<td>B</td>
</tr>
</tbody>
</table>
</body>
</html>
在这个示例中,<colgroup>
标签包含三个 <col>
标签,分别代表表格的三列。通过 CSS 的 nth-child
选择器为每一列设置了不同的背景颜色。
<colgroup>
标签的注意事项<colgroup>
标签必须位于 <table>
标签内部,且通常在 <thead>
、<tbody>
等标签之前。<colgroup>
标签只能设置列的样式,如背景颜色、边框等,不能设置单元格内文本的样式,如字体大小、颜色等。<colgroup>
标签是 HTML5 中一个非常实用的表格元素,它可以帮助我们更方便地对表格中的列进行样式设置和管理。通过合理使用 <colgroup>
标签和 <col>
标签,我们可以为表格创建出更加美观和易于阅读的样式。希望本文的介绍和示例能够帮助你更好地掌握 <colgroup>
标签的使用方法。
前端 - HTML5
整章节共240节
快分享给你的小伙伴吧 ~