How to make table cell shrink according to the content?

前端 未结 4 573
野性不改
野性不改 2021-02-02 05:25

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 (w
相关标签:
4条回答
  • 2021-02-02 06:01

    I know this was asked years ago. The OP probably have solved it by now. But this could still be helpful to someone. So I'm posting what worked for me. Here's what I did with mine. I 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;
    } 
    
    0 讨论(0)
  • 2021-02-02 06:06

    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.

    0 讨论(0)
  • 2021-02-02 06:16

    Set this:

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

    This will solve your problem.

    0 讨论(0)
  • 2021-02-02 06:19

    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.

    0 讨论(0)
提交回复
热议问题