文本溢出处理显示省略号

一、文本溢出处理显示省略号

1、单行文本溢出显示省略号

方法1:省略号

单行文本: width:200px;设置盒子的宽度(限制宽度) white-space:nowrap;设置文本不换行 overflow:hidden;溢出隐藏 text-overflow:ellipsis;文本溢出的显示省略号

<style>

.box{

    width:200px;

    background-color:red;

    height:40px;

    line-height:40px;

    /* 关键代码 */

    text-overflow: ellipsis; /* 溢出显示省略号 */

    overflow: hidden; /* 溢出隐藏 */

    white-space: nowrap;  /* 强制不换行 */

}

</style>

<body>

<div class="box">MONTBLANC万宝龙 男士经典系列椭圆形针扣皮带/腰带 38157</div> 

</body>方法2:伪元素

<style>

    .box {

        width: 200px;

        height: 40px;

        line-height: 40px;

        background-color: orchid;

        overflow: hidden;/* 溢出隐藏 */

        white-space: nowrap;/* 强制不换行 */

        position: relative;/* 子绝父相 */

    }

    .box:after {

        content: "...";

        position: absolute;

        right: 0px;

        bottom: 0px;

        /* 设置宽度可以微调省略号的盒子大小,盖住多余的文字 */

        width: 20px;

        background-color: red;

    }

</style>

</head>

<body>

    <div class="box">

        可以沉迷,可以抱怨,可以奔溃,可以抱怨,可以奔溃,人要有自愈能力

    </div>

</body>2、多行文本溢出显示省略号

方法1:伪元素

  <style>

        .box {

            width: 200px;

            /* 通过行高和高度控制显示的行数 */

            height: 120px;

            line-height: 30px;

            background-color: red;

            position: relative;

            /* 文本两端对齐方式 */

            text-align: justify;

            /* 溢出隐藏 */

            overflow: hidden;

        }

        .box:after {

            content: "...";

            position: absolute;

            right: 0;

            bottom: 0;

            background-color: red;

            width: 16px;

        }

    </style>

<body>

    <div class="box">MONTBLANC万宝龙 男士经典系列椭圆形针扣皮带/腰男士经典系列椭圆形针扣皮带男士经典系列椭圆形针扣皮带男士经典系列椭圆形针扣皮带带 38157</div>

</body>

​方法2:弹性盒

width:200px;设置盒子的宽度(限制宽度) ​ display:-webkit-box;将对象作为弹性伸缩盒子模型显示 ​ -webkit-line-clamp:2;用来限制在一个快元素显示的文本行数 ​ -webkit-box-orient:vertical;设置或检索伸缩盒对象的子元素的排列方式

overflow:hidden;溢出隐藏

   <style>

        .box2 {

       /* 1.设置宽度 */

      width: 200px;

      background-color: pink;

      line-height: 30px;

      /* 2.设置旧版弹性盒 */

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值