How to prevent line-break in a column of a table c

2019-01-22 00:26发布

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

标签: html css layout
9条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-22 01:08

Use the nowrap style:

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

It's CSS!

查看更多
Juvenile、少年°
3楼-- · 2019-01-22 01:08
<td style="white-space: nowrap">

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

查看更多
迷人小祖宗
4楼-- · 2019-01-22 01:10

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.

查看更多
一纸荒年 Trace。
5楼-- · 2019-01-22 01:13

To apply it to the entire table, you can place it within the table tag:

<table style="white-space:nowrap;">

查看更多
Viruses.
6楼-- · 2019-01-22 01:22

You can use the CSS style white-space:

white-space: nowrap;
查看更多
狗以群分
7楼-- · 2019-01-22 01:22
 <table class="blueTable">
   <tr>
      <td>My name is good</td>
    </tr>
 </table>    
 table.blueTable td, table.blueTable th {border: 1px solid #AAAAAA;padding: 3px 2px;white-space: nowrap;text-align: left;
}

This is an example usage of the white space property with value nowrap, the bluetable is the class of the table, below the table are the css styles

查看更多
登录 后发表回答