3

I have a div with 300px width and 20px height, the text changes randomly in it. I want to show ellipsis only when overflow with 2 lines otherwise as it, using only CSS.

http://jsfiddle.net/7BXtr/272/

HTML:

<div id="container">
            <div class="box">

        <div class="text-body">
            <div class="text-inner">This is the text description of the item.
                It can flow onto more than 2 lines, too,
                if there is room for it, but otherwise
                should only show 1 line.
            </div>
        </div>
    </div>
</div>

CSS:

#container {
    width: 104px;
}
.box {

    max-height: 40px;
    overflow: hidden;

    border: solid 1px red;
    position: relative;
}
.box:after {
    content: '...';
    position: absolute;
    left: 84px;
    bottom: 0;
}
.text-body {
    position: relative;
}
.text-body:before {
    content: '';
    position: absolute;
    left: 80px;
    top: 14px;
    width: 12px;
    height: 2px;
    background: white;
}
.text-inner {
    width: 90px;
    padding-right: 10px;
}
1
  • 4
    No - this is not possible in CSS unfortunately, you will need a JS solution Commented May 9, 2014 at 14:59

1 Answer 1

6

You can have a text overflow ellipsis with larger than one line only for Webkit, it is not supported by the other vendors yet

Here's an example with three lines of text with an ellipse example:

.three-line-block {
    line-height: 15px; //for not webkit browser
    max-height: 45px; //for not webkit browser
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}
Sign up to request clarification or add additional context in comments.

1 Comment

What do you mean by your comments in the code? Both lines are supported by webkit and other vendors...

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.