请解释rowspan和colspan,col和colgroup

时间:2010-03-10 11:30:36

标签: css xhtml semantic-markup

任何人都可以解释rowspancolspancolcolgroup吗?这些W3C的有效性和语义是否正确?在哪些情况下这些有用?

3 个答案:

答案 0 :(得分:15)

列跨度

<table border="1">
  <tr>
    <th colspan="2">people are...</th>
  </tr>
  <tr>
    <td>monkeys</td>
    <td>donkeys</td>
  </tr>
</table>

rowspan的

<table border="1">
  <tr>
    <th rowspan="2">monkeys are...</th>
    <td>... real monkeys</td>
  </tr>
  <tr>
    <td>... 'unreal' monkeys (people...)</td>
  </tr>
</table>

W3C

如你所见,这是用于连接表格单元格 - 因为这有时是必要的,它是有效的(RegDwights链接将提供更多信息......)。

COL / COLGROUP

colgroupcol用于为表格中的每一行设置属性(因此您不必为每行中的第一个width="80"tdtr)):

<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>

你也可以对cols进行分组,假设第一列和第二列的with应为80,第三列应为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>

答案 1 :(得分:5)

是的,它们都是W3C推荐的。以下是文档的直接链接:

答案 2 :(得分:1)

rowspancolspan是允许设计者“合并”单元格的属性 - 非常类似于Excel中的相同命令(例如)。

colcolgroup允许设计人员将css应用于垂直列,而不必将css应用于列中的单个单元格。如果没有这些,这个任务将变得更加困难,因为html表是基于行的。

所有这四项都是有效的。

供将来参考,请尝试http://reference.sitepoint.com/html

相关问题