全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

CSS实现溢出显示省略号

发布时间:2022-06-01 13:50:00
发布人:wjy

  小伙伴们,对于省略号呢,咱们前端攻城狮的实现方法可就是多种多样了,那接下来呢我就给你罗列一下如果用css书写这些特殊效果,来一起看看吧~~~

CSS实现溢出显示省略号

  ### **1.单行文本超出显示省略号**

  效果图:

  ![img](https://pic3.zhimg.com/80/v2-cc68769832667893109e16f63c89e52a_720w.jpg)

  实现代码:

  HTML部分

  ```html

  使用css实现单行省略号

  Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam ipsaexplicabo quos sapiente ea error, mollitia necessitatibus animi facere rem non sed velit aperiam laboriosamdebitis. Quae deleniti doloremque nisi.

  ```

  CSS部分

  ```css

  h3 {

  padding-left: 10px;

  }

  .a1 {

  text-decoration: none;

  color: #000;

  padding-left: 20px;

  }

  .box1 {

  height: 40px;

  line-height: 40px;

  background-image: linear-gradient(white, gray);

  box-shadow: 0 0 2px 2px rgb(148, 145, 145);

  box-sizing: border-box;

  /* 单行显示省略号 */

  overflow: hidden;

  text-overflow: ellipsis;

  white-space: nowrap;

  }

  ```

  注:此案例为京东首页的部分切图,此时的省略号可以用上述代码实现

  ### **2.多行显示省略号**

  效果图:

  ![img](https://pic1.zhimg.com/80/v2-69730dbe13d154f226d1104a10a02a68_720w.jpg)

  HTML部分

  ```html

  使用css实现三行之后显示省略号

  Lorem, ipsum dolor sit amet consectetur adipisicing elit. Fugit dicta laudantium aspernatur illo id, beatae mollitia magnam, laboriosam cupiditate harum veritatis ullam delectus adipisci quasi, laborum ipsum quis est molestiae.

  ```

  CSS部分

  ```css

  h3 {

  padding-left: 10px;

  }

  .a1 {

  text-decoration: none;

  color: #000;

  padding-left: 20px;

  }

  .box2 {

  height: 60px;

  line-height: 30px;

  background-image: linear-gradient(white, gray);

  box-shadow: 0 0 2px 2px rgb(148, 145, 145);

  overflow: hidden;

  /* 三行显示省略号 */

  display: -webkit-box;

  -webkit-box-orient: vertical;

  -webkit-line-clamp: 2;

  }

  ```

  实际案例应用场景说明:

  ![img](https://pic1.zhimg.com/80/v2-be574101a4729058dccfee91b7028a28_720w.jpg)

  注:此时明显是折行后在第二行多余的部分显示省略号,那由于内容不固定字数不固定,所以需要动态设置,那么就应用到了上述的多行显示省略号的方法

  ### **3.内容中间显示省略号(方法一)**

  效果图:

  ![img](https://pic3.zhimg.com/80/v2-64d9bef7ebb2ea8653e8e1b782fdd7ea_720w.jpg)

  HTML部分

  ```html

  使用css实现中间显示省略号方法一

  Lorem, ipsum dolor sit amet consectetur adipisicing elit. Commodi perferendis iste sit! Et quos aspernatur suscipit ab qui? Cumque debitis fugiat ab fugit repudiandae, vel eius error nisi minus全文

  ```

  css部分

  ```css

  .box3 {

  /* height: 120px; */

  line-height: 30px;

  background-image: linear-gradient(white, gray);

  box-shadow: 0 0 2px 2px rgb(148, 145, 145);

  }

  .box3 span::after {

  content: '......';

  }

  ```

  实际案例应用场景说明:

  ![img](https://pic1.zhimg.com/80/v2-ed093032be893a951e15397bf999e934_720w.jpg)

  ### **4.内容中间显示省略号(方法二)**

  效果图:

  ![img](https://pic1.zhimg.com/80/v2-4f4e6c2d1ff095fbf6a347a132d16840_720w.jpg)

  HTML部分

  ```html

  使用css实现中间显示省略号方法二

  我是左侧内容我是左侧内容我是左侧内容

  ```

  css部分

  ```css

  .box4 {

  height: 30px;

  line-height: 30px;

  background-image: linear-gradient(white, gray);

  box-shadow: 0 0 2px 2px rgb(148, 145, 145);

  }

  .box4 .span1 {

  float: left;

  width: 62%;

  height: 30px;

  line-height: 30px;

  overflow: hidden;

  }

  .box4 a {

  color: #000;

  }

  .box4 .span2::before {

  content: attr(title);

  width: 38%;

  float: right;

  overflow: hidden;

  text-overflow: ellipsis;

  white-space: nowrap;

  direction: rtl;

  }

  /* 优化两个span中间的间距 */

  .box4 {

  text-align: justify;

  }

  ```

  实际案例应用场景说明:

  ![img](https://pic4.zhimg.com/80/v2-1d01d51500b3d3181ed2b1c5d92bf97b_720w.jpg)

  综上所述:以上四种方案是目前页面中应用较多的实现省略号的方法,仅代表个人观点,如您有更优的方法欢迎联系我们。更多关于“html5培训”的问题,欢迎咨询千锋教育在线名师。千锋已有十余年的培训经验,课程大纲更科学更专业,有针对零基础的就业班,有针对想提升技术的提升班,高品质课程助理你实现梦想。

相关文章

今日头条视频怎么赚钱?头条号赚钱方法汇总

2023-09-19

今日头条展现量是什么?今日头条展现量规则分析

2023-09-19

今日头条怎么发布文章有收益?今日头条收益规则

2023-09-19

自媒体怎么入门挣钱?自媒体赚钱入门教程分享

2023-09-19

自媒体写文章怎么赚钱?自媒体写文章教程

2023-09-19

怎么上传短视频赚钱?短视频赚钱方法

2023-09-19
在线咨询 免费试学 教程领取