1

I want to set the TD width to number of characters, for ex, if I set to 11, only 11 characters must be displayed and rest must be in a new row. I tried width property but it displays differently in different browsers/resolutions.

<table border="1">
    <tr>
<td>test1</td>
<td>CRTAX_G</td>
    <td>
    28-FEB-2013, 11-MAR-2013, 13-MAR-2013
    </td>
    </tr>
<tr>
<td>test1</td>
<td>CRTAX_G</td>
    <td>
    28-FEB-2013, 11-MAR-2013, 13-MAR-2013
    </td>
    </tr>
<tr>
<td>test1</td>
<td>CRTAX_G</td>
    <td>
    28-FEB-2013, 04-MAR-2013, 05-MAR-2013, 07-MAR-2013, 11-MAR-2013, 13-MAR-2013, 12-MAR-2013, 08-MAR-2013, 06-MAR-2013
    </td>
    </tr>
<tr>
<td>test1</td>
<td>CRTAX_G</td>
    <td>
    28-FEB-2013, 11-MAR-2013, 13-MAR-2013
    </td>
    </tr>
<tr>
<td>test1</td>
<td>CRTAX_G</td>
    <td>
    28-FEB-2013, 06-MAR-2013, 08-MAR-2013, 12-MAR-2013, 13-MAR-2013, 11-MAR-2013, 07-MAR-2013, 05-MAR-2013, 01-MAR-2013
    </td>
    </tr>
    <table>

which displays the dates in the table in a single line as,

28-FEB-2013, 06-MAR-2013, 08-MAR-2013, 12-MAR-2013, 

I want it to be displayed as

   28-FEB-2013,
   11-MAR-2013,
   13-MAR-2013,

every date must be in a new line.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NEO
  • 1,961
  • 8
  • 34
  • 53
  • The contents of the TD looks like a list of dates to me. Why not declare a OL or UL and style the items? That way you don't have to treat is as unstructured text, but you create structure and style it. – Kwebble Mar 14 '13 at 08:32

5 Answers5

3

The simplest solution i can imagine is that:

table tr td+td+td {
  font-size: 100%;
  font-family: monospace;
  width: 8em;
}

http://jsbin.com/elaleg/1/edit

Saram
  • 1,500
  • 1
  • 18
  • 35
  • It works. Is there anyway not to use monospace font and get the same result? – NEO Mar 14 '13 at 08:50
  • I thing that using monospace is obligatory - if you want do it without changing content. – Saram Mar 14 '13 at 08:54
  • It would seem that em is the height of a character, not the width. You should use ch instead. See the second answer down here: http://stackoverflow.com/questions/8186457/specify-width-in-characters – Joel B Oct 07 '15 at 14:31
2

You can wrap your text in tag or anything else with following style:

display: inline-block; width: 11em;

Check JsFiddle

Sergio
  • 6,900
  • 5
  • 31
  • 55
0
<html>
 <body>
   <table border="1">
    <tr>
        <td style="width:103px;word-wrap:break-word;">28-FEB-2013, 11-MAR-2013, 13-MAR-2013</td>
    </tr>
</table>
 </body>
</html>

Try this, I'm hoping you'll get the solution

Ankit Chauhan
  • 384
  • 2
  • 18
0

You can check this

or try css style white- space effectively

white-space: nowrap;
Community
  • 1
  • 1
Sandy
  • 2,429
  • 7
  • 33
  • 63
0

here is one solution which worked for me. Assuming that td has a class name (say payerName) specified and the table has an id (say sortable).

$(".payerName").css({"white-space":"nowrap"});
$(".payerName").css({"overflow":"hidden"});
$(".payerName").css({"text-overflow":"ellipsis"});
$("#sortable").css("table-layout","fixed");

This is a set of steps you can use to avoid long length strings into the td. the ellipsis property will add a '...' at the end of the string/value. You can add the title attribute so that it displays the actual value when mouse is hover.

Hope this helps!

RishikeshD
  • 189
  • 2
  • 4