为什么 colspan 没有按预期应用 [英] Why is colspan not applied as expected

查看:21
本文介绍了为什么 colspan 没有按预期应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张如下表.第二行定义了三列,一列使用 colspan=8,其他列使用 colspan=1.尽管如此,单元格不会根据 colspan 拉伸,第二个单元格的宽度"多一点,第三个单元格的宽度"最宽.

I have a table according to below. The second row has defined three columns, one with colspan=8 and the others with colspan=1. Still, the cells are not stretched according to the colspan, the "width" are a little bit more for second cell and widest for the third.

<table class="floating simpletable">
    <tbody>
        <tr><td colspan="10">1st row</td></tr>
        <tr><td colspan="8">Column 1 -&gt; Least wide</td><td colspan="1">2nd</td><td colspan="1">3rd</td></tr>
        <tr><td colspan="10">3rd row</td></tr>
        <tr><td colspan="1">1st cell</td><td colspan="9">4th row</td></tr>
    </tbody>
</table>

有什么问题以及如何解决?

What's the problem and how to fix it?

推荐答案

单元格的宽度取决于单元格内容、宽度的 HTML 和 CSS 设置、浏览器以及可能的月相.colspan 属性只是指定一个单元格占据多少列(因此,表格网格中有多少个插槽).

The widths of cells depend on cell contents, HTML and CSS settings for widths, browser, and possibly phase of the moon. The colspan attribute just specifies how many columns (hence, how many slots in the grid for the table) a cell occupies.

如果你看到第 2 行的最后一个单元格是最宽的,那么原因可能是它的内容最多(或者它有一个宽度设置).您的演示代码未展示此类行为.

If you see the last cell of row 2 as the widest, then the reason is probably that it has most contents (or there is a width setting for it). Your demo code does not demonstrate such behavior.

如果您不想根据单元格的大小要求调整列宽,请在 CSS(或 HTML)中明确设置宽度.在此之前,最好从表结构中去除所有不必要的复杂性.如果您的演示代码反映了整个结构,则第 2 列到第 8 列是不必要的划分,即它们可以变成单个列.演示(为了确定性而使用糟糕的像素宽度):

If you don’t want the column widths adjust to the size requirements of cells, set the widths explicitly in CSS (or in HTML). Before this, it is best to remove all unnecessary complications from the table structure. If your demo code reflects the entire structure, then columns 2 through 8 are an unnecessary division, i.e. they could be turned to a single column. Demonstration (with poor-style pixel widths just for definiteness):

<table class="floating simpletable" border>
    <col width=100><col width=100><col width=100>
    <tbody>
        <tr><td colspan="4">1st row</td></tr>
        <tr><td colspan="2">span 1</td><td>span 2</td><td>span 3 </td></tr>
        <tr><td colspan="4">3rd row</td></tr>
        <tr><td>span</td><td colspan="3">other span</td></tr>
    </tbody>
</table>

如果没有这样的重写,恐怕你的表格违反了 HTML 的表格模型,因为目前没有从第 3 列或第 4 列开始的单元格或...

Without a rewrite like this, I’m afraid your table violates the table model of HTML, as currently there is no cell that starts in column 3 or column 4 or...

这篇关于为什么 colspan 没有按预期应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