Element UI Progress带text-inside属性如何将百分比文字置于进度条末端不被截断
解决方案
文字截断的核心原因是开启text-inside属性后,百分比文字被包裹在宽度随进度动态变化的进度条填充元素内部,该元素默认设置了overflow: hidden,偏移后的文字超出容器范围就会被截断。你可以通过自定义插槽+调整布局的方式实现需求,不需要强行偏移内部文字:
1. 修改HTML代码
关闭text-inside属性,通过默认插槽自定义百分比文字的展示位置,给组件添加自定义类方便样式控制:
<el-progress class="progress-with-end-text" :text-inside="false" :percentage="item.value" color="#6A7EC7" :stroke-width="12" > <template #default="{ percentage }"> <span class="progress-percentage">{{ percentage }}%</span> </template> </el-progress>
2. 替换原有CSS代码
调整组件布局为弹性布局,让进度条占满剩余宽度,文字固定跟在进度条末端:
/* 组件外层改为弹性布局,垂直居中对齐 */ .progress-with-end-text { display: flex; align-items: center; gap: 8px; /* 可自行调整进度条和文字的间距 */ } /* 进度条区域占满剩余宽度 */ .progress-with-end-text .el-progress-bar { flex: 1; } /* 隐藏组件默认的外部百分比文字 */ .progress-with-end-text .el-progress__text { display: none; } /* 保留你原来的背景透明设置 */ .progress-with-end-text .el-progress-bar__outer { background-color: transparent; } /* 自定义百分比文字样式 */ .progress-percentage { color: $color-tremor-black; min-width: 36px; /* 设置最小宽度,避免不同位数的数字导致布局偏移 */ }
如果你的样式加了scoped标识不生效,可以给类名前加上::v-deep(Vue3)或/deep/(Vue2)做样式穿透即可。
内容的提问来源于stack exchange,提问作者tommyahav10
相关产品推荐
相关产品推荐

