Colspan在纯HTML中不起作用,没有应用CSS,应用于每一行的th/td中。

huangapple go评论65阅读模式
英文:

Colspan not working in pure HTML, no CSS, applied to th/td, equally in each row

问题

我无法弄清楚为什么我的colspan不起作用。我有一个CSS解决方案,但我只是对为什么这不起作用感到困惑。第一列应该是其他列宽度的1/3,第一个thtd上使用了colspan="1",而其他列都使用了colspan="3"

这是一个排行榜的应用,所以第一列将包含排名(1、2、3...),需要比后续列窄得多。

当然,可以使用class="colspan-3"class="colspan-1"以及CSS colspan-1{width:4.5%} colspan-3{width:13.5%}或者根据数学运算得出的任何值来解决这个问题...但再次强调,我只是想知道出了什么问题。

为了补充细节,因为我认为这个问题将帮助将来的某个人:

当时,我认为可以将colspan用作相对单位,基本上是通过使每列的宽度相对于其他列。我遇到的问题是每列都具有相同的单位,这只在列中的单元格中真正起作用,基本上就像Microsoft Excel中的“合并”功能一样。

<table border="2">
  <thead>
    <tr>
      <th colspan="1">th1 colspan1</th><!-- 这里使用了colspan=1 -->
      <th colspan="3">th2 colspan3</th>
      <th colspan="3">th3 colspan3</th>
      <th colspan="3">th4 colspan3</th>
      <th colspan="3">th5 colspan3</th>
      <th colspan="3">th6 colspan3</th>
      <th colspan="3">th7 colspan3</th>
      <th colspan="3">th8 colspan3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td colspan="1">td1 colspan1</td><!-- 这里使用了colspan=1 -->
      <td colspan="3">td2 colspan3</td>
      <td colspan="3">td3 colspan3</td>
      <td colspan="3">td4 colspan3</td>
      <td colspan="3">td5 colspan3</td>
      <td colspan="3">td6 colspan3</td>
      <td colspan="3">td7 colspan3</td>
      <td colspan="3">td8 colspan3</td>
    </tr>
  </tbody>
</table>

Colspan在纯HTML中不起作用,没有应用CSS,应用于每一行的th/td中。
1: https://i.stack.imgur.com/DBpnL.png

英文:

I cannot figure out why my colspan isn't working. I have a CSS solution, but I am just baffled why this wouldn't work. The First column should be 1/3 the width of the other columns with colspan="1" on the first th and td, while all others have colspan="3".

The application is for a leaderboard, so the first column will contain the rank number (1, 2, 3...) and needs to be much more narrow than the subsequent columns.

Of course, it can be solved with class=&quot;colspan-3&quot; and class=&quot;colspan-1&quot; along with the css colspan-1{width:4.5%} colspan-3{width:13.5%} or whatever the math works out to... but again, I really just want to know what's up.

To add details, because I think this question will help someone in the future:

At the time, I thought i could use colspan as a relative unit, essentially, by making each column's width relative to the other columns. The issue I was having was that each column had the exact same unit in it all the way down. It only really works for a single cell in a column, essentially like the "merge" function in Microsoft Excel.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;table border=&quot;2&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th colspan=&quot;1&quot;&gt;th1 colspan1&lt;/th&gt;&lt;!-- colspan=1 here --&gt;
      &lt;th colspan=&quot;3&quot;&gt;th2 colspan3&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;th3 colspan3&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;th4 colspan3&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;th5 colspan3&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;th6 colspan3&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;th7 colspan3&lt;/th&gt;
      &lt;th colspan=&quot;3&quot;&gt;th8 colspan3&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td colspan=&quot;1&quot;&gt;td1 colspan1&lt;/td&gt;&lt;!-- colspan=1 here --&gt;
      &lt;td colspan=&quot;3&quot;&gt;td2 colspan3&lt;/td&gt;
      &lt;td colspan=&quot;3&quot;&gt;td3 colspan3&lt;/td&gt;
      &lt;td colspan=&quot;3&quot;&gt;td4 colspan3&lt;/td&gt;
      &lt;td colspan=&quot;3&quot;&gt;td5 colspan3&lt;/td&gt;
      &lt;td colspan=&quot;3&quot;&gt;td6 colspan3&lt;/td&gt;
      &lt;td colspan=&quot;3&quot;&gt;td7 colspan3&lt;/td&gt;
      &lt;td colspan=&quot;3&quot;&gt;td8 colspan3&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Colspan在纯HTML中不起作用,没有应用CSS,应用于每一行的th/td中。

答案1

得分: 1

感谢 @Ouroborus,我以为我疯了,但在使用HTML将近十年后,显然还有东西要学。

如果一列中的所有单元格都设置为要跨越的相同列数,如在这里定义的那样,colspan 将不起作用:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td

再次感谢Ouroborus纠正了我的误解!

英文:

Thanks to @Ouroborus, I thought I was going nuts but after nearly a decade of using HTML every day apparently there are still things to be learned.

colspan won't work if all cells in a column are set to the same number of columns to span, as defined here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td

Thanks again to Ouroborus for catching my misunderstanding!

huangapple
  • 本文由 发表于 2023年4月7日 02:40:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定