在span里包3个span,通过text-align
属性控制对齐方式
html"><el-table-column prop="BYC" label="BYC/PBYC" width="95"><template slot-scope="scope"><!-- 保留原有条件判断,但调整内部结构 --><template v-if="scope.row.BYC"><span v-if="scope.row.PBYC >= 0" class="align-slash" style="color:red"><span class="left-part">{{ scope.row.BYC }}</span><span class="slash">/</span><span class="right-part">{{ scope.row.PBYC }}%</span></span><span v-else-if="scope.row.PBYC < 0" class="align-slash" style="color:#00B05A"><span class="left-part">{{ scope.row.BYC }}</span><span class="slash">/</span><span class="right-part">{{ scope.row.PBYC }}%</span></span></template></template>
</el-table-column><style>
/* 关键样式:固定左右宽度 + 对齐 */
.align-slash {display: inline-flex; /* 行内弹性布局 */width: 100%; /* 占满单元格宽度 */
}.left-part {flex: 1; /* 左侧自适应宽度 */text-align: right; /* 右对齐内容(如"100") */padding-right: 2px; /* 与斜杠的间距 */
}.slash {flex: none; /* 斜棍固定宽度 */
}.right-part {flex: 1; /* 右侧自适应宽度 */text-align: left; /* 左对齐内容(如"10%") */padding-left: 2px; /* 与斜杠的间距 */
}
</style>