Custom theme ​

Element Plus uses BEM-styled CSS so that you can override styles easily. But if you need to replace styles at a large scale, e.g. change the theme color from blue to orange or green, maybe overriding them one by one is not a good idea.

We provide four ways to change the style variables.

Change theme color ​

These are examples about custom theme.

Preview with the theme editor ​

Add ?themeEditor=true to a documentation page URL, then open Theme Editor using the brush button in the bottom-right corner. The editor is also enabled during local development.

Choose a preset or edit the primary, success, warning, danger, and info colors. Changes are previewed on the page and saved in your browser. Click a shade or a primary color value (HEX, RGB, or HSB) to copy it, or use Restore defaults to reset the palette.

Use the switch in the drawer header to preview light and dark modes. Press Enter to apply a typed color. Closing the drawer returns keyboard focus to the brush button.

Text contrast compares black and white text on the primary color against the WCAG AA minimum of 4.5:1 for normal text. The info card automatically uses the more readable foreground. Inspect component states in the page preview when choosing application colors. The separate APCA section evaluates theme-colored text on a white background.

Copy CSS copies the base colors and their light/dark variants to the clipboard, ready to paste into your stylesheet. Export CSS downloads the same CSS as a file. Place the CSS after Element Plus styles in your application; dark variants use the html.dark selector.

Import theme accepts exported CSS or a JSON file with a colors object, for example:

json
{
  "colors": {
    "primary": "#0075eb",
    "success": "#67c23a"
  }
}

Missing colors use Element Plus defaults. Invalid imports leave the current theme unchanged. Use ?themeEditor=false to hide the editor, including during local development.

By SCSS variables ​

theme-chalk is written in SCSS. You can find SCSS variables in packages/theme-chalk/src/common/var.scss.

WARNING

We use sass modules (sass:map...) and @use to refactor all SCSS variables. And by using @use to all SCSS variables, it solves the duplicate output problem caused by @import.

Introducing Sass Modules | CSS-TRICKS

For example, We use $colors as a map to preserve different types of colors.

$notification is a map where all variables of the notification component at.

In the future, we will write documentation for variables that can be customized for each component. You can also directly checkout the source var.scss.

scss
$colors: () !default;
$colors: map.deep-merge(
  (
    'white': #ffffff,
    'black': #000000,
    'primary': (
      'base': #409eff,
    ),
    'success': (
      'base': #67c23a,
    ),
    'warning': (
      'base': #e6a23c,
    ),
    'danger': (
      'base': #f56c6c,
    ),
    'error': (
      'base': #f56c6c,
    ),
    'info': (
      'base': #909399,
    ),
  ),
  $colors
);

How to override it? ​

If your project also uses SCSS, you can directly change Element Plus style variables. Create a new style file, e.g. styles/element/index.scss:

WARNING

You should use @use 'xxx.scss' as *; instead of @import 'xxx.scss';.

Because the sass team said they will remove @import eventually.

Sass: @use vs Sass: @import

styles/element/index.scss
scss
/* just override what you need */
@forward 'element-plus/theme-chalk/src/common/var.scss' with (
  $colors: (
    'primary': (
      'base': green,
    ),
  )
);

// If you just import on demand, you can ignore the following content.
// if you want to import all styles:
// @use "element-plus/theme-chalk/src/index.scss" as *;

Then in the entry file of your project, import this style file instead of Element's built CSS:

TIP

Import element/index.scss before scss of element-plus to avoid the problem of sass mixed variables, because we need generate light-x by your custom variables.

Create a element/index.scss to combine your variables and variables of element-plus. (If you import them in ts, they will not be combined.)

TIP

In addition, you should distinguish your scss from the element variable scss. If they are mixed together, each hot update of element-plus needs to compile a large number of scss files, resulting in slow speed.

main.ts
ts
import { createApp } from 'vue'
import './styles/element/index.scss'
import ElementPlus from 'element-plus'
import App from './App.vue'

const app = createApp(App)
app.use(ElementPlus)

If you are using vite, and you want to custom theme when importing on demand.

Use scss.additionalData to compile variables with scss of every component.

vite.config.ts
ts
import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
// You can also use unplugin-vue-components
// import Components from 'unplugin-vue-components/vite'
// import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// or use unplugin-element-plus
import ElementPlus from 'unplugin-element-plus/vite'

