Using overflow:ellipsis;
and white-space:nowrap;
with the td {}
selector or the <td style="">
element will not work because the TDs and THs behave differently than elements of type block
and inline-block
, meaning they would have that have their widths fixed for it to work, otherwise the table will expand beyond the size fixed in table, for example:
.foo {
width: 400px;
}
.foo thead {
font-weight: bold;
}
.foo td, .foo th {
border: 1px #e0e0e0 solid;
}
.text-limit {
width: 100%;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
<table class="foo">
<thead>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
<th>head4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-limit">foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
<tr>
<td>cell1_1</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
</tbody>
</table>
Note that even setting the size to 400px on the tabel when you use a TD or TD with the layout expanded, to solve this, just use the table-layout: fixed;
:
.foo {
width: 400px;
table-layout: fixed;
}
.foo thead {
font-weight: bold;
}
.foo td, .foo th {
border: 1px #e0e0e0 solid;
}
.text-limit {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
<table class="foo">
<thead>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
<th>head4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-limit">foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
<tr>
<td>cell1_1</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
</tbody>
</table>
Note that the table can use 100% too and the effect still works:
.foo {
width: 100%;
table-layout: fixed;
}
.foo thead {
font-weight: bold;
}
.foo td, .foo th {
border: 1px #e0e0e0 solid;
}
.text-limit {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
<table class="foo">
<thead>
<tr>
<th>head1</th>
<th>head2</th>
<th>head3</th>
<th>head4</th>
</tr>
</thead>
<tbody>
<tr>
<td class="text-limit">foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
<tr>
<td>cell1_1</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
</tbody>
</table>
Using inline form
Just copy the example from .text-limit
to the desired TD element, like this:
<table style="table-layout: fixed;">
...
<tr>
<td style="white-space: nowrap; text-overflow: ellipsis; overflow: hidden;">foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz foo bar baz</td>
<td>cell2_1</td>
<td>cell3_1</td>
<td>cell4_1</td>
</tr>
...
However if you want to apply to all elements and you have control over HTML you could simply add a style element like this:
<style>
.minhatabela {
width: 100%;
}
.minhatabela th, .minhatabela td {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
</style>
And in HTML just add this:
<table class="minhatabela">
Or if you believe that your CSS will eventually be customized in different ways and you want to take advantage of it in many elements and pages, it would be ideal to create your own .css
aside from the frameworks (such as materialize) creates a file called:
/projeto/css/main.css
And inside it put all the customizations you want, including those of the tables and link it in HTML like this:
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script><linkrel="stylesheet" href="css/main.css">