【问题标题】:css, html - colspan not working the same on different tables [duplicate]css,html - colspan 在不同的表上工作不一样[重复]
【发布时间】:2019-01-04 17:05:28
【问题描述】:

我有两个要对齐的 html 表格(两个表格的列的宽度应该相等)。两个表的标题都应该在开始时跨越两列,并且都应该有四列。

即使定义了列的宽度,也会根据列内容调整大小。

td,
th {
  padding: .5rem;
}

.red-header {
  font-weight: 700;
  background-color: rgb(184, 167, 177);
  text-align: start;
}
<table>
  <thead>
    <tr>
      <th colspan="2" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      <th width='35%' />
      <th width="15%" />
      <th width="15%" />
    </tr>
  </thead>
  <tbody>
    <tr>
      <td width="35%">something</td>
      <td width="35%">something else</td>
      <td width="15%">something</td>
      <td width="15%">something else</td>
    </tr>
  </tbody>
</table>

<table>
  <thead>
    <tr>
      <th colspan="2" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      <th width='35%' />
      <th width="15%">something</th>
      <th width="15%">something else</th>
    </tr>
  </thead>
  <tbody />
</table>

无论内容如何,​​如何使列保持其大小?

【问题讨论】:

  • 使用table-layout: fixed 并给它一个宽度,例如table { table-layout: fixed; width: 100%; }
  • ...修正表格列数,现在您有 5 个标题单元格和 4 个内容。
  • 请注意,表格元素不是自闭标签&lt;th /&gt;,它们是开始/结束标签&lt;th&gt;&lt;/th&gt; ...尽管您可以省略结尾(但通常不建议这样做)更难阅读):stackoverflow.com/questions/4020374/…
  • @LGSon 谢谢,这解决了我遇到的问题。你可以把它写成anwser,我会接受的。基本上我需要table-layout: fixed,还需要删除colspan 为2 的不必要的td 元素。

标签: html css


【解决方案1】:

您在第二个表中将 colspan 设置为 2,但第二个标题仍然存在,但缺少结束标记。如果您删除缺少标签的第二个标题,它应该可以解决您的问题。

<table>
  <thead>
    <tr>
      <th colspan="2" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      <th width='35%' />
      <th width="15%" />
      <th width="15%" />
    </tr>
  </thead>
  <tbody>
    <tr>
      <td width="35%">something</td>
      <td width="35%">something else</td>
      <td width="15%">something</td>
      <td width="15%">something else</td>
    </tr>
  </tbody>
</table>

<table>
  <thead>
    <tr>
      <th colspan="2" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>

      <th width="15%">something</th>
      <th width="15%">something else</th>
    </tr>
  </thead>
  <tbody />
</table>

或关闭标签

<table>
  <thead>
    <tr>
      <th colspan="2" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      <th width='35%' />
      <th width="15%" />
      <th width="15%" />
    </tr>
  </thead>
  <tbody>
    <tr>
      <td width="35%">something</td>
      <td width="35%">something else</td>
      <td width="15%">something</td>
      <td width="15%">something else</td>
    </tr>
  </tbody>
</table>

<table>
  <thead>
    <tr>
      <th colspan="2" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      <th width='35%' /></th>
      <th width="15%">something</th>
      <th width="15%">something else</th>
    </tr>
  </thead>
  <tbody />
</table>

【讨论】:

  • 不,这不能解决问题。
  • 对不起,我可能没听懂你在问什么。我再看看。
  • 缺少关闭标签不是问题...stackoverflow.com/questions/4020374/…
  • 为什么将 colspan 设置为 2?它不加起来。
  • 我不是那个问...但这里的主要问题不是那个(如果它有不等列的问题),它是如何确保表格单元格保持它们的大小无关内容。
【解决方案2】:

我发现你的代码的一个错误是你在多个地方错过了关闭标签..你可以同时使用 colspan 或宽度的百分比,这没有意义你可以使用 colspan

table{
  width:100%
}
<table border='1'>
  <thead>
    <tr>
      <th colspan="4" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      
    </tr>
  </thead>
  <tbody>
    <tr>
      <td >something</td>
      <td >something else</td>
      <td >something</td>
      <td >something else</td>
    </tr>
  </tbody>
</table>

<table border="1">
  <thead>
    <tr>
      <th colspan="4" width="35%" class="red-header">
        THIS IS A LONG LONG HEADER STRING
      </th>
      
    </tr>
  </thead>
  
  <tbody>
    <tr>
      <td >something</td>
      <td >something else</td>
      <td >something</td>
      <td >something else</td>
    </tr>
  </tbody>
</table>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-03-18
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2021-12-20
  • 1970-01-01
  • 2014-09-08
相关资源
最近更新 更多