Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent text in a table cell from wrapping

Tags:

html

css

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:

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

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?

like image 881
pkaeding Avatar asked Nov 18 '08 21:11

pkaeding


People also ask

How do I turn off text wrapping in a table in word?

3. Click the “Wrap Text” button in the Alignment group to cancel the word-wrapping option. This button is marked with two rectangles and a curved arrow.

How do I stop excel from wrapping text?

Select the cells or could be the entire sheet where you want auto-wrap text to be disabled > Right-click > Format cells > Click on "Alignment" tab > Under text control, remove the checkmark from the "wrap text" option.

How do I stop text wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).


1 Answers

Have a look at the white-space property, used like this:

th {     white-space: nowrap; } 

This will force the contents of <th> to display on one line.

From linked page, here are the various options for 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.

like image 166
Owen Avatar answered Oct 10 '22 07:10

Owen