Html 如何防止表格单元格(不是单个单元格)的列中的换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1893751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:32:55  来源:igfitidea点击:

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

htmlcsslayout

提问by Steven

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

如何防止表格列(不是单个单元格)中的自动换行?

回答by David M

You can use the CSS style white-space:

您可以使用 CSS 样式的空白:

white-space: nowrap;

回答by estani

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_idtable.

用于将样式应用于the table_id表格的 2 列。

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

所有主要浏览器都支持此功能,IE 从 IE9 开始支持此功能。

回答by Derek Illchuk

Use the nowrap style:

使用 nowrap 样式:

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

It's CSS!

是CSS!

回答by Xanthir

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.

将 white-space:nowrap 应用于 a<col>将不起作用;只有四个 CSS 属性适用于<col>元素 - 背景颜色、宽度、边框和可见性。IE7 及更早版本曾经支持所有属性,但那是因为它们使用了一种奇怪的表模型。IE8 现在匹配其他所有人。

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.)

好吧,如果您可以忽略 IE(包括 IE8),您可以使用:nth-child()伪类<td>从每一行中选择特定的s。你会用td:nth-child(2) { white-space:nowrap; }. (这适用于本示例,但如果涉及任何 rowspans 或 colspans,则会中断。)

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.

如果您必须支持 IE,那么您必须走很长的路,并将一个类应用于<td>您想要影响的每个类。这很糟糕,但它们是休息时间。

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.

从长远来看,有一些建议可以解决 CSS 中的这一缺陷,以便您可以更轻松地将样式应用于列中的所有单元格。你将能够做类似的事情td:nth-col(2) { white-space:nowrap; },它会做你想做的事。

回答by Harun Or Rashid

Just add

只需添加

style="white-space:nowrap;"

Example:

例子:

<table class="blueTable" style="white-space:nowrap;">
   <tr>
      <td>My name is good</td>
    </tr>
 </table>

回答by Dan Breen

<td style="white-space: nowrap">

The nowrapattribute I believe is deprecated. The above is the preferred way.

nowrap我认为该属性已被弃用。以上是首选方式。

回答by Roger Keays

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

在文本中放置不间断空格而不是普通空格。在 Ubuntu 上,我使用 (Compose Key)-space-space 执行此操作。

回答by OlgaMaciaszek

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

要将其应用于整个表格,您可以将其放置在table标签中:

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

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

回答by Matovu Ronald

<table class="blueTable">
  <tr>
     <td>My name is good</td>
   </tr>
</table> 
<style>   
    table.blueTable td,
    table.blueTable th {
        white-space: nowrap;
        /* non-question related further styling */
        border: 1px solid #AAAAAA;
        padding: 3px 2px;
        text-align: left;
    }
</style>

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.

这是使用值为 nowrap 的 white space 属性的示例,bluetable 是表格的类,表格下方是 CSS 样式。