el-popover气泡宽度由内容撑起

前言

  • el-popover有个width属性,就算不填也有默认值150。想要让气泡内容撑起气泡宽度要特殊处理
  • 以下内容由Trae改造生成

方案

html 复制代码
<template>
  <el-popover
    placement="top"
    :width="0"
    effect="light"
    popper-class="auto-fit-popover"
    v-model:visible="popoverShow"
  >
    <div class="chat-popover-options">
      <div
        class="ctrl-item"
        @click="fuc1()"
      >
        功能1
      </div>
    </div>
    <template #reference>
      <button @click="popoverShow = true"></button>
    </template>
  </el-popover>
</template>

<style>
.auto-fit-popover {
  min-width: unset !important;
  width: auto !important;
  white-space: nowrap;
}
</style>

实现原理

  • :width="0" :通过设置宽度为 0,让 Element Plus 的 popover 组件不使用默认的固定宽度
  • !important :使用 !important 来覆盖 Element Plus 的默认样式,确保我们的样式能够生效
  • white-space: nowrap; :确保内容不会换行,这样气泡框的宽度就会根据内容的实际宽度来调整
相关推荐
IT_陈寒3 分钟前
Vite静态资源加载把我坑惨了
前端·人工智能·后端
herinspace4 分钟前
管家婆实用贴-如何分离和附加数据库
开发语言·前端·javascript·数据库·语音识别
小码哥_常28 分钟前
从MVC到MVI:一文吃透架构模式进化史
前端
嗷o嗷o29 分钟前
Android BLE 的 notify 和 indicate 到底有什么区别
前端
豹哥学前端32 分钟前
别再背“var 提升,let/const 不提升”了:揭开暂时性死区的真实面目
前端·面试
lar_slw1 小时前
k8s部署前端项目
前端·容器·kubernetes
拉拉肥_King1 小时前
Ant Design Table 横向滚动条神秘消失?我是如何一步步找到真凶的
前端·javascript
GreenTea1 小时前
DeepSeek-V4 技术报告深度分析:基础研究创新全景
前端·人工智能·后端
河阿里2 小时前
HTML5标准完全教学手册
前端·html·html5
吴声子夜歌2 小时前
Vue3——新语法
前端·javascript·vue.js