Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I truncate table cells, but fit as much as content possible?

Meet Fred. He's a table:

One cell has more content and is wider, the other has less content and is narrower

<table border="1" style="width: 100%;">     <tr>         <td>This cells has more content</td>         <td>Less content here</td>     </tr> </table> 

Fred's apartment has a bizarre habit of changing size, so he's learned to hide some of his content so as not to push all the other units over and shove Mrs. Whitford's living room off into oblivion:

The cells are now the same size, but only one has its content truncated, and it looks like if the other cell gave if some whitespace, they could both fit.

<table border="1" style="width: 100%; white-space: nowrap; table-layout: fixed;">     <tr>         <td style="overflow: hidden; text-overflow: ellipsis">This cells has more content</td>         <td style="overflow: hidden; text-overflow: ellipsis">Less content here</td>     </tr> </table> 

This works, but Fred has a nagging feeling that if his right cell (which he's nicknamed Celldito) gave up a little space, his left cell wouldn't be truncated quite as much of the time. Can you save his sanity?


In summary: How can a table's cells overflow evenly, and only when they've all given up all their whitespace?

like image 667
s4y Avatar asked Mar 08 '11 23:03

s4y


People also ask

How do you fit content in a table cell?

In "Table Tools" click the [Layout] tab > locate the "Cell Size" group and choose from of the following options: To fit the columns to the text (or page margins if cells are empty), click [AutoFit] > select "AutoFit Contents." To fit the table to the text, click [AutoFit] > select "AutoFit Window."

How do you truncate text in CSS?

Solution # 1: Truncate text for single line This solution works for single-line truncation. Force text to be on a single line: white-space: nowrap; Now, our text should be on the same line and should overflow from the box if it's long enough and wrapped before.

How do I auto adjust column width in HTML?

If the text is non wrapping text then you set the cell to width:1px and use white-space:nowrap. The text in that column will then determine the width of the cell. It's a technique commonly used for images and captions (without the white-space:nowrap) and allows text to wrap at the limits of an image automatically.


2 Answers

<table border="1" style="width: 100%;">     <colgroup>         <col width="100%" />         <col width="0%" />     </colgroup>     <tr>         <td style="white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:1px;">This cell has more content.This cell has more content.This cell has more content.This cell has more content.This cell has more content.This cell has more content.</td>         <td style="white-space: nowrap;">Less content here.</td>     </tr> </table> 

http://jsfiddle.net/7CURQ/

like image 128
ladislav Avatar answered Sep 18 '22 16:09

ladislav


I believe I have a non-JavaScript solution! I didn't want to settle for a JavaScript fix because I find the slight jitter of things moving around after the page is loaded to be unacceptable.

Features:

  • No JavaScript
  • No fixed-layout
  • No weighting or percentage-width tricks
  • Works with any number of columns
  • Simple server-side generation and client-side updating (no calculation necessary)
  • Cross-browser compatible

How it works: Inside the table cell place two copies of the content in two different elements within a relatively-positioned container element. The spacer element is statically-positioned and as such will affect the width of the table cells. By allowing the contents of the spacer cell to wrap we can get the "best-fit" width of the table cells that we are looking for. This also allows us to use the absolutely-positioned element to restrict the width of the visible content to that of the relatively-positioned parent.

Tested and working in: IE8, IE9, IE10, Chrome, Firefox, Safari, Opera

Result Images:

Nice proportional widthsNice proportional clipping

JSFiddle: http://jsfiddle.net/zAeA2/

Sample HTML/CSS:

<td>     <!--Relative-positioned container-->     <div class="container">         <!--Visible-->         <div class="content"><!--Content here--></div>         <!--Hidden spacer-->         <div class="spacer"><!--Content here--></div>         <!--Keeps the container from collapsing without             having to specify a height-->         <span>&nbsp;</span>      </div> </td>  .container {     position: relative; } .content {     position: absolute;     max-width: 100%;     white-space: nowrap;     overflow: hidden;     text-overflow: ellipsis; } .spacer {     height: 0;     overflow: hidden; } 
like image 35
Jeff Camera Avatar answered Sep 17 '22 16:09

Jeff Camera