简体   繁体   中英

Increase width of a td so that the contents is on a single line

I have a table that looks something like this:

+----------------------+-------------------+
| This text is on the  | This text is on   |
| first column         | the second column |
+----------------------+-------------------+

And I want it to look like this:

+----------------------------------+-----------------------------------+
| This text is on the first column | This text is on the second column |
+----------------------------------+-----------------------------------+

My table is longer than the screen and has many many lines, but this is the functionality I want from it. Is there a CSS property that can make this happen without truncating the text? If not could somebody point me to a javascript example on how to do this?

试试white-space

td { white-space: nowrap; }

If you need wider browser support than white-space: nowrap you can use a pre tag. The css solution is nicer though so only use pre if you really have to:

<tr>
  <td><pre>This is my non wrapping text</pre></td>
  <td><pre>Some more non wrapping text</pre></td>
</tr>

Obviously this will only work for simple html in your table cells, plus pre generally comes with it's own default styling which you can get around with the following in CSS:

td pre {
  font-family: inherit;
  font-size: inherit;
  /* ... and so on */
}

Using inherit can be slightly less supported, so if you can it is best to actually define the style you want directly.

Try:

<table>
  <tr>
    <td nowrap>Look ma</td>
  </tr>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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