views:

33

answers:

2

In the following table, the first cell content is dynamically generated. The long text in the second cell causes the table width to grow beyond what is needed to fit the first cell.

How can I get the bottom cell to wrap to another row, instead of widening the table?
I can't use a fixed width for the second cell, because the first cell content is not known until run time.

<table border="1">
  <tr><td>name: <input type="text"></input></td></tr>
  <tr><td>blah blah blah blah blah blah blah blah blah blah</td></tr>
</table> 
+1  A: 

First of all, do not close you input like you do, do the following:

<input type="text" />

Second: Just give the table a fixed width:

<table border="1" style="width: 400px;">
  <tr><td>name: <input type="text" /></td></tr>
  <tr><td>blah blah blah blah blah blah blah blah blah blah</td></tr>
</table> 

Ah and finally third: Do not use tables for layouts. Even forms can be done without a table.

Kau-Boy
First, the input is just an example. It represents some dynamically generated content.Second, as I said I can't give a fixed width (because of the first).Finally, I don't use tables for layout. My framework (GWT) does.
Hagai Cibulski
The suggestion was to use a fixed width on the table, not on the table cell. This will cause the text to wrap rather than expand it's container.
roryf
I would like the table width to fit the width needed by the components in the first table cell.
Hagai Cibulski
I don't think that you want your table to grow infinite. So setting a fixed size is a good idea. You could also set a min-width (and max-width), but than the second row might increase the width to the max-width. Using only a max-width the second row might increase the width, but at a specific width, the content of the second row will be wrapped.
Kau-Boy
The table won't grow infinite, only according to the components in the first row (which vary in each specific instance of this generic table). However max-width is not good, since it will be different for different components in the first row.
Hagai Cibulski
But there must be in any case a max-width. To make it short. You cannot tell a table to ignore the necessary space of a second row without making either this row or the table itself to a max width. Not without using any kind of JavaScript which I higly recommend not to use.
Kau-Boy
A: 

Not sure why you say you cannot use a fixed width because you have a dependancy on the first cell. You can calculate this at run time by checking the value for the 2nd cell, and if it is longer than a certain defined width you would apply a defined width to your cell.

If the output for the 2nd cell however is not very long (and does not trigger your length check) then you do not need add any width definition.

It seems simple to me, but my assumption here is that you are using a scripting language on the side to generate this content, unless you mean this is PURE html, in which case I would tackle the problem with a JS library like jQuery and do the same length check on the value and if triggered add a width definition for your cell.

Jakub
The html is generated by GWT. The real html is much more complex. It is a generic dialog box that can have arbitrary components, hence each dialog will have a different width. On the bottom of my generic dialog box there is a status line that may present different texts. If the status text is long, it should wrap to the next line, making the dialog a bit higher, but not wider.
Hagai Cibulski
Would wrapping your output in a div with something like `max-width: 200px;` work? Could technically use a div to keep the content from spreading the cell too wide past your predefined width.
Jakub
No, since the width is *not* predefined.
Hagai Cibulski