Text abbreviation <td> does not work

Asked

Viewed 137 times

0

I would like to apply abbreviation in a <td>:

<td> TextoTextoTextoTextoTextoTexto </td>

Displaying only:

TextoTexto...

I tried to use style="text-overflow:ellipsis" but it didn’t work.

Detail: I need it to be inline, because I already have style sheet, I can not change and nor put separate style.

  • Downvoter, what you didn’t understand in the question, that I might be getting better for you?

  • The downvote is not mine, but the closing one is :)

  • @But that question they didn’t use inline or I don’t understand any more! hahaha

  • 1

    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 the style="".

  • 2

    @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

  • 1

    @Wallacemaxters I agree! It is that sometimes it is something so simple that it becomes difficult to ask the question!

  • Guys, I think this one td does not accept to do this. I put a div to put the text in and it worked. It would just be that way, it will be!?

  • @Guilhermenascimento had never stopped to think so simple. Shame!

  • @Rbz quiet, we are all here to learn and share ;)

  • @Did Rbz work put inside TD? Inform me this, if it doesn’t work I will reopen the question.

  • @Guilhermenascimento No... Even from what I’ve researched, it doesn’t work directly on td, you have to put the text in a div. Then you change the question again, and to "worse", you have to set the width fixed, then the blessed does not adjust with the size of the td, 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 the divwith td truncating text...

  • 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

  • @Guilhermenascimento there he assumes 100% and trumps nothing! Even if I have width in the th, in the td, in div...

  • @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.

Show 9 more comments

3 answers

2


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">

1

It is possible yes, maybe it is not working alone, but accompanied by the properties white-space, overflow and a width defined in container, works...

<h2>text-overflow: ellipsis:</h2>
<div class="b" style="

  text-overflow: ellipsis;
  white-space: nowrap; 
  width: 50px; 
  overflow: hidden; 
  border: 1px solid #000000;

">Hello world!</div>

  • Felipe, I appreciate the strength! But I would like inline, because I already have style sheet, but in this case I wanted straight on the line.

  • @But it is always necessary to have the white-space:nowrap

  • @Wallacemaxters But this one white-space:nowrap can it be inline? Because if I can, I can use: <td style:"white-space:nowrap;text-overflow:ellipsis;">?

  • @Rbz, está inline... https://www.codecademy.com/articles/html-inline-styles, the difference is I skipped a few lines, you don’t have to do this

  • @Felipeduarte I wrote wrong, yours did not work! I used without the edge: style="text-overflow:ellipsis;white-space:nowrap;width:50px;overflow:hidden" doesn’t work

  • Is it because it’s a td?

  • Put your code in question and if possible the css s that affect the element, it may not be working for various factors, behavior and type of container, styles being superimposed...

  • CSS is a Framework: Materialize

Show 3 more comments

1

I usually leave a predefined class to do this. I think is the best way to work on CSS

.text-limit{
   white-space: nowrap;
   text-overflow: ellipsis;
   overflow: hidden;
   
}
<div class="text-limit" style="width:50px">
   Meu nome é Wallace
</div>

<div class="text-limit" style="width:75px">
   Meu nome é Wallace
</div>


<div class="text-limit" style="width:100px">
   Meu nome é Wallace
</div>

<div class="text-limit" style="width:130px">
   Meu nome é Wallace
</div>

By default, I always match overflow: hidden, text-overflow: ellipsis and white-space: nowrap.

  • I need you to be inline teacher! Like this: <td style:"white-space:nowrap;text-overflow:ellipsis;"> ... I know the best way is in CSS or style, but in this case I need it to be straight on the line.

  • Yeah, but what’s so hard?

  • Wallacemaxters the behavior of <TD> is different from DIV, which means that you cannot apply directly to TD or TH, you would have to use an element within TD, for example: http://jsfiddle.net/3t7kaxy8/ (cc @Rbz)

  • I know, but the answer was given before, right. I’m kind of busy now, then I see if it gives time to change (maybe I delete)

  • @Wallacemaxters was just giving a way for a future issue :( with an example, I was going to edit his reply, but I was afraid of how the community would look at it (reply attempt)

  • Ah, if you can edit, it’s cool, kkkkk. I’m kind of stuck today.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.