export default defineConfig({
  resolve: {
    alias: {
      '~/': `${path.resolve(__dirname, 'src')}/`,
    },
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "~/styles/element/index.scss" as *;`,
      },
    },
  },
  plugins: [
    vue(),
    // use unplugin-vue-components
    // Components({
    //   resolvers: [
    //     ElementPlusResolver({
    //       importStyle: "sass",
    //       // directives: true,
    //       // version: "2.1.5",
    //     }),
    //   ],
    // }),
    // or use unplugin-element-plus
    ElementPlus({
      useSource: true,
    }),
  ],
})

If you are using webpack, and you want to custom theme when importing on demand.

webpack.config.js
js
// use unplugin-element-plus

import ElementPlus from 'unplugin-element-plus/webpack'

export default defineConfig({
  css: {
    loaderOptions: {
      scss: {
        additionalData: `@use "~/styles/element/index.scss" as *;`,
      },
    },
  },
  plugins: [
    ElementPlus({
      useSource: true,
    }),
  ],
})

By CSS Variable ​

CSS Variables is a very useful feature, already supported by almost all browsers. (IE: Wait?)

Learn more from Using CSS custom properties (variables) | MDN

We have used css variables to reconstruct the style system of almost all components.

TIP

It is compatible with the SCSS variable system. We use the function of SCSS to automatically generate css variables for use.

This means you can dynamically change individual variables inside the component to better customize it without having to modify scss and recompile it.

In the future, the css variable names and role documentation for each component will be written to each component.

Like this:

css
:root {
  --el-color-primary: green;
}

If you just want to customize a particular component, just add inline styles for certain components individually.

html
<el-tag style="--el-tag-bg-color: red">Tag</el-tag>

For performance reasons, it is more recommended to custom css variables under a class rather than the global :root.

css
.custom-class {
  --el-tag-bg-color: red;
}

RGB channel variables 2.14.10 ​

In both light and dark themes, Element Plus generates companion RGB channel variables for the base theme colors and shades light-3, light-5, light-7, light-8, light-9, and dark-2. Use them with rgba() to build translucent colors that follow the theme:

css
/* Default light theme example:
   --el-color-primary-light-9-rgb: 236, 245, 255 */

.custom-class {
  /* 10% translucent primary */
  background-color: rgba(var(--el-color-primary-rgb), 0.1);
  /* translucent primary-light-9 */
  border-color: rgba(var(--el-color-primary-light-9-rgb), 0.5);
}

Sass color overrides also update their companion -rgb variables. If you set a shade to a CSS expression such as var(--brand-shade), supply its RGB channels as a comma-separated list in the companion variable. Set it in the same theme scope and load it after the compiled theme stylesheet. For the page's dark theme, use html.dark. The compiled dark theme clears the light theme's RGB channels for that shade.

WARNING

Overriding a color CSS variable at runtime changes that variable alone. Set any shades and -rgb variables you use separately, or customize Sass colors at compile time.

Use additional shades on demand ​

In addition to the shades in the default stylesheet, the Sass $colors map includes light-1, light-2, light-4, and light-6. Read the shade you need at build time. This example makes var(--app-accent) use the primary light-4 shade from the active theme.

scss
@use 'sass:map';
@use 'element-plus/theme-chalk/src/common/var.scss' as light;
@use 'element-plus/theme-chalk/src/dark/var.scss' as dark;

:root {
  --app-accent: #{map.get(light.$colors, 'primary', 'light-4')};
}

html.dark {
  --app-accent: #{map.get(dark.$colors, 'primary', 'light-4')};
}

Update CSS variables from JavaScript ​

ts
// document.documentElement is global
const el = document.documentElement
// const el = document.getElementById('xxx')

// get css var
getComputedStyle(el).getPropertyValue(`--el-color-primary`)

// set css var
el.style['--el-color-primary'] = 'red'

If you want a more elegant way, check this out. useCssVar | VueUse

Shadow DOM 2.14.10 ​

Load element-plus/theme-chalk/shadow/index.css inside a shadow root to give it an independent theme. This stylesheet includes the component styles and both color schemes.

With Vite, import the stylesheet as a string with ?inline and add it to the shadow root.

ts
import theme from 'element-plus/theme-chalk/shadow/index.css?inline'

const host = document.createElement('div')
const shadowRoot = host.attachShadow({ mode: 'open' })
const style = document.createElement('style')
style.textContent = theme
shadowRoot.append(style)
document.body.append(host)

Add custom styles after the theme stylesheet and use :host to override its variables. See Dark Mode for dark mode overrides.

For popups such as Tooltip and Select, set :teleported="false" or pass an HTMLElement inside the shadow root to append-to so the popup uses the same theme.

To inherit theme variables from the surrounding page, load the regular element-plus/dist/index.css inside the shadow root and keep the page's theme styles in the document.