Reputation: 94
I'm trying to display a table (with given column width using Bootstrap's col-md-X
) with at most one line of (text) content per cell. Refer to the last example in my jsfiddle for an example of what I'd want.
I use this oneliner css class to limit the amount of text displayed :
.oneliner {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
I have two issues:
col-md-1
to col-md-12
in production tables, plus it doesn't solve next issue : .oneliner class
, it puts all the text in a single line, streching the parent (even with my custom sizing class), event if I apply in on a <span>
or <p>
inside my <td>
.JSFiddle here : http://jsfiddle.net/85TjP/2/ (updated)
I would really like to avoid using js for this.
Upvotes: 3
Views: 4634
Reputation: 94
Nevermind, found the solution.
Applying style="table-layout: fixed;"
to the table fixes both problems, the final html+css would look like :
CSS
.oneliner {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
HTML
<table class="table" style="table-layout: fixed;">
<tr>
<th class="col-md-6">Some title</th>
<th class="col-md-6">Other title</th>
</tr>
<tr>
<td>Short row</td>
<td class="oneliner">Much, much longer row, this is so long it's going to overflow</td>
</tr>
</table>
Of course table-layout : fixed;
should be applied via CSS
Upvotes: 1
Reputation: 71160
See the below for an example of how this can be accomplished:
Crucially applying the following properties to an element will ensure only one line of text is displayed and it doesnt resize:
white-space: nowrap;
overflow:hidden;
You can use the below for ellipses to appear if any text overflows, for visual purposes only:
text-overflow: ellipsis;
HTML
<div class='table'>
<div class='row'>
<div class='cell'>Something quite long</div>
<div class='cell'>here is some moreSomething quite long that should exceed the table cell.Something quite long that should exceed the table cell.</div>
</div>
</div>
CSS
.table {
margin:0;
padding:0;
display:table;
table-layout: fixed;
width:100%;
max-width:100%;
}
.row {
display:table-row;
}
.cell {
display:table-cell;
white-space: nowrap;
overflow:hidden;
text-overflow: ellipsis;
}
Upvotes: 2