0

I have a Table

<table class="tbl" border="1">   
    <tr>
        <td width="20%"><span class="spn">column one is a long one</span></td>
        <td width="60%"><span class="spn">column two</span></td>
        <td width="20%"><span class="spn">column three is the longest of all columns</span></td>
    </tr>
</table>

and CSS-Settings:

.spn
{
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  width: inherit;
  color: red;    
}

.tbl
{
  width: 300px
}

I want the first and third column to be 20% width and the second should be 60% of the 300px table width. The text should be fill the full td and end with ... at the end. How to achieve this?

http://jsfiddle.net/8jgmnyn5/

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
fubo
  • 44,811
  • 17
  • 103
  • 137

2 Answers2

4

You have two problems.

  1. For the table, you'll need table-layout:fixed. Otherwise it will prioritise displaying all the contents over the given column widths.
  2. The spans have width:inherit, but the table columns have widths like 20%, so the spans will be 20% of 20%, which I'm sure is not what you meant. So get rid of the width on the spans. display:block type elements don't need widths.
.spn {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  /*width: inherit;*/
  color: red;    
}

.tbl
{
  width: 300px;
  table-layout:fixed;
}

Then the result will look like this fiddle.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

You want something like this?

http://jsfiddle.net/8jgmnyn5/2/

your altered css looks like this:

.spn {
  display: block;
  color: red;    
}

.spn:after {
    content: "…";
}
Doml The-Bread
  • 1,761
  • 1
  • 11
  • 17
  • almost, i want to avoid newline with `white-space: nowrap;` - the text can be cut off when it doesn't fit – fubo Apr 27 '15 at 13:44