2

我正在用 HTML5 编写一个带有表头和表体的表。在使用 Sublime Text 3 W3C Validator 进行验证时,我收到错误消息:“由元素 th 建立的范围 3...4 中的表列没有以它们开头的单元格”。这可能是一个错误还是我对表格的编码不正确?请在下面找到表格代码:

<table>
    <thead>
        <tr>
        <th colspan="5"><a id="button01" href="#" title="Learn More">Learn More</a></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="1">A</td>
            <td colspan="3">Item A</td>
            <td colspan="1"><span class="bullet">*</span></td>
        </tr>
        <tr>
            <td colspan="1">B</td>
            <td colspan="3">Item B</td>
            <td colspan="1"><span class="bullet">*</span></td>
        </tr>
        <tr>
            <td colspan="1">M</td>
            <td colspan="3">Traditional (DIN-compliant flange)</td>
            <td colspan="1"><span class="bullet">*</span></td>
        </tr>
    </tbody>
</table>
4

1 回答 1

1

为了解决这个错误,我使用colspan="1"而不是colspan="3"并将表头colspan="5"更改为colspan="3"。这消除了 W3C HTML5 Validator 错误。我还包括了我用来根据自己的需要设计样式的 css。

请参阅下面的代码:

<table>
  <thead>
    <tr>
      <th colspan="3"><a id="button01" href="#" title="Learn More">Learn More</a>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="1">A</td>
      <td colspan="1">Item A</td>
      <td colspan="1"><span class="bullet">*</span>
      </td>
    </tr>
    <tr>
      <td colspan="1">B</td>
      <td colspan="1">Item B</td>
      <td colspan="1"><span class="bullet">*</span>
      </td>
    </tr>
    <tr>
      <td colspan="1">M</td>
      <td colspan="1">Traditional (DIN-compliant flange)</td>
      <td colspan="1"><span class="bullet">*</span>
      </td>
    </tr>
  </tbody>
</table>

/*
 * Table Components
 */

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  line-height: 20px;
}
thead {
  background: #EEE;
  font-weight: bold;
}
td {
  border: 1px solid #222;
  padding: .35em;
  vertical-align: middle;
}
td:first-child {
  width: 10%;
}
td:last-child {
  width: 5%;
}

于 2014-12-04T14:29:46.683 回答