Question

Original question: Does HTML <table> have a default width?

Recently someone asked a question somewhere along these lines, and got me wondering.

Take this for example.

http://jsfiddle.net/rqmNY/1/

In this fiddle, if you were to check its width (I'm using inspect element from chrome), it shows 100px, working as intended.

Lets add a few more "td"s in, and we shall see that the "td:100px" css is being ignored.

http://jsfiddle.net/rqmNY/2/

As you can see, now it's 83px instead of 100px as originally intended.

But let's say, I move back to fewer TD's (7), and I add in a wider width to each TD element (500px), the result is that the width of the td gets stuck at 119px.

http://jsfiddle.net/rqmNY/6/

And finally, let's say I have a table of 2000px width, and td of 100px width, and many td elements. http://jsfiddle.net/rqmNY/7/

Now the table width overrides the TD width, and expands the td's width to 222px.

Can anyone explain this behavior?

p.s. Note that in all cases, inspect element tool tells me that the width is always corresponding to the css, it's just the final result not showing correctly.

Was it helpful?

Solution 2

I highly believe the answer to this question is such:

The priority of widths that will affect the TD is

  1. Table Width

  2. Parent Element Width (and if none, Viewport)

  3. Element(TD) Width.

Hence if the table width is set, the TD's will ALWAYS adjust to the width of the table. However, if the width is unset, the "main" width will be the true width of the viewport. Unless the CSS code states otherwise, this holds true. And only when the total width of the TD's is smaller than that of the viewport, the elemental width will be taken into account.

Edit

  1. Table width will always override TD width.

  2. Stated TD width will only be followed until it exceeds viewport width, and viewport width will be taken as priority.

OTHER TIPS

Have you tried adding display:inline-block to your TD CSS? That forces the browser to not ignore your TD width.

Actually the table width depends on the cell width when you do not specify the table width. But when you specify the table width it will ignore the td width. Look at the following example:

<table>
  <tr>
    <td>Column 1</td>
  </tr>
  <tr>
    <td>Column 2</td>
  </tr>
</table>

If you use

td { 
  width:500px;
}

then the table width will be 1000px.

But if you use

table {
  width:500px;
} 
td { 
  width:500px;
}

it will ignore the <td> width and the table width will be 500px.

According to the w3 Docs Here It says "In the absence of any width specification, table width is determined by the user agent."

What I can think of it is td width is always dependent on the table width. If you specify it or not. If you have a table with width 500px and 2 TDs with width 200px each. Now after adding these 2 TDs in table there are 100px remaining to accommodate so 50px each are added to both the TDs overwriting the original width property. See this link http://jsfiddle.net/rqmNY/7/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top