代码之家  ›  专栏  ›  技术社区  ›  Joe Scotto

如何使文本成为带填充和省略号的两行?

  •  0
  • Joe Scotto  · 技术社区  · 6 年前

    我的文本在省略号为两行时遇到问题。我可以让省略号正常工作,但要修改 line-height height 不更改显示的文本行数。任何关于我为什么不能更改行数的帮助都是很好的。

    https://codepen.io/Joe_Scotto/pen/gjXoJy

    3 回复  |  直到 6 年前
        1
  •  1
  •   dysfunc    6 年前

    试试这个

    CSS

    .slick-slide {
      border-radius: 7px;
      height: 200px;
      margin: 0 10px;
      overflow: hidden;
      position: relative;
      width: 200px;
    
      img {
        bottom: 0;        
        left: 0;
        margin: auto;
        position: absolute;
        right: 0;
        top: 0;
      }
    
      .info {
        background: rgba(black, 0.5);
        border: 1.5px solid white;
        border-radius: 7px;
        bottom: 10px;
        color: white;
        left: 0;
        margin: 0 10px;
        padding: 7px 10px;
        position: absolute;
        text-align: center;
        right: 0;
        z-index: 1;
    
        h6 {
          display: -webkit-box;
          margin: 0 auto;
          overflow: hidden;
          text-align: center;
          text-overflow: ellipsis;            
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;            
        }
      }
    }
    

    DEMO

    你只需要调整 -webkit-line-clamp 值设置为希望显示的行数。

        2
  •  0
  •   Aravind S    6 年前

    我用 -webkit-line-clamp: 2 属性。检查浏览器支持 here here 科迪森

    .slick-slide {
      margin: 0 10px;
      height: 200px;
      width: 200px;
      overflow: hidden;
      position: relative;
      border-radius: 7px;
    }
    .slick-slide img {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
    }
    .slick-slide .info {
      z-index: 1;
      position: absolute;
      left: 0;
      right: 0;
      bottom: 10px;
      color: white;
      text-align: center;
    }
    .slick-slide .info h6 {
      border: 1.5px solid white;
      display: inline-block;
      background: rgba(0, 0, 0, 0.5);
      border-radius: 7px;
      text-align: center;
      margin: 0 auto;
      max-width: 80%;
      padding: 7px 10px;
      line-height: 20px;
      height: 40px;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: block-axis;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    <div class="slick-slide">
        <div class="info">
            <h6>elements that are on fire my dude elements that are on fire my dude elements that are on fire my dude elements that are on fire my dude</h6>
        </div>
        <img src="http://placekitten.com/g/200/300" class="img-fluid">
    </div>
        3
  •  -1
  •   dkprajapati    6 年前
    .slick-slide .info {
        z-index: 1;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 15px;
        color: white;
        text-align: center;
        height: 40px;
        border: 1.5px solid white;
        background: rgba(0, 0, 0, 0.5);
        overflow: hidden;
        border-radius: 7px;
    }
    
    .slick-slide .info h6 {
        margin: 0;
        padding: 7px 10px;
        height: 19px;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        display: block;
        display: -webkit-box;
    }