CSS table column autowidth

前端 未结 4 1192
感情败类
感情败类 2020-12-02 07:45

Given the following how do i make my last column auto size to its content? (The last column should autosize-width to the content. Suppose i have only 1 li element it should

相关标签:
4条回答
  • 2020-12-02 08:15

    use auto and min or max width like this:

    td {
    max-width:50px;
    width:auto;
    min-width:10px;
    }
    
    0 讨论(0)
  • 2020-12-02 08:23

    If you want to make sure that last row does not wrap and thus size the way you want it, have a look at

    td {
     white-space: nowrap;
    }
    
    0 讨论(0)
  • 2020-12-02 08:36

    The following will solve your problem:

    td.last {
        width: 1px;
        white-space: nowrap;
    }
    

    Flexible, Class-Based Solution

    And a more flexible solution is creating a .fitwidth class and applying that to any columns you want to ensure their contents are fit on one line:

    td.fitwidth {
        width: 1px;
        white-space: nowrap;
    }
    

    And then in your HTML:

    <tr>
        <td class="fitwidth">ID</td>
        <td>Description</td>
        <td class="fitwidth">Status</td>
        <td>Notes</td>
    </tr>
    
    0 讨论(0)
  • 2020-12-02 08:38

    You could specify the width of all but the last table cells and add a table-layout:fixed and a width to the table.

    You could set

    table tr ul.actions {margin: 0; white-space:nowrap;}
    

    (or set this for the last TD as Sander suggested instead).

    This forces the inline-LIs not to break. Unfortunately this does not lead to a new width calculation in the containing UL (and this parent TD), and therefore does not autosize the last TD.

    This means: if an inline element has no given width, a TD's width is always computed automatically first (if not specified). Then its inline content with this calculated width gets rendered and the white-space-property is applied, stretching its content beyond the calculated boundaries.

    So I guess it's not possible without having an element within the last TD with a specific width.

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