Tim Huffam

Dotting the I and crossing the T of I.T.

  Home  |   Contact  |   Syndication    |   Login
  129 Posts | 0 Stories | 874 Comments | 677 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

To achieve the same results as a TD nowrap (<td nowrap="true">) using cascading style sheets, use the following:

white-space: nowrap;
 
This style attribute only applies at the TD element level ie needs to be applied to each TD, ie not the the TR or TBODY.  If you're desparate to not apply it to each TD,  then you can get away with only applying it to the TD's of the first row - but this is only effective if applied to the cells containing thelongest data (so IMHO is a half-baked approach - ie don't do it).
 
HTH
Tim
posted on Sunday, April 16, 2006 4:48 PM

Feedback

# re: CSS nowrap alternative 1/2/2007 9:37 PM Thomas Beaucourt
"white-space:nowrap;" works fine.
Please note that replacing white spaces by "&nbsp;" does the job, too. "nbsp" means "Non Breakable SPace".

# It runs in each cell 1/24/2007 2:28 PM caos30
Thanks Tim about your post.
But i've tried this writing this and works fine too, in every cell!!! ;o)

HTML----------------

<table>
<tr class=classtr>
<td>
bla bla bla...
</td>
<td>
bla bla bla...
</td>
</tr>
</table>

CSS-------------

.classtr td{
white-space:nowrap;
}

# re: CSS nowrap alternative 2/15/2007 10:44 AM rfutami
To apply this style to all td's in a table i would use inheritance. define a table id and set its td's to this style. This way you don't need to set the class of each td cell in a table
<table id="mytable">
<tr><td></td><td></td></tr>
</table>
table#mytable tr td{white-space:nowrap}

# re: CSS nowrap alternative 5/2/2007 7:00 PM Bill Meier
Note that replacing space with &nbsp; does NOT do the job, as the browser will wrap the text after other characters, such as a after a dash...

Yes, there is a non-breaking dash too, but how long do you want to fiddle with incomplete wordarounds?

# re: CSS nowrap alternative 5/17/2007 5:19 AM dizyart
Another alternative would be

<table>
<tr class="nowrap">
<td></td>
<td></td>
</tr>
</table>

CSS:

tr.nowrap { white-space:nowrap; }
td { white-space:inherit; }


This way only the TD's inside a NOWRAP classed TR are no-wrap and also the TR itself is nowrap.
Although I can't find a reason why it whould be - just showing off, I guess..


# re: CSS nowrap alternative 6/22/2007 11:54 PM Adam
To show of the ofshower:

CSS:
tr.nowrap td {white-space:nowrap;}

# re: CSS nowrap alternative 11/21/2007 10:45 AM GregTheGeek
To the one-upper of the &amp;quot;off-shower&amp;quot;:

CSS:
[code]
TR.nowrap TD, TH {white-space: nowrap;}
[/code]

This way, if you have a TH that is longer than your data, it does not wrap either.

If you're going to one-up someone else's code, make sure your spelling is dead on, too. :-) (just some light-hearted ribbing, not trying to start a flame war)

# re: CSS nowrap alternative 11/21/2007 10:48 AM GregTheGeek
Oh, nice. In the process of trying to post (and show off just a little) my quotes get messed up. <wipes the egg off his face>



# re: CSS nowrap alternative 12/17/2007 5:56 PM Jim Stanley
Looks good. Any advice on how to implement nowrap with a list item? I'm using a tree CSS I found on the internet, and whitespace:nowrap doesn't seem to work on ul or li.

Advice?

# re: CSS nowrap alternative 12/23/2007 1:31 AM Mark
Actually oneupmanship Greg, you're all wrong.

None of your TH elements will wrap.

What you meant to say, but failed to is:

TR.nowrap TD, TR.nowrap TH {white-space: nowrap;}

Before you engage in light-hearted ribbing (yeah, right), you ought to know what you're talking about.


# re: CSS nowrap alternative 2/15/2008 7:20 AM erewrewrew
ewrewrwe

# re: CSS nowrap alternative - IE issue 5/23/2008 7:39 PM narcis
I've used this rule for every TD that I needed and it worked fine on FireFox, but in IE the text is wrapped.

Do you know of this issue ? Is there a nice workaround for it ?

Thanks !

# re: CSS nowrap alternative 6/10/2008 9:47 AM herenvardo
An important issue about the "white-space: nowrap;" style: if you apply it to a parent element that contains some children elements, the parent element itself won't wrap, but its children might wrap, which will look exactly as if the parent was wrapped. To make it clearer, if you have:
<td style="white-space: nowrap;">
<h2>Main menu</h2>
<ul><li>Home</li>
<li>About us</li>
<li>Forum</li></ul>
</td>
The <td> itself is not wrapping, but the rendering engine might wrap any of the <li> elements inside. When this happens, it will look exactly the same as if the <td> itself had wrapped, which you probably don't want.
The simplest way to achieve this is using a CSS class and putting a bit of creativity in the selectors:
In your HTML:
<td class="nowrap"> ... </td>
In your CSS:
.nowrap * { white-space: nowrap; }
This selector will match (and apply the nowrap rule to) any element that has the nowrap class **and** any of its descendants (either direct or indirect, like the td > ul > li in the example above). I'm not sure how well will browsers handle this kind of selectors: my site using this is working fine in FireFox. Haven't even checked on IE (my site forsakes IE anyway because it doesn't want to accept a "application/xhtml+xml" content-type and I don't want to send my content under a fake header jusr for one browser).

# re: CSS nowrap alternative 6/10/2008 9:52 AM herenvardo
Woops: part of the HTML I used in the example got parsed ^^; To be more exact, the example anchor tags are appearing as actual links (oh! and one of them even works LOL!) Fortunatelly, the <td> and <li> items are there, which are the important ones :P

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 5 and 8 and type the answer here: