Element-UI框架的el-col组件span属性值通常不能超过24,超出后会自动换行。然而,如果需要在span值总和超过24的情况下,仍然在一行显示所有元素并显示水平滚动条,该如何解决呢?
问题的关键在于el-row组件的默认换行行为。要避免换行,我们需要绕过el-row的默认布局机制。 直接使用el-row和el-col无法实现此效果。
一种有效的解决方案是放弃el-row和el-col,采用Flexbox或Inline-block布局。 以下示例使用Flexbox:
创建一个父容器,并设置其display: flex和white-space: nowrap。 white-space: nowrap防止文本换行,而display: flex允许子元素在一行排列。 如果子元素总宽度超过父容器宽度,浏览器会自动显示水平滚动条。
示例代码:
<template> <div style="display: flex; white-space: nowrap; overflow-x: auto;"> <div style="width: 8%; background: #d3dce6; margin: 0 1%; height: 30px; border-radius: 4px;"></div> <div style="width: 8%; background: #e5e9f2; margin: 0 1%; height: 30px; border-radius: 4px;"></div> <div style="width: 8%; background: #d3dce6; margin: 0 1%; height: 30px; border-radius: 4px;"></div> <div style="width: 8%; background: #e5e9f2; margin: 0 1%; height: 30px; border-radius: 4px;"></div> <div style="width: 8%; background: #d3dce6; margin: 0 1%; height: 30px; border-radius: 4px;"></div> <div style="width: 8%; background: #e5e9f2; margin: 0 1%; height: 30px; border-radius: 4px;"></div> </div></template>
登录后复制
本文来自互联网或AI生成,不代表软件指南立场。本站不负任何法律责任。