【发布时间】:2015-01-31 23:34:24
【问题描述】:
谁能解释rowspan和colspan、col和colgroup?这些 W3C 是否有效且语义正确?这些在什么情况下有用?
【问题讨论】:
标签: css xhtml semantic-markup
谁能解释rowspan和colspan、col和colgroup?这些 W3C 是否有效且语义正确?这些在什么情况下有用?
【问题讨论】:
标签: css xhtml semantic-markup
<table border="1">
<tr>
<th colspan="2">people are...</th>
</tr>
<tr>
<td>monkeys</td>
<td>donkeys</td>
</tr>
</table>
<table border="1">
<tr>
<th rowspan="2">monkeys are...</th>
<td>... real monkeys</td>
</tr>
<tr>
<td>... 'unreal' monkeys (people...)</td>
</tr>
</table>
如您所见,这是用于连接表格单元格 - 因为有时这是必要的,所以它是有效的(RegDwights 链接将提供更多信息......)。
colgroup 和col 用于为表中的每一行设置属性(因此您不必为每一行中的第一个td 写width="80"(tr)):
<table border="1">
<colgroup>
<col width="80">
<col width="100">
<col width="320">
</colgroup>
<tr>
<td>first line, first column</td>
<td>first line, second column</td>
<td>first line, third column</td>
</tr>
<!-- more table-lines... -->
</table>
您还可以对列进行分组,假设第一列和第二列应该得到 80 的 with,第三列应该得到 320:
<table border="1">
<colgroup width="80" span="2"></colgroup>
<colgroup width="320" span="1"></colgroup>
<tr>
<td>first line, first column</td>
<td>first line, second column</td>
<td>first line, third column</td>
</tr>
<!-- more table-lines... -->
</table>
【讨论】:
rowspan 和 colspan 是允许设计者“合并”单元格的属性 - 很像 Excel 中的相同命令(例如)。
col 和 colgroup 允许设计人员将 css 应用于垂直列,而不必将 css 应用于列中的单个单元格。如果没有这些,这项任务将更加困难,因为 html 表格是基于行的。
这四个都是有效的。
为了将来参考,请尝试http://reference.sitepoint.com/html
【讨论】: