我的网站开发技术经验总结 我的网站开发技术经验总结
首页

fangdown

我的网站开发技术经验总结
首页
  • 大前端

  • 基础

    • js

    • css

      • 如何让chrome实现小于12px的字体大小
      • 如何使用css画出三角形,正方形
      • 如何使用CSS完成视差滚动效果
      • 说说单行和多行文本溢出的实现方式
        • 是什么
        • 实现方式
          • 单行文本
          • 多行文本
          • 基于高度溢出
          • 基于行数溢出
      • 说说对Css预编语言的理解
      • 说说你对CSS盒子模型的理解
      • 说说设备像素、css像素、设备独立像素、dpr、ppi 之间的区别
      • 说说什么是响应式设计及实现方式
      • 说说什么是重绘和回流,如何优化
      • 说说元素水平垂直居中的方法有哪些
      • 说说CSS3新增了哪些新特性
      • 说说css性能优化有哪些方式
      • 说说css中层叠上下文z-index的理解
      • 说说css中的选择器是优先级
      • 说说css中两栏布局和三栏布局有哪些
      • 说说css中哪些属性是继承和不继承的
      • 说说css中实现动画有哪些
      • 说说CSS中有哪些方式可以隐藏页面元素
      • 说说em-px-rem-vh-vw的区别
      • 说说flex盒子的理解及应用
      • 说说property和prototype的区别
      • 谈谈你对BFC的理解
    • ES6

  • 框架

  • 情商

  • 算法

  • 网络

  • 千锤百炼
  • 基础
  • css
fangdown
2021-08-24
目录

说说单行和多行文本溢出的实现方式

# 是什么

文本溢出: 元素内容宽度过长,不能完全显示, 把不能显示的文本用省略号显示...

# 实现方式

  • 单行文本
  • 多行文本

# 单行文本

  p{
    width:400px;
    overflow:hidden;
    white-space:nowrap;
    text-overflow: ellipsis;
  }

  • text-overflow只有在设置了overflow:hidden和white-space:nowrap才能够生效的
    • clip 超出裁切
    • ellipsis 超出显示省略号

# 多行文本

  • 基于高度溢出
  • 基于行数溢出

# 基于高度溢出

  • 在已知高度情况下
  • 兼容性好
  • 响应式
<style>
    .p1{
      position: relative;
      width: 200px;
      height: 40px;
      line-height: 20px;
      overflow: hidden;
    }
    .p1::after{
      content: '...';
      position: absolute;
      right: 0;
      bottom: 0;
      padding: 0 20px 0 10px;
      word-break: break-all;
      background-color: #FFF;
    }
  </style>
  <p class="p1">我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本</p>
  <p class="p1">hello world,hello world,hello world,hello world,hello world,hello world,</p>
  • 未知高度(动态js设置)

<style> 
    .p3{
      position: relative;
      width: 200px;
      line-height: 20px;
      overflow: hidden;
    }
    .p3-after::after{
      content: '...';
      position: absolute;
      right: 0;
      bottom: 0;
      padding-left: 40px;
      background: -webkit-linear-gradient(left, transparent, #fff 55%);
      word-break: break-all;
    }
  </style>
  <p class="p3">
    我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本
  </p>
  <script>
    const p3 = $('.p3')
    const lh = parseInt(p3.css('line-height'))
    const p3Height = p3.height()
    if(p3Height/lh > 1){
      p3.addClass('p3-after')
      p3.height('60')
    } else {
      p3.removeClass('p3-after')
    }
  </script>

# 基于行数溢出

  • webkit 移动端适用
  <style>
    .p2{
      width: 200px;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space:break-all;
    }
  </style>
  <p class="p2">我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本我是文本</p>
  <p class="p2">hello world,hello world,hello world,hello world,hello world,hello world,</p>

#css
上次更新: 2021/12/19, 18:05:42
如何使用CSS完成视差滚动效果
说说对Css预编语言的理解

← 如何使用CSS完成视差滚动效果 说说对Css预编语言的理解→

最近更新
01
多分支修复撞车的问题
05-01
02
如何成为架构师
01-23
03
服务器部署全过程
11-23
更多文章>
Theme by Vdoing | Copyright © 2019-2026 fangdown | 粤ICP备19079809号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式