简体   繁体   English

表标签的正确html标记?

[英]Proper html markup of table tags?

Kind of a silly question, but I've been seeing things such as tbody and thead/tfoot tags in other peoples tables. 这是一个愚蠢的问题,但我一直在其他人的表中看到诸如tbody和thead / tfoot标签之类的东西。

Are these required even if they're empty for good markup? 这些是否需要即使它们是空的以获得良好的标记? Or can I just leave them out? 或者我可以把它们留下来?

The table sections (thead/tbody/tfoot) are optional. 表格部分(thead / tbody / tfoot)是可选的。 The table caption (caption) and column definitons (col/colgroup) are also optional. 表格标题(标题)和列定义(col / colgroup)也是可选的。

In HTML (but not XHTML) the closing tag for rows and cells are also optional, so you could write a table as: 在HTML(但不是XHTML)中,行和单元格的结束标记也是可选的,因此您可以将表格写为:

<table>
  <tr>
    <th>1
    <th>2
  <tr>
    <td>3
    <td>4
  <tr>
    <td>5
    <td>6
</table>

It's however recommended that you close the tags to get better structure in the code. 但是,建议您关闭标记以在代码中获得更好的结构。 It also makes it a lot easier if you decide to change to XHTML. 如果您决定更改为XHTML,它也会使它变得容易多了。

它们不是必需的,但它们允许您使用页眉和页脚执行更高级的操作: http//www.htmldog.com/guides/htmladvanced/tables/

这些标签不是必需的,即使没有它们,页面也会验证。

if you really need tables, you should use them. 如果你真的需要表,你应该使用它们。 If you use tables only for design purposes you should switch to css-markup. 如果您仅将表用于设计目的,则应切换到css-markup。

A simple correct table example: 一个简单正确的表示例:

<table>
  <thead>
    <tr>
      <td>id</td>
      <td>name</td>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>User1</td>
    </tr>
    <tr>
      <td>2</td>
      <td>User2</td>
    </tr>
  </tbody>
</table>

Yes, that is proper markup. 是的,这是正确的标记。 Though they are optional You can read further on the W3Schools page. 虽然它们是可选的您可以在W3Schools页面上进一步阅读。

The <tbody> tag is only partially supported in all major browsers. 所有主流浏览器仅部分支持<tbody>标记。
Tables may have multiple bodies, but when it only has one single body, the HTML tbody tag may be safely omitted. 表可以有多个主体,但是当它只有一个主体时,可以安全地省略HTML tbody标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM