With CSS, use “…” for overflowed block of multi-lines

后端 未结 16 2316
情话喂你
情话喂你 2020-11-22 07:15

with

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

\"...\" will be shown in the end of the line if overflowed. However, t

相关标签:
16条回答
  • 2020-11-22 07:25

    I have hacked around until I've managed to achieve something close to this. It comes with a few caveats:

    1. It's not pure CSS; you have to add a few HTML elements. There's however no JavaScript required.
    2. The ellipsis is right-aligned on the last line. This means that if your text isn't right-aligned or justified, there may be a noticable gap between the last visible word and the ellipsis (depending on the length of the first hidden word).
    3. The space for the ellipsis is always reserved. This means that if the text fits in the box almost precisely, it may be unnecessarily truncated (the last word is hidden, although it technically wouldn't have to).
    4. Your text needs to have a fixed background color, since we're using colored rectangles to hide the ellipsis in cases where it's not needed.

    I should also note that the text will be broken at a word boundary, not a character boundary. This was deliberate (since I consider that better for longer texts), but because it's different from what text-overflow: ellipsis does, I thought I should mention it.

    If you can live with these caveats, the HTML looks like this:

    <div class="ellipsify">
        <div class="pre-dots"></div>
        <div class="dots">&hellip;</div>
        <!-- your text here -->
        <span class="hidedots1"></span>
        <div class="hidedots2"></div>
    </div>
    

    And this is the corresponding CSS, using the example of a 150 pixel wide box with three lines of text on a white background. It assumes you have a CSS reset or similar that sets margins and paddings to zero where necessary.

    /* the wrapper */
    .ellipsify {
        font-size:12px;
        line-height:18px;
        height: 54px;       /* 3x line height */
        width: 150px;
        overflow: hidden;
        position: relative; /* so we're a positioning parent for the dot hiders */
        background: white;
    }
    
    /* Used to push down .dots. Can't use absolute positioning, since that
       would stop the floating. Can't use relative positioning, since that
       would cause floating in the wrong (namely: original) place. Can't 
       change height of #dots, since it would have the full width, and
       thus cause early wrapping on all lines. */
    .pre-dots {
        float: right;
        height: 36px;  /* 2x line height (one less than visible lines) */
    }
    
    .dots {
        float: right; /* to make the text wrap around the dots */
        clear: right; /* to push us below (not next to) .pre-dots */
    }
    
    /* hides the dots if the text has *exactly* 3 lines */
    .hidedots1 {
        background: white;
        width: 150px;
        height: 18px;       /* line height */
        position: absolute; /* otherwise, because of the width, it'll be wrapped */
    }
    
    /* hides the dots if the text has *less than* 3 lines */
    .hidedots2 {
        background: white; 
        width: 150px;
        height: 54px;       /* 3x line height, to ensure hiding even if empty */
        position: absolute; /* ensures we're above the dots */
    }
    

    The result looks like this:

    image of the rendered result with different text lengths

    To clarify how it works, here's the same image, except that .hidedots1 is hightlighted in red, and .hidedots2 in cyan. These are the rectangles that hide the ellipsis when there's no invisible text:

    the same image as above, except that the helper elements are highlighted in color

    Tested in IE9, IE8 (emulated), Chrome, Firefox, Safari, and Opera. Does not work in IE7.

    0 讨论(0)
  • 2020-11-22 07:25

    I found a javascript trick, but you have to use the length of the string. Lets say you want 3 lines of width 250px, you can calculate the length per line i.e.

    //get the total character length.
    //Haha this might vary if you have a text with lots of "i" vs "w"
    var totalLength = (width / yourFontSize) * yourNumberOfLines
    
    //then ellipsify
    function shorten(text, totalLength) {
        var ret = text;
        if (ret.length > totalLength) {
            ret = ret.substr(0, totalLength-3) + "...";
        }
        return ret;
    }
    
    0 讨论(0)
  • 2020-11-22 07:26

    Great question... I wish there was an answer, but this is the closest you can get with CSS these days. No ellipsis, but still pretty usable.

    overflow: hidden;
    line-height: 1.2em;
    height: 3.6em;      // 3 lines * line-height
    
    0 讨论(0)
  • 2020-11-22 07:26
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical; 
    

    see more click here

    0 讨论(0)
  • 2020-11-22 07:27

    Here is the closest solution I could get using just css.

    HTML

    <div class="ellipsis"> <span>...</span>
    Hello this is Mr_Green from Stackoverflow. I love CSS. I live in CSS and I will never leave working on CSS even my work is on other technologies.</div>
    

    CSS

    div {
        height: 3em;
        line-height: 1.5em;
        width: 80%;
        border: 1px solid green;
        overflow: hidden;
        position: relative;
    }
    div:after {
        content:". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . .";
        background-color: white;
        color: white;
        display: inline;
        position: relative;
        box-shadow: 8px 1px 1px white;
        z-index: 1;
    }
    span {
        position: absolute;
        bottom: 0px;
        right: 0px;
        background-color: white;
    }
    

    Working Fiddle (resize the window to check)

    Link to my blog for explanation

    Updated Fiddle

    I hope now some css expert would have got idea on how to make it perfect. :)

    0 讨论(0)
  • 2020-11-22 07:28

    thanks @balpha and @Kevin, I combine two method together.

    no js needed in this method.

    you can use background-image and no gradient needed to hide dots.

    the innerHTML of .ellipsis-placeholder is not necessary, I use .ellipsis-placeholder to keep the same width and height with .ellipsis-more. You could use display: inline-block instead.

    .ellipsis {
        overflow: hidden;
        position: relative;
    }
    .ellipsis-more-top {/*push down .ellipsis-more*/
        content: "";
        float: left;
        width: 5px;
    }
    .ellipsis-text-container {
        float: right;
        width: 100%;
        margin-left: -5px;
    }
    .ellipsis-more-container {
        float: right;
        position: relative;
        left: 100%;
        width: 5px;
        margin-left: -5px;
        border-right: solid 5px transparent;
        white-space: nowrap;
    }
    .ellipsis-placeholder {/*keep text around ,keep it transparent ,keep same width and height as .ellipsis-more*/
        float: right;
        clear: right;
        color: transparent;
    }
    .ellipsis-placeholder-top {/*push down .ellipsis-placeholder*/
        float: right;
        width: 0;
    }
    .ellipsis-more {/*ellipsis things here*/
        float: right;
    }
    .ellipsis-height {/*the total height*/
        height: 3.6em;
    }
    .ellipsis-line-height {/*the line-height*/
        line-height: 1.2;
    }
    .ellipsis-margin-top {/*one line height*/
        margin-top: -1.2em;
    }
    .ellipsis-text {
        word-break: break-all;
    }
    <div class="ellipsis ellipsis-height ellipsis-line-height">
        <div class="ellipsis-more-top ellipsis-height"></div>
        <div class="ellipsis-text-container">
            <div class="ellipsis-placeholder-top ellipsis-height ellipsis-margin-top"></div>
            <div class="ellipsis-placeholder">
               <span>...</span><span>more</span>
            </div>
            <span class="ellipsis-text">text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text </span>
        </div>
        <div class="ellipsis-more-container ellipsis-margin-top">
            <div class="ellipsis-more">
                <span>...</span><span>more</span>
            </div>
        </div>
    </div>

    jsfiddler

    0 讨论(0)
提交回复
热议问题