129
<table>
  <tr>
    <td>Test</td>
    <td>A long string blah blah blah</td>
  </tr>
</table>

<style>
  td {
    max-width: 67%;
  }
</style>

The above does not work. How can I set the max-width of a table cell using percentages?

Pluto
  • 4,177
  • 1
  • 3
  • 25
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • 1
    Please expand upon _"does not work"_. – Sparky Dec 11 '11 at 16:58
  • 1
    You have to make sure the PARENT of your td has a specific width (be it that you pass width: 100%; all the way from body to your td, or you could give the parent (here: tr) 1000px or whatever you prefer. When using %% in css it always uses the exact px defined in the parent and then transforms the px into %% for the children, as they have a size _relative_ to their parent. – Philipp Meissner Sep 17 '15 at 12:56
  • For the record, max-width now works in Safari and Chrome. I am not sure how long it has been supported for. – Jay Aug 30 '19 at 06:36

4 Answers4

144

Old question I know, but this is now possible using the css property table-layout: fixed on the table tag. Answer below from this question CSS percentage width and text-overflow in a table cell

This is easily done by using table-layout: fixed, but a little tricky because not many people know about this CSS property.

table {
  width: 100%;
  table-layout: fixed;
}

See it in action at the updated fiddle here: http://jsfiddle.net/Fm5bM/4/

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
superphonic
  • 7,954
  • 6
  • 30
  • 63
  • 2
    Thanks! I was afraid fixed means it won't recalculate the width of cells which don't have any width set, but luckily that is not the case. – fassl Jul 04 '15 at 18:38
  • @shanzilla If you have no-wrap on the cells, or the text is one unbroken string that's longer, then yes it will spill over. But using auto layout has similar and different problems as well. Just depends what you need. You can ellipses it though with `text-overflow: ellipsis;` and can also add a scroll bar. CSS-Tricks as a great write up: https://css-tricks.com/fixing-tables-long-strings/ – superphonic Mar 19 '17 at 18:51
  • I was able to use max-width on my element without table-layout:fixed. – Kerry Johnson Jul 08 '17 at 16:15
  • 6
    In your jsfiddle if I remove the width properties the max-width still does not work so not sure how this solves the problem. – frezq Jul 23 '17 at 13:49
  • @frezq The table needs to have a width of some sort for a percentage `max-width` to work. Having a 100% table width means it will be whatever size the container is, and the cell will always be at most 67% of it. The table is then also responsive which is a plus. Not sure how this solution wouldn't help you. – superphonic Jul 23 '17 at 18:30
  • @superphonic I didn't remove the`width` property of the table, just the `width` on the `td`s. `max-width` no longer works in that case. – frezq Jul 23 '17 at 22:46
  • @frezq Seems to work for me http://jsfiddle.net/w3f8ey22/ - The cell with `An incredibly long name for a product intended for testing` is still 60% of the table. – superphonic Jul 24 '17 at 09:04
  • 7
    @superphonic I think you're confused. If you remove the `max-width` in that jsfiddle, the width will remain the same so it's not the `max-width` that sets it. It might be clearer if you replace it with a `max-width: 10%` for the first `td`... you'll see that it does not change. http://jsfiddle.net/w3f8ey22/1/ – frezq Jul 24 '17 at 21:07
  • 9
    table-layout: fixed does not solve the OP issue. It does not magically make 'min-width' work on a table cell. Both referenced fiddles have not difference in display if table-layout:fixed is removed. – cmerriman Aug 04 '17 at 13:51
  • 6
    this answer is misleading as it is not related to the question – Zoltán Süle Oct 16 '18 at 11:04
84

According to the definition of max-width in the CSS 2.1 spec, “the effect of 'min-width' and 'max-width' on tables, inline tables, table cells, table columns, and column groups is undefined.” So you cannot directly set max-width on a td element.

If you just want the second column to take up at most 67%, then you can set the width (which is in effect minimum width, for table cells) to 33%, e.g. in the example case

td:first-child { width: 33% ;}

Setting that for both columns won’t work that well, since it tends to make browsers give the columns equal width.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • Actually max-width works in Safari and Chrome now. I am not sure how long it has been supported for. – Jay Aug 30 '19 at 06:36
  • @Jay, if it works, there are unknown for me conditions when it works. – Takeshi Tokugawa YD Dec 02 '20 at 09:19
  • 2
    @TakeshiTokugawaYD, it seems that `max-width` for `td` is respected (to some degree) if `width` is also set AND `width` and `max-width` both have either length units or percentage units. With `table-layout:fixed` it is respected without regards to the content. – Superole Jan 26 '22 at 11:56
  • Still true in the [CSS 2.2](https://www.w3.org/TR/CSS22/visudet.html#min-max-widths) Working draft – Simon Apr 28 '22 at 06:59
9

I know this is literally a year later, but I figured I'd share. I was trying to do the same thing and came across this solution that worked for me. We set a max width for the entire table, then worked with the cell sizes for the desired effect.

Put the table in its own div, then set the width, min-width, and/or max-width of the div as desired for the entire table. Then, you can work and set width and min-widths for other cells, and max width for the div effectively working around and backwards to achieve the max width we wanted.

#tablediv {
    width:90%;
    min-width:800px
    max-width:1500px;
}

.tdleft {
    width:20%;
    min-width:200px;
}
<div id="tablediv">
  <table width="100%" border="1">
    <tr>
      <td class="tdleft">Test</td>
      <td>A long string blah blah blah</td>
    </tr>
  </table>
</div>

Admittedly, this does not give you a "max" width of a cell per se, but it does allow some control that might work in-lieu of such an option. Not sure if it will work for your needs. I know it worked for our situation where we want the navigation side in the page to scale up and down to a point but for all the wide screens these days.

Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
Damon
  • 269
  • 3
  • 10
1

the percent should be relative to an absolute size, try this :

table {
  width:200px;
}

td {
  width:65%;
  border:1px solid black;
}
<table>
  <tr>
    <td>Testasdas 3123 1 dasd as da</td>
    <td>A long string blah blah blah</td>
  </tr>
</table>
    
Matthew Haugen
  • 12,916
  • 5
  • 38
  • 54
dov.amir
  • 11,489
  • 7
  • 45
  • 51