HTML Table - Both fixed and multiple variable column widths

后端 未结 3 2086
我寻月下人不归
我寻月下人不归 2021-02-12 15:47

I have to build a table with 5 columns. The table width is variable (50% of content width). Some columns contain fixed-size buttons, so those columns should have a fixed with, s

相关标签:
3条回答
  • 2021-02-12 16:17

    to fixed width in a table, you need the table-layout propertie set to fixed; Since you mix % and px , it will not be coherent .

    You may set only the px width and eventually a small value of % and let other column to use width left avalaible. example : http://jsfiddle.net/M4Et8/2/

    <table style='table-layout:fixed;width:100%' border='1'>
        <tr>
            <th style='width: 20%;'>Column1</th>
            <th style='width: 100px;'>Column2</th>
            <th >Column3</th>
            <th style='width: 200px;'>Column4</th>
            <th >Column5</th>
        </tr>
    </table>
    
    0 讨论(0)
  • 2021-02-12 16:20

    I have applied the same solution: Used 'table-layout:fixed' and given the size to td in percentages. It worked for my static content but when my content increases it some how expands the td and all the td and tr are disturbed.

    Solution:

    After so much findings, i came up with the following solution, by this solution your single table will be able to display different column sizes with no expand on horizontal axis.

    <table style="table-layout: fixed;">
        <tbody>
            <tr>
                <td colspan="7">
                    <span>Record Name</span><br>
                    <span>FUNNY FUNNEL CAKES</span>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <span>Event Name</span><br>
                    <span>NATIONAL CAKE DAY CELEBRATION</span>
                </td>
                <td colspan="2">
                    <span>City</span><br>
                    <span>SAN DIEGO</span>
                </td>
                <td colspan="1">
                    <span>Zip</span><br>
                    <span>92117-4351</span>
                </td>
                <td colspan="1">
                    <span>Inspection Type</span><br>
                    <span>Routine</span>
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <span>Owner</span><br>
                    <span>PATTY CAKE</span>
                </td>
                <td colspan="1">
                    <span>Inspection Status</span><br>
                    <span>Complete</span>
                </td>
            </tr>
        </tbody>
    </table>
    
    0 讨论(0)
  • 2021-02-12 16:34

    You could set the table-layout: fixed for the table element, then simply set width attribute on relevant <td> or <th> elements:

    table {
        table-layout: fixed;
    }
    

    JSFiddle Demo.

    From the MDN:

    The table-layout CSS property defines the algorithm to be used to layout the table cells, rows, and columns.

    Values:

    fixed:
    Table and column widths are set by the widths of table and col elements or by the width of the first row of cells. Cells in subsequent rows do not affect column widths.

    0 讨论(0)
提交回复
热议问题