HTML5 之 Col Colgroup 标签

在这里插入图片描述

col

  col 用于定义表格中的列。

  col为表格内单标签元素,只能在table元素或者colgroup元素中使用。

  其属性值主要包括如下几种,均能通过CSS属性实现。

  • span:指定col元素横跨的列数,此属性值为正整数,默认值为1
  • align:指定col元素关联的列的内容的水平对齐方式,包括left(左对齐)、center(居中对齐)、right(右对齐)、char等,注意此属性HTML5已废弃,仅IE7及以下等浏览器可用,绝大多数浏览器已不再支持
  • bgcolor:指定col元素关联的列的背景色,其属性值可指定rgbargbhex和颜色名称,注意此属性为非标准属性,不同浏览器对此属性支持度不一致
  • valign:指定col元素关联的列的内容的垂直对齐方式,包括top(顶端对齐)、middle(居中对齐)、bottom(底部对齐)、baseline(基线对齐),注意此属性HTML5也已废弃,不同浏览器支持程度不一致
  • width:指定col元素关联的列的宽度,值为px宽度或者百分比,注意HTML5已废弃,绝大多数浏览器支持
  • charalign属性设置为char时生效,用于指定某列以某个字符对齐,注意HTML5不再支持此属性,且大部分浏览器不支持
  • charoffalign属性设置为char时生效,规定内容相对于char属性指定的字符的偏移量

span

  span指定横跨的列数,不指定或指定为空默认为1,通过指定col的样式作用于列上。绝大多数浏览器都兼容colspan属性。

<style>
  td {
    width: 240px;
    height: 30px;
  }

  .col-1 {
    background: lightblue;
  }

  .col-23 {
    background: pink;
  }
</style>

<table border="1">
  <col class="col-1">
  <col span="2" class="col-23">
  <tr>
    <th>Index</th>
    <th>Language</th>
    <th>Proportion</th>
  </tr>
  <tr>
    <td>1</td>
    <td>HTML</td>
    <td>20.36%</td>
  </tr>
  <tr>
    <td>2</td>
    <td>CSS</td>
    <td>19.64%</td>
  </tr>
  <tr>
    <td>3</td>
    <td>JavaScript</td>
    <td>160.00%</td>
  </tr>
</table>

  样式呈现如下,col-1指定第一列,col-23指定第二列和第三列。

在这里插入图片描述

  也可通过CSS实现colspan属性。

td:nth-child(1),
th:nth-child(1) {
  background: lightblue;
}

td:nth-child(2),
th:nth-child(2),
td:nth-child(3),
th:nth-child(3) {
  background: pink;
}

  IE8及以下浏览器不支持nth-child,可利用相邻选择器+兼容。IE5不支持相邻选择器则可指定class类名实现。

td,
th {
  background: lightblue;
}

td + td,
td + td + td,
th + th,
th + th + th {
  background: pink;
}

align

  指定col关联的列的对齐方式,默认为左对齐,仅IE7及以下等浏览器支持。

<style>
  td {
    width: 240px;
    height: 30px;
  }
</style>

<table border="1">
  <col align="center">
  <col align="right">
  ...
</table>

  IE7呈现效果如下。

在这里插入图片描述

  也可通过CSS属性实现,其中兼容性方面参考span属性。

td:nth-child(1),
th:nth-child(1) {
  text-align: center;
}

td:nth-child(2),
th:nth-child(2) {
  text-align: right;
}

<table border="1">
  ...
</table>

bgcolor

  非标准属性,浏览器支持度不一,指定col关联的列的背景色。

<table border="1">
  <col bgcolor="lightblue">
  <col bgcolor="#ccc">
  <col bgcolor="rgb(0,0,255,0.9)">
  ...
</table>

  如下为Chrome浏览器呈现样式,也可通过CSS属性实现。

在这里插入图片描述

valign

  指定col关联的列的垂直对齐方式,不同浏览器支持不一。

<style>
  td {
    width: 240px;
    height: 50px;
  }
</style>

<table border="1">
  <col valign="top">
  <col valign="bottom">
  ...
</table>

  如下为IE11浏览器呈现样式。

在这里插入图片描述

  也可通过 CSS属性实现。

td:nth-child(1),
th:nth-child(1) {
  vertical-align: top;
}

td:nth-child(2),
th:nth-child(2) {
  vertical-align: bottom;
}

<table border="1">
  ...
</table>

width

  指定col关联的列的宽度。

<style>
  td {
    height: 30px;
  }
</style>

<table border="1">
  <col width="120px">
  <col width="360px">
  <col width="240px">
  ...
</table>

  浏览器呈现效果如下。

在这里插入图片描述

  也可指定col的样式作用于列上。

.col-1 {
  width: 120px;
}

.col-2 {
  width: 360px;
}

.col-3 {
  width: 240px;
}

<table border="1">
  <col class="col-1">
  <col class="col-2">
  <col class="col-3">
  ...
</table>

  或者也可指定CSS样式,其中兼容性方面也可参考span属性。

td:nth-child(1),
th:nth-child(1) {
  width: 120px;
}

td:nth-child(2),
th:nth-child(2) {
  width: 360px;
}

td:nth-child(3),
th:nth-child(3) {
  width: 240px;
}

char

  指定列以某个字符对齐,需指定alignchar属性值,注意大部分浏览器都不支持。

<style>
  td {
    width: 240px;
    height: 30px;
  }
</style>

<table border="1">
  <col>
  <col>
  <col align="char" char=".">
  ...
</table>

  模拟呈现效果如下。

在这里插入图片描述

charoff

  规定内容相对于char属性指定的字符的偏移量,其中正数为右偏移,负数为左偏移。

<style>
  td {
    width: 240px;
    height: 30px;
  }
</style>

<table border="1">
  <col>
  <col>
  <col align="char" char="." charoff="2">
  ...
</table>

  如下为模拟呈现效果,即以字符.对齐后再往右偏移2个字符。

在这里插入图片描述

colgroup

  colgroup 用于定义表格中的一组列。

  colgroup为单标签元素,且只能在table内。

  不包含col子元素的colgroup,标签colgroup等价于col,且colgroupcol的属性的表现形式完全一致,包括spanalignbgcolorvalignwidthcharcharoff

  含有col子元素的colgroup,只要colgroup内部包含colcolgroupspan属性就会被忽略。并且内部col的样式或属性默认继承至colgroup,若col自身指定了样式或属性,则会覆盖继承的样式或属性。

<style>
  td {
    height: 30px;
  }

  colgroup {
    background: lightblue;
  }

  .col-2 {
    background: pink;
  }
</style>

<table border="1">
  <colgroup span="2" width="180px">
    <col>
    <col class="col-2" width="360px">
    <col>
  </colgroup>
  <tr>
    <th>Index</th>
    <th>Language</th>
    <th>Proportion</th>
  </tr>
  <tr>
    <td>1</td>
    <td>HTML</td>
    <td>20.36%</td>
  </tr>
  <tr>
    <td>2</td>
    <td>CSS</td>
    <td>19.64%</td>
  </tr>
  <tr>
    <td>3</td>
    <td>JavaScript</td>
    <td>160.00%</td>
  </tr>
</table>

  如下colgroupspan="2"属性被忽略,第一列和第三列的样式(background: lightblue)和属性(width="120px")继承至colgroup,而第二列则覆盖了继承的样式和属性。

在这里插入图片描述

🎉 写在最后

🍻伙伴们,如果你已经看到了这里,觉得这篇文章有帮助到你的话不妨点赞👍或 Star ✨支持一下哦!

手动码字,如有错误,欢迎在评论区指正💬~

你的支持就是我更新的最大动力💪~

GitHub / GiteeGitHub Pages掘金CSDN 同步更新,欢迎关注😉~

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
`<colgroup>` 和 `<col>` 标签是用于定义表格列的样式和属性。 `<colgroup>` 标签可用于将一组 `<col>` 标签组合在一起,从而为一组表格列定义共同的样式和属性。 下面是一个 `<colgroup>` 和 `<col>` 的例子,其中 `<colgroup>` 定义了一组表格列的背景颜色和宽度: ```html <table> <colgroup> <col style="background-color: #ccc; width: 50px;"> <col style="background-color: #eee; width: 100px;"> <col style="background-color: #ccc; width: 150px;"> </colgroup> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> </table> ``` 可以看到,`<colgroup>` 标签中包含三个 `<col>` 标签,每个 `<col>` 标签定义了一个表格列的样式和属性。这些 `<col>` 标签的作用是为 `<colgroup>` 标签内的所有表格列定义共同的样式和属性。 `<col>` 标签也可以单独使用,用于为单个表格列定义样式和属性。下面是一个使用单独的 `<col>` 标签的例子,其中 `<col>` 定义了第一列表格的背景颜色和宽度: ```html <table> <col style="background-color: #ccc; width: 50px;"> <tr> <td>Column 1</td> <td>Column 2</td> <td>Column 3</td> </tr> </table> ``` 可以看到,这个例子中只使用了一个 `<col>` 标签,它定义了第一列表格的样式和属性。 总之,`<colgroup>` 和 `<col>` 标签可以用于为表格列定义样式和属性,可以单独使用,也可以组合使用。`<colgroup>` 标签用于将一组 `<col>` 标签组合在一起,为一组表格列定义共同的样式和属性。而 `<col>` 标签则用于为单个表格列定义样式和属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

DonV

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

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

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

打赏作者

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

抵扣说明:

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

余额充值