简体   繁体   English

HTML表格,每行中包含不同数量的单元格:如何在单元格中均匀分隔单元格?

[英]HTML table with different number of cells in each row: how to space the cells evenly in their row?

I have an HTML table where the number of columns may differ from one row to another. 我有一个HTML表格,其中列数可能因行而异。 Nonetheless, I would like the columns to be evenly spaced in their row. 尽管如此,我希望列在它们的行中均匀分布。 (For instance, if my first row contains 2 columns, each column should have 50% of the total width of the table; if the second row has 3 columns, each of them should have ~33% of the total width of the table.) (例如,如果我的第一行包含2列,则每列应该具有表的总宽度的50%;如果第二行有3列,则每列应该具有表的总宽度的约33%。 )

Moreover, I would like the table to have the width of the container (so probably with a width: 100% in the CSS; but the problem is that this property tells the table to split the columns evenly based on what is in the forst row only). 此外,我希望表具有容器的宽度(因此可能在CSS中具有width: 100% ;但问题是此属性告诉表根据forst行中的内容均匀地拆分列只要)。 The container width may vary, this is why I have to use percentages. 容器宽度可能会有所不同,这就是我必须使用百分比的原因。

I also tried to make my table myself with divs and by specifying the width of each "cell-div" with a percentage, but this solution doesn't provide a good result on every browser: sometimes, when there is no more space on the line, the last "cell-div" of a row goes under it. 我也尝试用div创建自己的表,并用百分比指定每个“cell-div”的宽度,但是这个解决方案并不能在每个浏览器上提供好的结果:有时,当没有更多的空间时行,行的最后一个“cell-div”在它下面。 (I have to specify some weird width such as 49.99998% or 33.333233% because I need some space for my borders as well.) (我必须指定一些奇怪的宽度,例如49.99998%33.333233%因为我的边框也需要一些空间。)

Sounds like you're not really using the table element to display tabular data, just for some alignment. 听起来你并没有真正使用table元素来显示表格数据,只是为了一些对齐。 One way to get the result that you want is to make each row its own table and just set a width. 获得所需结果的一种方法是使每一行成为自己的表,并设置宽度。 Each row will evenly distribute the cells across the table width. 每行将在表格宽度上均匀分布单元格。 You might need to add some more CSS formatting to get it to behave just the way you want. 您可能需要添加一些CSS格式以使其按照您希望的方式运行。

You can see this at http://jsfiddle.net/RxSe5/1/ . 你可以在http://jsfiddle.net/RxSe5/1/看到这个。

You can add tables inside each of the TD: 您可以在每个TD中添加表格:

    <table style="width:100%;text-align:center;">
        <tr>
            <td>
                <table style="width:100%">
                    <tr>
                        <td>1</td>
                        <td>2</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table style="width:100%">
                    <tr>
                        <td>1</td>
                        <td>2</td>
                        <td>3</td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
​

Or you can just add new tables: 或者你可以添加新表:

<div>
    <table style="width:100%;text-align:center;">
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
    </table>
    <table style="width:100%;text-align:center;">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
    </table>
    <table style="width:100%;text-align:center;">
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
    </table>
</div>​

you could totally nest tables, you could also use tables for layout. 你可以完全嵌套表,你也可以使用表格进行布局。 or you could use markup how it's supposed to be used and enjoy all the fruits of its labors (accessibility, semantics, usability, etc.) just by doing the right thing. 或者你可以使用标记如何使用它并通过做正确的事情来享受其工作的所有成果(可访问性,语义,可用性等)。 you can can change column with via colspan="" attribute on you td elements. 您可以在td元素上使用via colspan =“”属性更改列。 http://www.w3.org/TR/html-markup/td.html http://www.w3.org/TR/html-markup/td.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM