Hello everybody.
I am working on a PHP program that shows some information in table cells..
However, the width of each cell is automatically widen (it becomes wider)..
How can I make the width of a cell fit to its content so that if it has 5 chars, then its width will fit to about only 5 chars, and so on and so forth.
Does anybody know?
This is an example:

<table border="0" cellspacing="0" cellpadding="0" width="100%">
<tr><td>John Smith</td><td>36</td><td>98</td></tr>
<tr><td>Adam Smith</td><td>35</td><td>85</td></tr>
<tr><td>Essam Khaleel Adams</td><td>35</td><td>90</td></tr>

So, in this code, how can I make the width of the "Name" cell fit to its largest content (which is the last cell in my example)?Thanks in advance.

7 Years
Discussion Span
Last Post by Pro2000

tables dont work that way
tables present tabular data in tabular form, SQUARE
the column is the width of the largest cell
the row is the height of the largest cell

If you dont want a square layout, dont use tables


Sorry, I do want a square layout, but what I mean is that I want the width of a cell, not the width of a column, to fit "tightly".


to the limits of the table size which in this case is set to full window width (100%)
if you want the whole table to adjust, remove the width statement from the table declaration and each column will adjust to its widest content
tables do not work that way, the column is the width of the widest cell in the column, the row is the height of the highest cell in the row,
the table is a collection of squares, even if the cell in column1 row3 is 3characters wide
column one, and therefore all the cells within in will be the width of the cell in column1 row4 which is 40 characters wide
the column or row fits 'tightly' around the largest column,

examine style text-align:right; on the columns containing the names

Edited by almostbob: n/a


Thank you very much, but do you have any solution for putting something similar to a table in 100% width?? I only want one column (not one cell.. sorry for being mistaken) to fit, not all the columns.. Can I do it??


thats easier, I thought you wanted them all to adjust
to make one column absorb all the width of the 100% make the first cell in that column <td width='*'>content</td> it should then occupy the entire table except for the width actually used by the other cells

Votes + Comments
Nice post @almostbox. You are master with table. It is appreciate for me too. I am still stupid in table. :)

Thank you, but I tried it and it didn't work..
Nevertheless, I found the solution.. I distributed the width of the columns for whose with I don't care, and I kept the "Name" column with no width specified and added the nowrap tag... So it becomes:

<td nowrap>

Thank you anyway.

Edited by Pro2000: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.