Custom Theme Colors and Dark Mode

Learn how to implement dark mode and customize Element UI theme colors with global SCSS variables.

🎯 Introduction

Customizing a UI library helps your application match its product design. Element UI is a popular Vue 2 library with a consistent default appearance, but projects often need custom theme colors or dark mode. This article covers theme colors and dark mode techniques for adapting your interface to your product.


🎨 Introducing Theme Colors and Dark Mode

Element UI’s default theme uses blue tones, which suit many applications. Custom theme colors and dark mode help you match brand colors and user preferences.

Dark mode can make viewing more comfortable for some users and reduce screen power consumption on some displays. You can implement it using theme styles, class switching, or theme variables.


🌙 Enabling Dark Mode

1. Dark Theme Styles

The following approach imports a dark theme package and uses a class to apply dark styles. Check the package’s compatibility and usage instructions for your Element UI version.

Steps:

  • Install a dark theme package in your project: yarn add element-theme-dark
  • Add the .el-theme-dark class to the root element to apply dark styles

Example:

main.js
import 'element-theme-dark';
<div id="app" class="el-theme-dark">
    <!-- Element UI components here will use dark styles -->
</div>

This provides a starting point for applying dark theme styles.

2. Implementing Custom Dark Mode

If a packaged dark theme does not meet your requirements, or you want to combine it with custom theme colors, consider these approaches:

  • Switch CSS variables: Define colors with CSS variables, override them for dark mode, and switch the active variable values.
  • Compile SCSS themes: Use your build tools to compile separate theme CSS files, then replace the active stylesheet at runtime.
  • Switch classes: Define colors under a .dark class and toggle that class to change modes.

These approaches provide more flexibility for complex theme systems.


🎨 Changing Theme Colors with Global SCSS Variables

Element UI themes are built from SCSS variables. Override these variables to define your own theme colors.

1. Prerequisites

  • Install sass and sass-loader to compile SCSS
  • Configure global SCSS variables in your Vue project so Element UI styles can access them

Import element-variables.scss in main.js.

main.js
import './styles/element-variables.scss';

2. Override the Default Theme Color

Override the following core variable in element-variables.scss:

element-variables.scss
$--color-primary: #90f9c1; // Primary color for dark mode

.el-button--primary {
    background-color: $--color-primary !important;
    border-color: $--color-primary !important;

    &:hover {
        background-color: darken($--color-primary, 10%) !important;
        border-color: darken($--color-primary, 10%) !important;
    }
}

4. Adjust Theme Colors for Dark Mode

If you toggle dark mode with a class, define conditional styles in SCSS:

element-variables.scss
.el-theme-dark {
    $--color-primary: #90caf9; // Primary color for dark mode

    .el-button--primary {
        background-color: $--color-primary !important;
        border-color: $--color-primary !important;

        &:hover {
            background-color: darken($--color-primary, 10%) !important;
            border-color: darken($--color-primary, 10%) !important;
        }
    }
}
When using !important, consider specificity carefully to avoid affecting other styles.

❓ Questions and Answers

Q1: Do I need to recompile styles after changing SCSS theme variables?

Yes. Changes to SCSS variables generally require recompiling the styles before they take effect.

Q2: Which Element UI versions support an official dark theme?

Element UI does not provide the official dark mode support described for Element Plus. Check the compatibility of any third-party dark theme with your Element UI version.


🧪 Exercises

Exercise 1

Change $--color-primary to a color you like and observe how it affects buttons, links, and other elements.

// element-variables.scss
$--color-primary: #3498db;

Exercise 2

Implement a dark mode toggle button that adds or removes el-theme-dark on the root element when clicked.

Template code:

<template>
    <div :class="{ 'el-theme-dark': isDark }">
        <el-button @click="toggleDark">Toggle dark mode</el-button>
        <el-button type="primary">Primary button</el-button>
    </div>
</template>

<script>
export default {
    data() {
        return {
            isDark: false,
        };
    },
    methods: {
        toggleDark() {
            this.isDark = !this.isDark;
        },
    },
};
</script>

🎉 You now know how to customize Element UI theme colors, toggle dark mode, and override SCSS variables!