68

I would like to have a table which in the columns can stretch but I'm having a little trouble with min and max width in css.

It also seems that theres some conflicting answers around how this works:

I would like to have the following

table{
   width:100%;
}
.a, .b, .c
{
    background-color: red;
}
.a
{
    min-width: 10px;
    max-width: 20px;
}
.b
{
    min-width: 40px;
    max-width: 45px;
}
.c
{
}

<table>
    <tr>
        <td class="a">A</td>
        <td class="b">B</td>
        <td class="c">C</td>
    </tr>
</table>

Is there a way of achieving this without javascript (ie constrained stretching of columns with a table)?

below is a table of what actually gets rendered for some different css setups:

enter image description here

Community
  • 1
  • 1
undefined
  • 33,537
  • 22
  • 129
  • 198
  • 1
    Well eh, there's a syntax error in your CSS: `min-width`s are without `:`. Fix that and add an initial `width` to `.a` and `.b` and it seems to work in my Chrome 29. – Passerby Aug 22 '13 at 03:47
  • Edit: `min-width` seem to work in Chrome 29, but `max-width` does not: http://jsfiddle.net/4b3RZ/9/ – Passerby Aug 22 '13 at 04:10
  • @Passerby yes your absolutely right about the :'s (must have been a typo) I've corrected this in the question. Any idea how to make the max-width work? – undefined Aug 22 '13 at 05:36

1 Answers1

113

Tables work differently; sometimes counter-intuitively.

The solution is to use width on the table cells instead of max-width.

Although it may sound like in that case the cells won't shrink below the given width, they will actually.
with no restrictions on c, if you give the table a width of 70px, the widths of a, b and c will come out as 16, 42 and 12 pixels, respectively.
With a table width of 400 pixels, they behave like you say you expect in your grid above.
Only when you try to give the table too small a size (smaller than a.min+b.min+the content of C) will it fail: then the table itself will be wider than specified.

I made a snippet based on your fiddle, in which I removed all the borders and paddings and border-spacing, so you can measure the widths more accurately.

table {
  width: 70px;
}

table, tbody, tr, td {
  margin: 0;
  padding: 0;
  border: 0;
  border-spacing: 0;
}

.a, .c {
  background-color: red;
}

.b {
  background-color: #F77;
}

.a {
  min-width: 10px;
  width: 20px;
  max-width: 20px;
}

.b {
  min-width: 40px;
  width: 45px;
  max-width: 45px;
}

.c {}
<table>
  <tr>
    <td class="a">A</td>
    <td class="b">B</td>
    <td class="c">C</td>
  </tr>
</table>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 4
    Nice, works perfectly! It makes no sense for the cells to shrink below the specified `width` but they do! Thanks. – José Tomás Tocino Jan 28 '14 at 00:54
  • 4
    Note that the above won't work for fixed layout table. – luke Jun 23 '16 at 06:20
  • I'm not getting the answer. Why is A larger than C? Shouldn't both be 14px large? If A is 16px it means it took the 2px from C trying to fill the 20px required by width. That's not the way max-width works. I understood the question (and my problem) as: "don't let the cell grow beyond Npx", at least for max-width. – estani May 27 '20 at 17:56
  • @estani Tables work differently to other elements. In this case, A and B should get 20 and 45 px, respectively, but then C would only get 5 px (left over by subtracting A and B from 70 px). Since the text is wider than 5px, C is enlarged to make the text fit, while A and B are decreased to keep the whole table at 70px. There is no rule that says A and C need to get the same width. – Mr Lister May 28 '20 at 07:01
  • Your problem seems to be different: it sounds like your table is _wider_ than the given widths, can that be right? In that case, each column would initially be assigned their widths, and then all column widths would be increased to make them fit the table. So in your case, width is treated as min-width. Does that answer your question? – Mr Lister May 28 '20 at 07:05
  • indeed, as here (70px is wider than 45px + 20px). I get for a,b,c 16, 43, 11. The given widths seem to be used as some kind of weight. from the 70px c takes a minimum of 11, so a + b get 59 to be distributed "somehow", and the widths seem to affect that distribution in some way. Nothing of it relates to max-width concept of limiting the growth, in my opinion. – estani May 28 '20 at 14:53