Home  >  Q&A  >  body text

javascript - How to display text in one line and replace it with... if it exceeds the number of words?

As in the title, the width is fixed, if the number of words exceeds the number, use... instead

大家讲道理大家讲道理2682 days ago847

reply all(7)I'll reply

  • 迷茫

    迷茫2017-05-19 10:15:23

    overflow:hidden;white-space:nowrap;text-overflow:ellipsis;
    
    overflow:hidden:超出隐藏
    white-space:nowrap:强制不换行
    text-overflow:ellipsis:超出省略号代替;
    

    Portal, go check it out

    reply
    0
  • 为情所困

    为情所困2017-05-19 10:15:23

    Fixed approach requires the following four conditions:
    1. Fixed width
    2.overflow:hidden
    3.white-space:nowrap
    4.text-overflow:ellipsis

    reply
    0
  • 高洛峰

    高洛峰2017-05-19 10:15:23

     display: -webkit-box;
        box-orient: vertical;
        line-clamp: 3; // 可控制几行后有 ... 
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3; // 兼容写法
        overflow: hidden;
        white-space:normal;

    reply
    0
  • 阿神

    阿神2017-05-19 10:15:23

    First of all, the first point is that the line needs to be given a fixed width. Even if the width is not given, the line needs to be limited by margin. To put it bluntly, it is to give the line a fixed width. . .
    Other codes are as follows

    如果是一个p标签
    p{
        width:100px;//固定宽度
        overflow:hidden;//超出隐藏
        white-space:nowrap;//不换行
        text-overflow:ellipsis;//省略号
    }

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-19 10:15:23

    /a/11...
    Two implementation methods, fixed width, excess display..., mouse hover to omit content

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-19 10:15:23

    // line-clamp、line-height和height直接控制多行
    .two-line {
        color: #333;
        line-height: 18px;
        height: 36px;
        overflow : hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        display: -moz-box;
        -webkit-line-clamp: 2;
        -moz-line-clamp: 2;
        line-clamp: 2;
        -webkit-box-orient: vertical;
        -moz-box-orient: vertical;
        box-orient: vertical;
      }

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-19 10:15:23

    With fixed width, use overflow:hidden;text-overflow:ellipsis;white-space:nowrap;

    reply
    0
  • Cancelreply