我可以让一个 HTML 表格标题超过两个表格列吗?喜欢在 Excel 中合并和居中吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8996466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:45:22  来源:igfitidea点击:

Can I have one HTML table header be over two table columns? Like merge and center in Excel?

htmlhtml-tablecentertableheader

提问by Lizza

I want to have one table header be centered over two table columns side by side. Is this possible?

我想让一个表格标题并排位于两个表格列的中心。这可能吗?

回答by SpoonNZ

<th colspan="2">. This .</th>

<th colspan="2">. This .</th>

To extrapolate a bit...

稍微推断一下...

<table>
  <thead>
    <tr>
      <th>Single Column</th>
      <th colspan="2">Double Column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Column One</td>
      <td>Column Two</td>
      <td>Column Three</td>
    </tr>
  </tbody>
</table>

That should give you enough to work from.

这应该给你足够的工作。

回答by Travis J

If you only have 2 columns then I would suggest using <caption>. Otherwise, use colspan as suggested in other answers.

如果您只有 2 列,那么我建议使用<caption>. 否则,请按照其他答案中的建议使用 colspan。

<table>
  <caption>This will span all columns.</caption>
  <tr><td>column one</td><td>column two</td></tr>
</table>

回答by Andrzej O?mia?owski

Of course. Please refer to thispage. You are looking for attribute called colspanfor table headers cells.

当然。请参阅页面。您正在寻找为colspan表格标题单元格调用的属性。

回答by Srikrushna

We can do it in better way.

我们可以以更好的方式做到这一点。

<table border="1"> 
  <tr>
    <th colspan="1" scope="colgroup">Test Heading</th>
    <th colspan="3" scope="colgroup">Mars</th>
    <th colspan="3" scope="colgroup">Venus</th>
    <th colspan="3" scope="colgroup">Test</th>
  </tr>
  <tr>
    <th rowspan="2"></th>
    <th scope="col">Produced</th>
    <th scope="col">Sold</th>
    <th scope="col">Sold</th>
    <th scope="col">Produced</th>
    <th scope="col">Sold</th>
    <th scope="col">Sold</th>
    <th scope="col">Test 1</th>
    <th scope="col">Test 2</th>
    <th scope="col">Test 3</th>
  </tr>
</table>

Please visit Table Ref From w3.orgif you want to know more.

如果您想了解更多信息,请访问来自 w3.org 的表格参考