tags:

views:

190

answers:

3

I have something like this:

<table>
  <tr>
    <td><nobr>hello</nobr></td>
    <td>this column can contain a lot of text, for some rows</td>
    <td><nobr>world</nobr></td>
    <td><nobr>hello world</nobr></td>
  </tr>
  ... more rows to come
</table>

Basically 3 of the columns have very short text, and I want them to be <nobr>. The other one can have very long text, and I want it to take all the remaining space. But what happens is that the table gets bigger than the whole browser window to accommodate the big column. If the text is really big it will eventually break, but it still gets quite a bit outside the window.

I tried setting width, max-width, but no luck. What am I doing wrong?

+1  A: 

I just threw this into my browser (tried both IE8 and Firefox). It stayed well within the Browser window:

<html>
 <body>
  <table>
   <tr>
    <td><nobr>hello</nobr></td>
    <td>this column can contain a lot of text, for some rows.</td>
    <td><nobr>world</nobr></td>
    <td><nobr>hello world</nobr></td>
   </tr>
  </table>
 </body>
</html>

Otherwise you might want to try tricks like width: 100% or margin: 0 auto; on the table.

Zyphrax
+3  A: 

Your example definitely behaves well as Zyphrax says.

The problem you report can only happen if, in the long column, there is a word which is very large or the content has no whitespace in it. Is that the case? May be you are using &nbsp; instead of spaces, and it is preventing the normal wrap that browsers do automatically.

If the text in the column is not wrapping at all, mostly you are using &nbsp; or you forgot to close a <nobr> somewhere.

Not sure if that helps.

Narayan Raman
You're right. One of my rows had a huge "word" (no spaces). I hadn't seen that, since it was far down in my table. Thanks!
ionut bizau
+1  A: 

You shouldn't be using the nobr tag, it is not a valid HTML tag. Use "white-space: nowrap" CSS instead.

eg.

<td style="white-space:nowrap">your long text</td>
Dan Diplo