tags:

views:

496

answers:

1
<td>
<div id="test">...</div>
</td>
<td>
.....
</td>

I can see from firebug that td is about 40px wider than the "test" div(width+border+margin all included),but there is no css style that do this(no setting of width,padding)!

Why is the "td" not as wide as div here?

I'm now hardcoding the td to9 be as wide as "test" div,but feel uncomfortable.

+2  A: 

Without the rest of your code, I'm going out on a limb here, but the <td> will auto size based on the table width / number of columns in the table. If your table does not have an explicit width assigned, then it will expand to 100% width of its container or parent element. You can feel comfortable avoiding this by setting explicit width on the table, or table cells as needed.

In the following scenario, each column will autosize to tablewidth / number of columns or in this case 400 / 4 = 100. So each column will be 100px wide.

<table style="width:400px">
<td></td>
<td></td>
<td></td>
<td></td>
</table>

Depending on the browser, there are two possible results in this scenario. 1. The table will expand to be 100% the width of its parent, or 600px here. That means that each column will then auto size to 150px wide. 2. The table and columns may expand to the width of the widest child element in its column group.

<body style="width: 600px"> <!-- Could be a div or other element here -->
<table>
<td></td>
<td></td>
<td></td>
<td></td>
</table>
</body>

The WC3 Spec can be found here and provides some great detail on how browsers are supposed to implement the HTML 4.01 spec in regards to tables.

PortageMonkey
without specifying td width,how is the autoresize done?I've somehow triggered an autoresize that's not expected by me! I mean the td(s) are assigned different width according to its content.But how is that done?
Shore
The browser manages the auto-sizing of table columns per the WC3 recommendations - the spec can be found at http://www.w3.org/TR/html401/struct/tables.html#h-11.2.4.4
PortageMonkey