95

One of the columns in my table can contain a long text without whitespaces. How can I limit its width to, say, 150px? I don't want it to be 150px always (if it's empty it should be narrow), but if there is a long text I want it to be limited with 150px and the text to be wrapped.

Here is a test example: http://jsfiddle.net/Kh378/ (let's limit the 3rd column).

Thank you in advance.

Update:
Setting this styles:

word-wrap: break-word;
max-width: 150px;

does not work in IE8 (tested on different computers) and I suppose it does not work in any version of IE.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
nightcoder
  • 13,149
  • 16
  • 64
  • 72

7 Answers7

91

Updated

On the table's td and th tags I added:

word-wrap: break-word;
max-width: 150px;

Not fully compatible in every browser but a start.

Brandon
  • 1,997
  • 3
  • 24
  • 33
15

You need to use width instead.

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Age</th>                
            <th style="width: 150px;"></th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

But just in the <thead> section.

Carlos Toledo
  • 2,519
  • 23
  • 23
8

You should be able to style your column with:

max-width: 150px; 
word-wrap: break-word;

Note that word-wrap is supported in IE 5.5+, Firefox 3.5+, and WebKit browsers (Chrome and Safari).

Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
isNaN1247
  • 17,793
  • 12
  • 71
  • 118
  • See this in more detail, especially the max-width and (currently) the problems there, in the similar/equivalent solution this page http://stackoverflow.com/a/24620364/2255628 – Destiny Architect Jul 07 '14 at 21:54
5

After some experimentation in Chrome I came to the following:

  • if your table has a <th> that should have either a "width" or a "max-width" set
  • the <td>'s must have set a "max-width" and "word-wrap: break-word;"

If you use a "width" in the <th> its value will be used. If you use a "max-width" in the <th> the "max-width" value of the <td>'s is used instead.

So this functionality is quite complex. And I didn't even study tables without headers or without long strings that need a "break-word".

user2587656
  • 309
  • 3
  • 5
2

there is an option to simulate max-width:

simulateMaxWidth: function() {
    this.$el.find('th').css('width', ''); // clear all the width values for every header cell
    this.$el.find('th').each(function(ind, th) {
        var el = $(th);
        var maxWidth = el.attr('max-width');
        if (maxWidth && el.width() > maxWidth) {
            el.css('width', maxWidth + 'px');
        }
    });
}

this function can be called on $(window).on('resize', ...) multiple times

this.$el

represents parent element, in my case I have marionette

for those th elements which should have max-width, there should be a max-width attribute like:

<th max-width="120">
user2846569
  • 2,752
  • 2
  • 23
  • 24
1

1) Specify width for each column in <th> according to your need.

2) Add word wrap for each <td> in <tr> of the <table>.

word-wrap: break-word;
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
Amay Kulkarni
  • 828
  • 13
  • 16
-6

it's very easy

You could add attributes like these

max-width

max-height

They can be set in html as an attribute or in CSS as a style

Kano
  • 5
  • 1