如何指定一个 td
标签跨越所有列(当HTML被渲染时,表格中确切的列数会是不确定/难以确定的)?w3schools 提到可以使用 colspan="0"
,但它没有明确说明哪些浏览器支持该值(我们需要支持IE 6)。
看起来将 colspan
设置为大于您可能拥有的理论列数的值将起作用,但如果您将 table-layout
设置为 fixed
,则不起作用。使用具有大量 colspan
的自动布局是否有任何缺点?是否有更正确的方法来实现这一点?
如何指定一个 td
标签跨越所有列(当HTML被渲染时,表格中确切的列数会是不确定/难以确定的)?w3schools 提到可以使用 colspan="0"
,但它没有明确说明哪些浏览器支持该值(我们需要支持IE 6)。
看起来将 colspan
设置为大于您可能拥有的理论列数的值将起作用,但如果您将 table-layout
设置为 fixed
,则不起作用。使用具有大量 colspan
的自动布局是否有任何缺点?是否有更正确的方法来实现这一点?
只需使用这个:
colspan="100%"
它可以在Firefox 3.6、IE 7和Opera 11上运行!(我想其他浏览器也可以,但我没试过)
警告:如下方的评论所述,这实际上与colspan="100"
相同。 因此,这个解决方案对于具有CSS table-layout: fixed
或超过100列的表将无效。
colspan="100%"
的意思确实是colspan="100"
。 - NemoStein我有IE 7.0,Firefox 3.0和Chrome 1.0
在任何上述浏览器中,TD中的colspan =“0”属性不会跨越所有TD 。
也许不是适当的标记实践建议,但是如果您给出比其他行中可能的总列数更高的 colspan值,则TD将跨越所有列。
当table-layout CSS属性设置为固定时,此方法无效。
再次强调,这不是完美的解决方案,但在上述提到的三个浏览器版本中,当table-layout CSS属性为自动时,该方法似乎有效。
colspan="42"
将整个区域合并。显然,对于大于42列的情况,这是一个问题,但这是我认可的为数不多的神奇数字之一。 - Martin<table>
<caption style="gimme some style!"><!-- Title of table --></caption>
<thead><!-- ... --></thead>
<tbody><!-- ... --></tbody>
</table>
不要
<div>
<div style="float: left;/* extra styling /*"><!-- Title of table --></div>
<table>
<thead><!-- ... --></thead>
<tbody><!-- ... --></tbody>
</table>
<div style="clear: both"></div>
</div>
以下是一些关于colspan="0"
的部分答案,这个在问题中被提到。
colspan="0"
在任何浏览器中都不起作用。W3Schools是错误的(像往常一样)。HTML 4规定colspan="0"
应该使一列跨越整个表格,但没有人实现它,在HTML 4之后就从规范中删除了。
All major browsers treat it as equivalent to colspan="1"
.
Here's a demo showing this; try it on any browser you like.
td {
border: 1px solid black;
}
<table>
<tr>
<td>ay</td>
<td>bee</td>
<td>see</td>
</tr>
<tr>
<td colspan="0">colspan="0"</td>
</tr>
<tr>
<td colspan="1">colspan="1"</td>
</tr>
<tr>
<td colspan="3">colspan="3"</td>
</tr>
<tr>
<td colspan="1000">colspan="1000"</td>
</tr>
</table>
The HTML 4 spec (now old and outdated, but current back when this question was asked) did indeed say that colspan="0"
should be treated as spanning all columns:
The value zero ("0") means that the cell spans all columns from the current column to the last column of the column group (COLGROUP) in which the cell is defined.
However, most browsers never implemented this.
HTML 5.0 (made a candidate recommendation back in 2012), the WhatWG HTML living standard (the dominant standard today), and the latest W3 HTML 5 spec all do not contain the wording quoted from HTML 4 above, and unanimously agree that a colspan
of 0 is not allowed, with this wording which appears in all three specs:
The
td
andth
elements may have acolspan
content attribute specified, whose value must be a valid non-negative integer greater than zero ...
Sources:
The following claims from the W3Schools page linked to in the question are - at least nowadays - completely false:
Only Firefox supports colspan="0", which has a special meaning ... [It] tells the browser to span the cell to the last column of the column group (colgroup)
and
Differences Between HTML 4.01 and HTML5
NONE.
If you're not already aware that W3Schools is generally held in contempt by web developers for its frequent inaccuracies, consider this a lesson in why.
colspan="0"
自那时起就已经被正式禁止使用。 - Mark Amery对于IE 6,您需要将colspan设置为表格中列数的数量。如果有5列,则需要:colspan="5"
。
原因在于IE以不同的方式处理colspan,它使用HTML 3.2规范:
IE实现了HTML 3.2的定义,将
colspan=0
设置为colspan=1
。
这个错误已经有充分的文献记录。
colspan
必须是正整数,这使得colspan=0
是不合法的;它也没有明确规定将colspan=0
处理为colspan=1
(虽然我相信这实际上是IE 6的做法)。此外,该引文不再(?)出现在您链接到的论坛页面上,大多数谷歌搜索结果(现在?)都与完全不同的IE 6 colspan
相关错误有关。 - Mark Ameryfunction getMaxColCount($table) {
var maxCol = 0;
$table.find('tr').each(function(i,o) {
var colCount = 0;
$(o).find('td:not(.maxcols),th:not(.maxcols)').each(function(i,oo) {
var cc = Number($(oo).attr('colspan'));
if (cc) {
colCount += cc;
} else {
colCount += 1;
}
});
if(colCount > maxCol) {
maxCol = colCount;
}
});
return maxCol;
}
$('td.maxcols, th.maxcols').each(function(i,o) {
$t = $($(o).parents('table')[0]); $(o).attr('colspan', getMaxColCount($t));
});
Array.from(document.querySelectorAll('[data-colspan-max]')).forEach(td => {
let table = td;
while (table && table.nodeName !== 'TABLE') table = table.parentNode;
td.colSpan = Array.from(table.querySelector('tr').children).reduce((acc, child) => acc + child.colSpan, 0);
});
html {
font-family: Verdana;
}
tr > * {
padding: 1rem;
box-shadow: 0 0 8px gray inset;
}
<table>
<thead>
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
<th>Header 4</th>
<th>Header 5</th>
<th>Header 6</th>
</tr>
</thead>
<tbod><tr>
<td data-colspan-max>td will be set to full width</td>
</tr></tbod>
</table>
另一种可行但不美观的解决方案是:colspan="100"
,其中100是大于你需要跨列的总列数的值。
根据W3C的规定,colspan="0"
选项仅适用于COLGROUP
标签。
<td>
具有colspan="0"
(请参见https://www.w3.org/TR/REC-html40/struct/tables.html#adef-colspan),而HTML 5.0不允许在colgroup
上使用span="0"
(请参见https://www.w3.org/TR/html50/tabular-data.html#attr-colgroup-span,其中指出“span内容属性的值必须是有效的大于零的非负整数。”)。 - Mark Amerycolspan
设置为表格中的列数即可。最好的方法是一开始就将colspan
设置为正确的列数。如果您的表格有5列,请将其设置为colspan="5"
,这是在所有情况下都有效的唯一方法。不,这并不是过时的解决方案,也不仅适用于IE6或任何其他情况--这实际上是处理此问题的最佳方式。
除非列数在运行时更改,否则不建议使用Javascript来解决此问题。
如果列数是可变的,那么您需要计算列数,以便可以填充colspan
。如果您的列数是可变的,那么生成表格的任何内容都应该能够适应并同时计算表格的列数。
如其他回答所述,如果您的表格未设置为table-layout: fixed
,则也可以将colspan
设置为一个非常大的数字。但我认为这个解决方案很混乱,并且如果您稍后决定使用固定表格布局,则可能会造成麻烦。最好还是第一次就正确地做。
我一直都是这样用的;如果不够,就增加数量。 (超出您认为会有的列数)
<td colspan='999'>