CSS word ellipsis ('…') after one or two lines

前端 未结 9 1988
执笔经年
执笔经年 2021-01-01 23:40

I\'m trying to create a word-wrap in JavaScript using CSS, and the condition is:

If DIV contains one very long word, such as \"asdasfljashglajksgkjasghk

9条回答
  •  -上瘾入骨i
    2021-01-02 00:10

    If you can guarantee the content will run over, here is the solution I came up. It works for my site and I wanted to keep it simple.

    html:

    [content]

    css:

    .snippet {
        position: relative;
        height: 40px; // for 2 lines
        font-size: 14px; // standard site text-size
        line-height: 1.42857; // standard site line-height
        overflow: hidden;
        margin-bottom: 0;
    }
    .news-insights .snippet:after {
        content: "\02026";
        position: absolute;
        top: 19px;
        right: 0;
        padding-left: 5px;
        background: linear-gradient(to right, rgba(255, 255, 255, 0), white 20%, white); // use vendor prefixes for production code
    }
    

    You can then play around with the padding and background gradient to get a more stylish presentation.

提交回复
热议问题