How to make table cell shrink according to the content?

HtmlCssHtml TableWidth

Html Problem Overview


How can I make the table with 2 columns (cells) look like this:

  • First cell is shrink according to the content
  • The other cell fits the rest of the table (wider than both contents together)

Example:

<table style="width: 500px;">
   <tr>
      <td>foo</td>
      <td>bar</td>
   </tr>
</table>

I need the table to look like this:

.___________________________________________________________________________.
| foo | bar            <a lot of space here>                                |
|_____|_____________________________________________________________________|
                           500 px total width

Notice: I don't know the width of "foo" so I cannot set "50px", "10%" or something like that.

Html Solutions


Solution 1 - Html

Set the width to anything near zero, to shrink it, then set the white-space to nowrap. Solved.

td {
width:0.1%;
white-space: nowrap;
} 

Solution 2 - Html

You can set the width of the second cell to 100%

HTML:

<table>
   <tr>
      <td>foo</td>
      <td class="grow">bar</td>
   </tr>
</table>

CSS:

table { width: 500px; }
.grow { width: 100%; }​

Check this fiddle out.

Solution 3 - Html

Set this:

td {
    max-width:100%;
    white-space:nowrap;
}

This will solve your problem.

Solution 4 - Html

If the total size of the table is 500 pixels and will not pass, put td{max-width: 500px;}; If the minimum value is 500 pixels, td {min-width: 500px;}; Look this exemple.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPavel S.View Question on Stackoverflow
Solution 1 - HtmlNat Julian BelzaView Answer on Stackoverflow
Solution 2 - HtmlscumahView Answer on Stackoverflow
Solution 3 - HtmlShubham NigamView Answer on Stackoverflow
Solution 4 - HtmlFilipe ManuelView Answer on Stackoverflow