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; :确保内容不会换行,这样气泡框的宽度就会根据内容的实际宽度来调整
相关推荐
ZC跨境爬虫31 分钟前
跟着 MDN 学CSS day_39:(Flexbox 弹性盒子核心机制)
前端·css·ui·html·tensorflow
小陈同学呦34 分钟前
前端如何处理订单状态导航的数据竞态问题
前端·javascript
喵个咪1 小时前
GoWind Toolkit 前端代码生成|Vue3(ElementPlus/Vben)、React(AntDesign)全自动一键生成教程
前端·vue.js·react.js
摆烂大大王2 小时前
玩转 OpenClaw:用 TaskFlow + Heartbeat 打造自动化工作流
前端·人工智能·自动化
zhangxingchao2 小时前
AI 大模型核心六:量化、Workflow 与 Agent、多轮 RAG
前端·人工智能·后端
梦想的颜色3 小时前
TypeScript 完全指南(上):从零开始掌握类型系统
前端·typescript
之歆3 小时前
Day01_ES6+ 专业指南:从基础到实战的现代JavaScript开发(下)
前端·javascript·es6
lichenyang4533 小时前
鸿蒙 MVVM 实战:从 Demo 到工程化,聊聊登录、状态管理与埋点系统设计
前端
IT_陈寒4 小时前
Vite打包时遇到的坑,原来问题出在这里
前端·人工智能·后端
kyriewen4 小时前
AI生成代码快如闪电,但我修了三个小时——它到底帮了谁?
前端·javascript·ai编程