Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why TD width is not working or not followed?

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.

like image 277
He Hui Avatar asked Feb 08 '13 07:02

He Hui


People also ask

How fix TD table width?

Using width attribute: The <td> tag has width attribute to control the width of a particular column. By assigning a numeric value to this attribute between 0 to 100 in terms of percentage(or you can use pixel format). We can restrict the column width up to that much percentage of the table's total width.

What determines td width?

HTML | <td> width AttributeIf width attribute is not set then it takes default width according to content. Attribute Values: pixels: It sets the width of table in terms of pixels. %: It sets the width of table in terms of percentage (%).

How do I make TD the same width?

Using table-layout: fixed as a property for table and width: calc(100%/3); for td (assuming there are 3 td 's). With these two properties set, the table cells will be equal in size.

How do I change the width of a TD in CSS?

Just add <div> tag inside <td> or <th> define width inside <div> . This will help you. Nothing else works.


1 Answers

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.

like image 107
He Hui Avatar answered Sep 19 '22 21:09

He Hui