Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force table column widths to always be fixed regardless of contents

Tags:

html

css

I have an html table with table-layout: fixed and a td with a set width. The column still expands to hold the contents of text that doesn't contain a space. Is there a way to fix this other than wrapping the contents of each td in a div?

Example: http://jsfiddle.net/6p9K3/29/

<table>     <tbody>         <tr>             <td style="width: 50px;">Test</td>             <td>Testing 1123455</td>         </tr><tr>             <td style="width: 50px;">AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</td>             <td>B</td>         </tr>     </tbody> </table>  table {     table-layout: fixed; }  td {     border: 1px solid green;     overflow: hidden; } 

In the example, you can see that the column with AAAAAAAAAAAA... expands despite being explicitly set to 50px wide.

like image 368
datadamnation Avatar asked Mar 27 '13 20:03

datadamnation


People also ask

How do I keep my column width fixed in HTML?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width for the columns can be in pixels, or percentage.

How do you set a fixed column width?

Select your table. On the Layout tab, in the Cell Size group, click AutoFit. Click Fixed Column Width.

How do you set the fixed width of a TD table?

By using CSS, the styling of HTML elements is easy to modify. To fix the width of td tag the nth-child CSS is used to set the property of specific columns(determined by the value of n) in each row of the table.

How do you stop a table from expanding?

To prevent cells (rows) from expanding vertically, you have to set a fixed height for table rows. Select the relevant rows and, on the Table Tools Layout tab, click Properties. On the Row tab, select "Specify height" and then choose "Exactly" for "Row height is." Specify the desired amount.


2 Answers

Specify the width of the table:

table {     table-layout: fixed;     width: 100px; } 

See jsFiddle

like image 70
Arie Xiao Avatar answered Sep 25 '22 23:09

Arie Xiao


Try looking into the following CSS:

word-wrap:break-word; 

Web browsers should not break-up "words" by default so what you are experiencing is normal behaviour of a browser. However you can override this with the word-wrap CSS directive.

You would need to set a width on the overall table then a width on the columns. "width:100%;" should also be OK depending on your requirements.

Using word-wrap may not be what you want however it is useful for showing all of the data without deforming the layout.

like image 23
Drew Anderson Avatar answered Sep 24 '22 23:09

Drew Anderson