CSS 实现的文本截断

考察如下代码实现文本超出自动截断的样式代码:

.truncate-text-4 {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 4;
}

使用如下的 HTML 片段进行测试:

<style>
.my-div {
width: 300px;
margin: 10px auto;
background: #ddd;
}
</style>
<div class="my-div truncate-text-4">
How Not To Shuffle - The Knuth Fisher-Yates Algorithm. Written by Mike James.
Thursday, 16 February 2017. Sometimes simple algorithms are just wrong. In
this case shuffling an .... In other words as the array is scanned the element
under
</div>

运行效果:

通过 CSS ellipsis 实现的文本截断效果

padding 的问题

一切都很完美,直到给文本容器加上 padding 样式后。

  .my-div {
width: 300px;
margin: 10px auto;
background: #ddd;
+ padding: 30px;
}

现在的效果是这样的:

padding 破坏了文本截断

因为 padding 占了元素内部空间,但这部分区域却是在元素内部的,所以不会受 overflow: hidden 影响。

解决办法

line-height

当设置的 line-height 适当时,或足够大时,可以将 padding 的部分抵消掉以实现将多余部分挤出可见范围的目标。

.my-div {
width: 300px;
margin: 10px auto;
background: #ddd;
padding: 30px;
+ line-height: 75px;
}

通过 line-height 修复

这种方式并不适用所有场景,因为不是所有地方都需要那么大的行高。

替换掉 padding

padding 无非是要给元素的内容与边框间添加间隔,或是与别的元素间添加间隔。这里可以考虑其实方式来替换。

比如 margin。但如果元素有背景,比如本例中,那 margin 的试就不适用了,因为元素 margin 部分是不带背景的。

还可用 border 代替。

.my-div {
width: 300px;
margin: 10px auto;
background: #ddd;
- padding: 30px;
+ border: 30px solid transparent;
}

使用 border 替换 padding

毫不意外,它仍然有它的局限性。就是在元素本身有自己的 border 样式要求的时候,就会冲突了。

将边距与内容容器分开

比较普适的方法可能就是它了,即将内容与边距分开到两个元素上,一个元素专门用来实现边距,一个元素用来实现文本的截断。这个好理解,直接看代码:

<div className="my-div">
<div className="truncate-text-4">
How Not To Shuffle - The Knuth Fisher-Yates Algorithm. Written by Mike
James. Thursday, 16 February 2017. Sometimes simple algorithms are just
wrong. In this case shuffling an .... In other words as the array is scanned
the element under
</div>
</div>

而我们的样式可以保持不动。

将边距与内容容器分开

相关资源

最新文章

  1. APP并非一个人在战斗,还有API—Xamarin.Android回忆录
  2. The world won&#39;t care about your self-esteem. The world will expect you to accomplish something before you feel good about yourself.
  3. java selenium (八) Selenium IDE 用法
  4. [Google Guava]学习--新集合类型Multiset
  5. Spring Data JPA 的配置文件 已经数据库的状态
  6. 《DSP using MATLAB》示例Example4.2
  7. 转载大神的dfs讲解
  8. ZOJ 1107 FatMouse and Cheese
  9. .NET_RSA加密全接触(重、难点解析)
  10. Android:PopupWindow简单弹窗
  11. linux read 用法
  12. LeetCode 解题报告--202Happy Number
  13. 使用Mono Cecil 动态获取运行时数据 (Atribute形式 进行注入 用于写Log) [此文报考 xxx is declared in another module and needs to be imported的解决方法]-摘自网络
  14. 使用了UnityEditor中的API,打包时却不能打包UnityEditor的问题
  15. servlet(2)servlet过滤器
  16. CentOS7 开启网卡,设置开机启用网卡
  17. (4) MySQL中EXPLAIN执行计划分析
  18. DataGridView控件使用大全说明-各种常用操作与高级操作
  19. Redis源码阅读(六)集群-故障迁移(下)
  20. Android中SurfaceView用法示例

热门文章

  1. .net core Webapi +EF
  2. CH4301 Can you answer on these queries III 题解
  3. 【Offer】[25] 【合并两个排序的链表】
  4. odoo12之应用:一、双因子验证(Two-factor authentication, 2FA)(HOTP,TOTP)附源码
  5. F#周报2019年第37期
  6. FreeSql 新手上路系列教程已发布在 cnblogs
  7. java对象排序(Comparable)详细实例
  8. Cookie的删除
  9. 03.Django基础三之视图函数
  10. 16 (OC)* UIAnimation和CoreAnimation