How to prevent line-break in a column of a table cell (not a single cell)?

后端 未结 9 956
粉色の甜心
粉色の甜心 2020-12-07 23:43

How can I prevent automatic line breaks in a column of table (not a single cell)?

相关标签:
9条回答
  • 2020-12-08 00:11

    Put non-breaking spaces in your text instead of normal spaces. On Ubuntu I do this with (Compose Key)-space-space.

    0 讨论(0)
  • 2020-12-08 00:13

    Use the nowrap style:

    <td style="white-space:nowrap;">...</td>
    

    It's CSS!

    0 讨论(0)
  • 2020-12-08 00:14

    You can use the CSS style white-space:

    white-space: nowrap;
    
    0 讨论(0)
  • 2020-12-08 00:15
    <td style="white-space: nowrap">
    

    The nowrap attribute I believe is deprecated. The above is the preferred way.

    0 讨论(0)
  • 2020-12-08 00:20

    For completion sake:

    #table_id td:nth-child(2)  {white-space: nowrap;}
    

    Is used for applying a style to the 2 column of the table_id table.

    This is supported by all major Browsers, IE started supporting this from IE9 onwards.

    0 讨论(0)
  • 2020-12-08 00:24

    There are a few ways to do this; none of them are the easy, obvious way.

    Applying white-space:nowrap to a <col> won't work; only four CSS properties work on <col> elements - background-color, width, border, and visibility. IE7 and earlier used to support all properties, but that's because they used a strange table model. IE8 now matches everyone else.

    So, how do you solve this?

    Well, if you can ignore IE (including IE8), you can use the :nth-child() pseudoclass to select particular <td>s from each row. You'd use td:nth-child(2) { white-space:nowrap; }. (This works for this example, but would break if you had any rowspans or colspans involved.)

    If you have to support IE, then you've got to go the long way around and apply a class to every <td> that you want to affect. It sucks, but them's the breaks.

    In the long run, there are proposals to fix this lack in CSS, so that you can more easily apply styles to all the cells in a column. You'll be able to do something like td:nth-col(2) { white-space:nowrap; } and it would do what you want.

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