Show first 3 lines in HTML paragraph

前端 未结 3 1074
南方客
南方客 2021-01-04 23:07

I want to show only the first 3 lines of the paragraph below using HTML formatting. I am searching on W3Schools but it doesn\'t show how to do it.



        
相关标签:
3条回答
  • 2021-01-04 23:54

    You can give the container a height and line-height and set its overflow property to hidden:

    HTML

    <p>loremloremlorem...</p>
    

    CSS

    p {
        height:60px;
        line-height:20px; /* Height / no. of lines to display */
        overflow:hidden;
    }
    

    JSFiddle example.

    0 讨论(0)
  • 2021-01-04 23:56

    You can use -webkit-line-clamp property with div.

    div {
      width: 100%;
      line-height: 1.2em;
      height: 3.6em;
      background-color: gainsboro;
      overflow: hidden;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 3;
    }
    <div>
      loremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremlore
      loremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremlore
      loremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremlore
      loremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremlore
      loremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremloremlore
    </div>

    0 讨论(0)
  • 2021-01-04 23:58

    Set the height of the paragraph to three line-heigths. Like -

    p{
      line-height:1.2em;
      height:3.6em;
      overflow:hidden;
    }
    

    Set overflow to hidden to hide overflowing text

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