40

I have a table containing cells with text of various lengths. It is essential that all of the table cells are of the same width. If this means truncating long words or forcing a break in long words then that's OK.

I cannot figure out any way of getting this to work.

This is for an internal client application so needs to work in IE6 and IE7 only.

An example page is below. The cell containing onereallylongword is the offending one.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        td { width: 30px; }
    </style>
</head>
<body>
    <table border="2">
        <tr>
            <td>word</td>
            <td>two words</td>
            <td>onereallylongword</td>
        </tr>
    </table>
</body>
</html>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
  • Essentially a duplicate of [http://stackoverflow.com/questions/416401/using-css-to-create-table-cells-of-a-specific-width-with-no-word-wrapping](http://stackoverflow.com/questions/416401/using-css-to-create-table-cells-of-a-specific-width-with-no-word-wrapping) Sadly, the best answer I'm aware of is wrapping the `` contents in a `
    ` and applying the fixed width and overflow to that.
    – annakata Jan 15 '09 at 13:08

7 Answers7

38

As long as you fix the width of the table itself and set the table-layout property, this is pretty simple :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        td { width: 30px; overflow: hidden; }
        table { width : 90px; table-layout: fixed; }
    </style>
</head>
<body>

    <table border="2">
        <tr>
            <td>word</td>
            <td>two words</td>
            <td>onereallylongword</td>

        </tr>
    </table>
</body>
</html>

I've tested this in IE6 and 7 and it seems to work fine.

inferis
  • 1,303
  • 1
  • 9
  • 15
  • That works well - however in my scenario the number of columns is variable and it will be complex for me to calculate the total width of the table. – Richard Ev Jan 15 '09 at 14:51
  • 1
    @RichardEv As far as I have checked with Chrome, the width the be specified for the table can be anything. So, you can just set it as `0`. – sawa Feb 21 '12 at 07:09
  • Instead of using `overflow: hidden` for `` I'm using `word-wrap: break-word`. Better when you don't want hide the overflow – instead Apr 26 '14 at 11:37
  • This shit works... Specially that table-layout:fixed. I had tried all the other stuff none seem to work. – Saras Arya Sep 30 '15 at 07:00
21

If you want to the long text wrapped properly in new lines then in your table id call use a css property table-layout:fixed; otherwise simply css can't break the long text in new lines.

Katie
  • 371
  • 4
  • 4
18

Stack Overflow has solved a similar problem with long lines of code by using a DIV and having overflow-x:auto. CSS can't break up words for you.

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
12

Try this:

text-overflow: ellipsis; 
overflow: hidden; 
white-space:nowrap;
Nikola K.
  • 7,093
  • 13
  • 31
  • 39
Anupam
  • 200
  • 1
  • 4
3
<style type="text/css">
td { word-wrap: break-word;max-width:50px; }            
</style>
Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
Mukund Thakkar
  • 1,225
  • 14
  • 19
2

I realize you needed a solution for IE6/7 but I'm just throwing this out for anyone else.

If you can't use table-layout: fixed and you don't care about IE < 9 this works for all browsers.

td {
    overflow-x: hidden;
    overflow-y: hidden;
    white-space: nowrap;
    max-width: 30px;
}
shrimpwagon
  • 867
  • 12
  • 23
-7

try td {background-color:white}

It just worked for a column I didn't want to get trampled by a previous column's long text.

kapa
  • 77,694
  • 21
  • 158
  • 175
  • I'm not sure that would work - the previous column's long text will surely cause the width of that column to expand. Could you edit your reply to contain an example HTML snipped to illustrate? – Richard Ev Apr 14 '09 at 09:27
  • 2
    I don't see how setting background-color to white could help here ? – dwarfy Apr 28 '11 at 08:35