14

Fiddle: http://jsfiddle.net/wTtsV/

The table cell #t2 does not size correctly:

HTML:

<div id="table">
    <div id="t1">a</div>
    <div id="t2">
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    </div>
    <div id="t3">a</div>
</div>

CSS:

body{
    margin: 0;
}
#table{
    display: table;
    width: 100%;
}
#t1{
    display: table-cell;
    background-color: red;
}
#t2{
    display: table-cell;
    background-color: green;
}
#t3{
    display: table-cell;
    background-color: blue;
}

Expected result:

enter image description here

How to hide text in #t2 when it is too long?

7
  • See, this is my desired result and the text of # t2 here is short: jsfiddle.net/wTtsV/1 if the length of the text exceeds 100% #t2, happens that it lengthens such as here: jsfiddle.net/wTtsV Commented Sep 11, 2013 at 16:57
  • "exceeds 100%" - 100% of WHAT? Herein lies your problem. Commented Sep 11, 2013 at 17:00
  • 100% of #t2, see this picture: i42.tinypic.com/n1patu.png, I would like this result instead of jsfiddle.net/wTtsV Commented Sep 11, 2013 at 17:05
  • The dots at the end there because the text is too long and cause the elongation of the table, it would not be long 100% as I desire, but more! Commented Sep 11, 2013 at 17:07
  • 100% of t2 = t2, it's self-referential. That's why you need to set a maximum width in pixels, not percent. Commented Sep 11, 2013 at 17:11

3 Answers 3

42

I had exactly the same problem, and I got a nice solution. Add table-layout: fixed; to your id table:

#table{
    display: table;
    width: 100%;
    table-layout: fixed;
}

Have fun! :D

Sign up to request clarification or add additional context in comments.

1 Comment

Much better (and correct) solution. For those looking, here are some great use cases: css-tricks.com/fixing-tables-long-strings
9

Fiddle: http://jsfiddle.net/wTtsV/7/

word-break:break-all;

1 Comment

It's not an exact solution but it's the only viable CSS solution. Never used that property before but I'll be sure to remember it now :-)
2

May be you are looking for:-

overflow:hidden;

to hide the text outside the zone.

or may you can try using this:-

overflow: hidden;
white-space: nowrap; 
text-overflow: ellipsis;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.