Vue ElementUI 修改消息提示框样式—messageBox 的大小

在窄屏模式下(移动端或pda),提示框的宽度太宽,会出现显示不完全的问题。 应当如何修改 ElementUI 的样式呢?

javascript 复制代码
  open() {
      this.$confirm(
        window.vm.$i18n.t("tips.conLogOut"),
        window.vm.$i18n.t("tips.tip"),
        {
          confirmButtonText: window.vm.$i18n.t("backTips.confirm"),
          cancelButtonText: window.vm.$i18n.t("backTips.cancel"),
          type: "warning",
        }
      ).then(() => {
        this.logout();
      });
    },
css 复制代码
 <style scoped>
  .el-message-box {
    width: 235px;
}
</style>


此时在scoped的style中写是无效的,因为ElementUI组件不可以给样式添加scoped,因此必须去掉scoped;但是去掉scoped后不满足单组件的CSS。

解决方案

1、附加在没有scoped的style中

css 复制代码
<style scoped>
  ...
</style>
<style>
  ...
  .el-message-box {
    width: 235px;
  }
</style>


2、给消息提示框加类名(荐)
更加推荐这个messageBox添加一个类名,比较好用并且不会影响到其他页面的弹框样式。

javascript 复制代码
this.$confirm('确认注销吗?', '提示', {
  customClass: 'elmessageWidth'
}).then(() => {
  this.$message({
    message: '已成功注销',
    type: 'success'
  })
}).catch(() => {  })
css 复制代码
<style scoped>
  ...
</style>
<style>
 .elmessageWidth {
    width: 350px;
  }
</style>

或者直接important

css 复制代码
@media (max-width: 730px) {
  .elmessageWidth{
    width: 350px !important;
  }
 }
相关推荐
一拳不是超人24 分钟前
Electron主窗口弹框被WebContentView遮挡?独立WebContentView弹框方案详解!
前端·javascript·electron
anyup34 分钟前
🔥2026最推荐的跨平台方案:H5/小程序/App/鸿蒙,一套代码搞定
前端·uni-app·harmonyos
雮尘1 小时前
如何在非 Claude IDE (TARE、 Cursor、Antigravity 等)下使用 Agent Skills
前端·agent·ai编程
icebreaker1 小时前
Weapp-vite:原生模式之外,多一种 Vue SFC 选择
前端·vue.js·微信小程序
icebreaker1 小时前
重走 Vue 长征路 Weapp-vite:编译链路与 Wevu 运行时原理拆解
前端·vue.js·微信小程序
wuhen_n1 小时前
代码生成:从AST到render函数
前端·javascript·vue.js
喝咖啡的女孩1 小时前
浏览器前端指南
前端
wuhen_n1 小时前
AST转换:静态提升与补丁标志
前端·javascript·vue.js
喝咖啡的女孩1 小时前
浏览器前端指南-2
前端
cxxcode1 小时前
从 V8 引擎视角理解微任务与宏任务
前端