当前位置: 首页 > news >正文

css文字自适应宽度动态出现省略号...

前言

在列表排行榜中通常会出现的一个需求:从左到右依次是名次、头像、昵称、徽标、分数。徽标可能会有多个或者没有徽标,徽标长度是动态的,昵称如果过长要随着有无徽标进行动态截断出现省略号。如下图布局所示(花里胡哨的底色是为了看的更清楚,咱忍忍。。。)

一、实现效果

  • 多个徽标

        

  • 一个徽标

         

  • 没有徽标

        

 影响中间文字部分动态截断实现方法:

  • 设置昵称和徽标这个整体div的宽度;​​​​​​​
  • 方法1️⃣:昵称部分div设置为 flex-shrink: 1 自动收缩宽度;
  • 方法2️⃣:昵称部分div设置为 flex: 1;min-width: 0;

二、结构拆分

        

  1. 使用flex左右布局,分数之前为左边,分数固定最大长度为右边;
  2. 左边的内容继续使用flex布局排列,固定名次、头像宽度;
  3. 昵称和徽标的模块使用flex布局,给定一个大概宽度,昵称设置截断省略;

三、全部代码

1.整体结构

代码如⬇️:

  <!-- 整行 --><div class="item-wrapper"><div class="item-left"><div class="item-left-num">1</div><image class="item-left-avatar" src="xxx" /><div class="item-left-text"><div class="item-left-name">lemon是我的名字lemon是我的名字lemon是我的</div><image class="item-left-icon item-left-level" src="xxx" /><image class="item-left-icon item-left-fan" src="xxx" /></div></div><div class="item-right">666</div></div>

2.css样式

代码如下⬇️:

.item-wrapper {width: 100%;height: 62pit;flex: 0 0 auto;overflow: hidden;background-color: gray;padding: 0 16pit;display: flex;flex-direction: row;justify-content: space-between;align-items: center;
}.item-left {overflow: hidden;display: flex;flex-direction: row;align-items: center;justify-content: flex-start;background-color: rgb(139, 201, 237);
}.item-left-num {flex: 0 0 auto;font-family: PingFangSC-Medium;font-size: 20pit;color: #333;font-weight: 700;line-height: 62pit;letter-spacing: 0;width: 26pit;text-align: center;background-color: rgb(225, 131, 197);
}.item-left-avatar {flex: 0 0 auto;width: 38pit;height: 38pit;background-color: #fff;background-repeat: no-repeat;background-position: center center;background-size: cover;border-radius: 36px;margin-left: 8pit;
}.item-left-text {display: flex;flex-direction: row;align-items: center;width: 55vw;background-color: #333;
}.item-left-name {font-family: PingFangSC-Medium;font-weight: 500;font-size: 14pit;line-height: 62pit;color: #333;letter-spacing: 0;margin-left: 8pit;// 方式1️⃣// flex: 1;// min-width: 0;// 方式2️⃣flex-shrink: 1; // 自动收缩宽度overflow: hidden;text-overflow: ellipsis;white-space: nowrap;background-color: aqua;
}.item-left-icon {height: 16pit;background-color: #fff;background-repeat: no-repeat;background-position: center center;background-size: cover;margin-left: 4pit;
}.item-left-level {flex: 0 0 auto;width: 32pit;
}.item-left-fan {flex: 0 0 auto;width: 52pit;
}.item-right {flex: 0 0 auto;font-family: PingFangSC-Medium;font-weight: 400;line-height: 62pit;font-size: 12pit;color: #666;letter-spacing: 0;text-align: right;width: 52pit;margin-left: 16pit;background-color: rgb(240, 171, 229);
}

http://www.lryc.cn/news/397283.html

相关文章:

  • 边缘计算盒子_B100_Jetson Nano (aarch64)开发环境搭建
  • 【Superset】dashboard 自定义URL
  • 【Linux网络】IP协议{初识/报头/分片/网段划分/子网掩码/私网公网IP/认识网络世界/路由表}
  • 香蕉派BPI-Wifi6迷你路由器公开发售
  • WPF-控件样式设置
  • C++20中的指定初始化器(designated initializers)
  • QT跨平台开发(windows、mac)中.pro文件设置
  • wifi中的stream parser
  • GitHub网页打开慢的解决办法
  • 前端vue 实现取色板 的选择
  • [leetcode]partition-list 分隔链表
  • Apache功能配置:访问控制、日志分割; 部署AWStats日志分析工具
  • 开源可视化Flutter图表库:Graphic
  • Linux搭建Socks5网络代理服务器,Centos 8 系统
  • mysql 导出导入 数据库
  • linux_进程概念——理解冯诺依曼体系结构
  • Linux笔记之使用系统调用sendfile高速拷贝文件
  • OpenCV和PIL进行前景提取
  • Linux虚拟化大师:使用 KVM 和 QEMU 进行高级虚拟化管理
  • CentOS-6的iso下载地址镜像yum源
  • 【python】PyQt5可视化开发,鼠标键盘实现联动界面交互逻辑与应用实战
  • Raw Socket(一)实现TCP三次握手
  • 考研数学开始的晚?别慌,超全复习规划拿去抄
  • 创建React 项目的几种方式
  • 探索Kotlin:从K1到K2
  • Python爬虫速成之路(1):获取网页源代码
  • OpenGL笔记七之顶点数据绘制命令和绘制模式
  • 力扣题解( 最长湍流子数组)
  • pytorch-RNN存在的问题
  • Leetcode 17:电话号码的字母组合