Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: how to make the widths of the columns as wide as the longest text

Tags:

css

I open this post after I looked at this post without success: How to set table column widths to the longest value, excluding header

I want to draw a table, and I want that the width of each column will be compatible to the longest text in this column. For example, if the following values lie in column A: "abc", "abcd", "abcde", the width of column A will be 5 characters long.

I tried:

td {
  white-space: nowrap;
  overflow: hidden;
}

How can I do it?

like image 723
CrazySynthax Avatar asked Oct 24 '25 19:10

CrazySynthax


1 Answers

I think that's the default behavior of tables. Have made a simple table that just behaves as you want it (refer this JSFiddle). The column width is equal to the longest text in them.

<table>
  <thead>
    <th>Column A</th>
     <th>Column B</th>
  </thead>
  <tbody>
    <tr>
      <td>abc</td> <td>abcdefghijklm</td>
    </tr>
    <tr>
       <td>abcdefghijklmno ddsp</td>  <td>ab</td>
    </tr>
     <tr>
       <td>ab</td>  <td>ab</td>
    </tr>
  </tbody>
</table>

Not sure if you wanted to ask something else ?

like image 69
Himanshu Arora Avatar answered Oct 27 '25 12:10

Himanshu Arora



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!