Wrap a text within only two lines inside div

前端 未结 11 804

I want to wrap a text within only two lines inside div of specific width. If text goes beyond the length of two lines then I want to show elipses. Is there a way to do that

11条回答
  •  醉话见心
    2020-12-04 09:41

    Limiting output to two lines of text is possible with CSS, if you set the line-height and height of the element, and set overflow:hidden;:

    #someDiv {
        line-height: 1.5em;
        height: 3em;       /* height is 2x line-height, so two lines will display */
        overflow: hidden;  /* prevents extra lines from being visible */
    }
    

    --- jsFiddle DEMO ---

    Alternatively, you can use the CSS text-overflow and white-space properties to add ellipses, but this only appears to work for a single line.

    #someDiv {
        line-height: 1.5em;
        height: 3em;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
        width: 100%;
    }
    

    And a demo:

    --- jsFiddle DEMO ---

    Achieving both multiple lines of text and ellipses appears to be the realm of javascript.

提交回复
热议问题