Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: as table gets narrower, text-overflow should cut off more and more text

Tags:

html

css

overflow

I have a web page that has an HTML table generated by an ASP.NET control. The top row of the table emulates a toolbar. That top row is laid out sort of like this:

<tr>
  <td style="white-space:nowrap;">
    <span>Very long title</span>&nbsp;
  </td>
  <td>
    { bunch of hyperlinks with background images, emulating a toolbar }
  </td>
</tr>

I am stuck with the fact that this is a table, with the contents of the title area being put in a span, and probably with that inline style specifying no wrapping.

This table is supposed to narrow itself when the page narrows, and it does that pretty well. However, if the actual title text (which can vary) is too long, it effectively serves as a limit on how narrow the table can get, which can lead to overlapping with other things on the page. (I'm using IE8, BTW.)

I tried applying overflow:hidden, text-overflow:ellipsis, and display: inline-block to the span and adding a width value for the table cell, all through a stylesheet. (Display:block on the span doubled the height of the row, which I didn't want, so inline-block was my choice.) The title text appeared truncated, just as I wanted. But the table wouldn't get any narrower.

After messing around changing values and styles through IEDT I concluded that, when figuring out how narrow the table could get, the text was still counted as being its full length. The span was relatively short, only the text that appeared in the span appeared, but for table-cell width purposes, the text was still all there.

I didn't find this phenomenon documented anywhere, but maybe I just don't get around enough. :) If text-overflow can't help me, I guess I'll have to dynamically truncate the title through script, which I'd rather not do. Is there a way to do what I want solely through CSS?

like image 651
Ann L. Avatar asked Oct 27 '11 21:10

Ann L.


People also ask

How do I reduce the width of a table in HTML?

To manipulate the height or width of an entire table, place the size attribute (either "WIDTH=" or "HEIGHT=") within the <TABLE> code. To manipulate individual cells, place the size attribute within the code for that cell.

How do you increase and decrease the size of a cell in a table of HTML?

Use the CSS property width and height to set the width and height of the cell respectively. Just keep in mind, the usage of style attribute overrides any style set globally. It will override any style set in the HTML <style> tag or external style sheet.

How do you truncate text in a HTML table cell?

Within the table-cell that you want to apply truncation, simply include a container div with css table-layout: fixed. This container takes the full width of the parent table cell, so it even acts responsive. Make sure to apply truncation to the elements in the table.


1 Answers

Table's cells display behavior differs from usual behavior of blocks. To make it more typical you need to change table's table-layout property to "fixed" first and set table width:

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

It will disable cell's automatic sizing it most cases. After that you can set td's width and overflow as usual.

td.title { white-space:nowrap;overflow: hidden; width: 200px; }

Here is example: http://jsfiddle.net/vS3qq/1/

like image 186
Alexey Ivanov Avatar answered Sep 28 '22 16:09

Alexey Ivanov