Use overflow:ellipsis;
and white-space:nowrap;
with the selector td {}
or in the element <td style="">
It won’t work because the Tds and Ths behave differently than type elements block
and inline-block
, i.e., they would have to 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 in the table when using a TD or TD with the expanded layout, to solve this just use the property 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% as well 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
for the desired TD element, thus:
<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 be customized in different ways in the future and you want to take advantage of it in various elements and pages the ideal would be to create your own .css
the part, outside the frameworks (as materialize), for example, creates a file called:
/projeto/css/main.css
And inside put all the customizations you want, including the tables and link in HTML so:
<!-- 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>
<link rel="stylesheet" href="css/main.css">
Downvoter, what you didn’t understand in the question, that I might be getting better for you?
– rbz
The downvote is not mine, but the closing one is :)
– Guilherme Nascimento
@But that question they didn’t use inline or I don’t understand any more! hahaha
– rbz
style=""
is the same behavior to put on a selector, the properties do not change the writing or anything, just copy from within the selector of the other answer and put inside thestyle=""
.– Guilherme Nascimento
@Rbz I am the downvoter. I took the vote now that you edited. I was confused and leading users to answer something that did not suit your need
– Wallace Maxters
@Wallacemaxters I agree! It is that sometimes it is something so simple that it becomes difficult to ask the question!
– rbz
Guys, I think this one
td
does not accept to do this. I put adiv
to put the text in and it worked. It would just be that way, it will be!?– rbz
@Guilhermenascimento had never stopped to think so simple. Shame!
– rbz
@Rbz quiet, we are all here to learn and share ;)
– Guilherme Nascimento
@Did Rbz work put inside TD? Inform me this, if it doesn’t work I will reopen the question.
– Guilherme Nascimento
@Guilhermenascimento No... Even from what I’ve researched, it doesn’t work directly on
td
, you have to put the text in adiv
. Then you change the question again, and to "worse", you have to set thewidth
fixed, then the blessed does not adjust with the size of thetd
, Then I realized I’m wasting my time for "perfume" that I don’t need right now. Then later would open another question about size adjustment of thediv
withtd
truncating text...– rbz
No need to fix width, just use
100%
no div, I voted to reopen the question dup does not cover at all the problem, so as soon as they reopen turn into answer– Guilherme Nascimento
@Guilhermenascimento there he assumes 100% and trumps nothing! Even if I have
width
in theth
, in thetd
, indiv
...– rbz
@Rbz is because the table has to apply
table-layout: fixed
, see the comment I made in Wallace’s reply, has an example of use.– Guilherme Nascimento