Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have an HtmlTable cell that I want to be small, width-wise. It has a label "Total Expenses" that I want on two lines, but everything I've tried to force it to wrap has not worked. Here's the html:

HTML
<td width="40" class="skybluebackground centertext"><label class="wrappable">Total Expenses</label></td>



...other rows have cells like this in that column (number input elements):

HTML
<td width="40"><input width="40" type="number" step="0.01" min="0" name="airfareTotalExpense" id="airfareTotalExpense"/></td>



Trying to reduce the width of those number input elements fails, too.

The CSS I've tried:

CSS
.wrappable {
  /*word-wrap: normal;*/
  float:left;
  width: 40px;
}


I tried setting several other properties, too, but obviously nothing worked.
Posted
Updated 25-Aug-15 12:41pm
v2

1 solution

First of all, you need to understand that, by default, the size of the cell td is defined by its inner HTML. If this is text, it will be wrapped as any other text; you don't need anything except limiting the width of this element, then the inner content will try to fit it. The same goes for label.

With input, however, it won't work, because it is always one-line. Instead of input, you have to use textarea.

In my example below, for illustration, I used td with both limited and default size, and for visibility, added border, padding, and made the table width 100%, so you could see what happens if you change the width of the browser window:
XML
<html>
   <head>
      <style>
         table { width:100%; }
         td { border: solid thin black; padding: 1em; }
         td.wrappable { width: 10em; }
      </style>
   </head>
<body>

<table><tr>
   <td class="wrappable">This text should be wrappable</td>
   <!-- or it can be <label>This text should be wrappable</label> -->
   <td>1.2</td>
</tr>
<tr>
   <td class="wrappable"><textarea>some content</textarea></td>
   <td>2.2</td>
</tr></table>

</body>
</html>


By the way, I strongly advise you to quit the habit of using any style-related attributes except, perhaps, class. Instead, always use CSS. Also, for most applications, relative units, such as "em" are preferable over absolute units (pixels).

The problem is solved.

—SA
 
Share this answer
 
v6
Comments
B. Clay Shannon 26-Aug-15 12:05pm    
That's not working for me. I tried this HTML:

<td class="skybluebackground centertext"><label class="wrappable">Total Expenses</label></td>

(label is wrappable)

With this CSS:

wrappable {
width: 5em;
}


...then, based on your idea, this:

<td class="skybluebackground centertext wrappable"><label>Total Expenses</label></td>

With this CSS:

td.wrappable {
width: 5em;
}


...but neither works - the column remains too wide for my purposes, and thus the text doesn't wrap.

I tried this on the table, too:

<COLGROUP WIDTH="48"></COLGROUP>
<COLGROUP WIDTH="32"></COLGROUP>
<COLGROUP WIDTH="32"></COLGROUP>
<COLGROUP WIDTH="32"></COLGROUP>
<COLGROUP WIDTH="32"></COLGROUP>

...but it has no effect.
Sergey Alexandrovich Kryukov 26-Aug-15 12:18pm    
My code sample does work. What can I do if you are doing it wrong? For example, you selector "wrappable" won't be applied to any element; you can use, for example, td.wrappable. Everything should work. I test my samples before posting.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900