Cédric D
Cédric D

Reputation: 412

-webkit-line-clamp Ellipsis displayed with an offset when using text-align: justify

I'm using the following properties to get an ellipsis at the third line of my myClass paragraph

p.myClass {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
line-height: 120%;
max-height: 55px;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
font-size: 15px;
}

The ellipsis is appearing, but here is the issue I'm facing: Ellipsis appears over text

The thing is my class is also using:

text-align: justify;

Apparently the ellipsis rendering completely ignores the text-align: justify property. And the "..." appears where the text would have ended without justify.

I found a few workarounds using javascript and/or custom CSS classes. But I wondered if there were any solution to this using only simple CSS (Webkit is allowed since it's fine if it's working on Chrome only).

Upvotes: 2

Views: 2268

Answers (1)

Defims
Defims

Reputation: 243

a pure css method base on -webkit-line-clamp, it hide the origin "..." and use a custom div:

@-webkit-keyframes ellipsis {/*for test*/
    0% { width: 622px }
    50% { width: 311px }
    100% { width: 622px }
}
.ellipsis {
    max-height: 40px;/* h*n */
    overflow: hidden;
    background: #eee;

    -webkit-animation: ellipsis ease 5s infinite;/*for test*/
    /**
    overflow: visible;
    /**/
}
.ellipsis .content {
    position: relative;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-box-pack: center;
    font-size: 20px;/* w */
    line-height: 20px;/* line-height h */
    color: transparent;
    -webkit-line-clamp: 2;/* max row number n */
    vertical-align: top;
}
.ellipsis .text {
    display: inline;
    vertical-align: top;
    font-size: 14px;
    color: #000;
}
.ellipsis .overlay {
    position: absolute;
    top: 0;
    left: 50%;
    width: 100%;
    height: 100%;
    overflow: hidden;

    /**
    overflow: visible;
    left: 0;
    background: rgba(0,0,0,.5);
    /**/
}
.ellipsis .overlay:before {
    content: "";
    display: block;
    float: left;
    width: 50%;
    height: 100%;

    /**
    background: lightgreen;
    /**/
}
.ellipsis .placeholder {
    float: left;
    width: 50%;
    height: 40px;/* h*n */

    /**
    background: lightblue;
    /**/
}
.ellipsis .more {
    position: relative;
    top: -20px;/* -h */
    left: -14px;/* -w */
    float: left;
    color: #000;
    width: 20px;/* width of the .more w */
    height: 20px;/* h */
    font-size: 14px;

    /**
    top: 0;
    left: 0;
    background: orange;
    /**/
}
<div class='ellipsis'>
    <div class='content'>
        <div class='text'>text text text text text text text text text text text text text text text text text text text text text text</div>
        <div class='overlay'>
            <div class='placeholder'></div>
            <div class='more'>...</div>
        </div>
    </div>
</div>

Upvotes: 1

Related Questions