简体   繁体   English

如何防止表格单元格中的文本换行

[英]How to prevent text in a table cell from wrapping

Does anyone know how I can prevent the text in a table cell from wrapping?有谁知道如何防止表格单元格中的文本换行? This is for the header of a table, and the heading is a lot longer than the data under it, but I need it to display on only one line.这是一个表格的标题,标题比它下面的数据长很多,但我需要它只显示在一行上。 It is okay if the column is very wide.如果列很宽也没关系。

The HTML of my (simplified) table looks like this:我的(简化的)表格的 HTML 如下所示:

<table>
  <thead>
    <tr>
      <th>
        <div>Really long column heading</div>
      </th>
      <th>
        <div>Really long column heading</div>
      </th>
      <th>
        <div>Really long column heading</div>
      </th>
      <th>
        <div>Really long column heading</div>
      </th>
      <th>
        <div>Really long column heading</div>
      </th>
      <th>
        <div>Really long column heading</div>
      </th>
      <th>
        <div>Really long column heading</div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <div>data</div>
      </td>
      <td>
        <div>data</div>
      </td>
      <td>
        <div>data</div>
      </td>
      <td>
        <div>data</div>
      </td>
      <td>
        <div>data</div>
      </td>
      <td>
        <div>data</div>
      </td>
      <td>
        <div>data</div>
      </td>
    </tr>
  </tbody>
</table>

The heading itself is wrapped in a div inside the th tag for reasons pertaining to the javascript on the page.由于与页面上的 javascript 有关的原因,标题本身被包裹在th标记内的 div 中。

The table is coming out with the headings wrapping onto multiple lines.该表格的标题包含多行。 This seems to only happen when the table is sufficiently wide, as the browser is trying to avoid horizontal scrolling.这似乎只在表格足够宽时发生,因为浏览器试图避免水平滚动。 In my case, though, I want horizontal scrolling.不过,就我而言,我想要水平滚动。

Any ideas?有任何想法吗?

Have a look at the white-space property, used like this:看看white-space属性,使用如下:

th {
    white-space: nowrap;
}

This will force the contents of <th> to display on one line.这将强制<th>的内容显示在一行上。

From linked page, here are the various options for white-space :从链接页面,这里是white-space的各种选项:

normal普通的
This value directs user agents to collapse sequences of white space, and break lines as necessary to fill line boxes.该值指示用户代理折叠空白序列,并根据需要换行以填充行框。

pre
This value prevents user agents from collapsing sequences of white space.此值可防止用户代理折叠空白序列。 Lines are only broken at preserved newline characters.仅在保留的换行符处换行。

nowrap无包装
This value collapses white space as for 'normal', but suppresses line breaks within text.该值会像“正常”一样折叠空白,但会抑制文本中的换行符。

pre-wrap预包装
This value prevents user agents from collapsing sequences of white space.此值可防止用户代理折叠空白序列。 Lines are broken at preserved newline characters, and as necessary to fill line boxes.在保留的换行符处换行,并根据需要填充行框。

pre-line前线
This value directs user agents to collapse sequences of white space.该值指示用户代理折叠空白序列。 Lines are broken at preserved newline characters, and as necessary to fill line boxes.在保留的换行符处换行,并根据需要填充行框。

<th nowrap="nowrap">

or或者

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

or或者

<th class="nowrap">
<style type="text/css">
.nowrap { white-space: nowrap; }
</style>

There are at least two ways to do it:至少有两种方法可以做到:

Use nowrap attribute inside the "td" tag:在“td”标签内使用 nowrap 属性:

<th nowrap="nowrap">Really long column heading</th>

Use non-breakable spaces between your words:在单词之间使用不可破坏的空格:

<th>Really&nbsp;long&nbsp;column&nbsp;heading</th>

I came to this question needing to prevent text wrapping at the hyphen.我遇到了这个问题,需要防止在连字符处换行。

This is how I did it:我是这样做的:

<td><nobr>Table Text</nobr></td>

Reference:参考:

How to prevent line break at hyphens on all browsers 如何防止在所有浏览器上的连字符处换行

For Use with React / Material UI用于 React / Material UI

In case you're here wondering how this works for Material UI when building in React, here's how you add this to your <TableHead> Component:如果您在这里想知道在 React 中构建 Material UI 时这是如何工作的,那么您可以将其添加到<TableHead>组件中:

<TableHead style={{ whiteSpace: 'nowrap'}}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM