Search code examples
javascripthtmlvue.jselement-uielement-plus

Enable dark mode in ElementPlus with CDN


How do i set the theme to dark when using ElementUI + Vue with CDNs? I know that with npm you edit some scss files but i dont use npm.

  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1.0" />
    <script src="https://unpkg.com/vue@next"></script>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css">
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-plus"></script>
    <title>Element Plus demo</title>
  </head>
  <body>
    <div id="app">
      <el-button>{{ message }}</el-button>
    </div>
    <script>
      const App = {
        data() {
          return {
            message: "Hello Element Plus",
          };
        },
      };
      const app = Vue.createApp(App);
      app.use(ElementPlus);
      app.mount("#app");
    </script>
  </body>
</html>```

Solution

  • You simply add the class dark to the html tag.

    This is also documented here: https://element-plus.org/en-US/guide/dark-mode.html#how-to-enable-it

    <html class="dark">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />
        <script src="https://unpkg.com/vue@next"></script>
        <!-- import CSS -->
        <link rel="stylesheet" href="https://unpkg.com/element-plus/theme-chalk/index.css">
            <link rel="stylesheet" href="https://unpkg.com/element-plus/theme-chalk/dark/css-vars.css">
        <!-- import JavaScript -->
        <script src="https://unpkg.com/element-plus"></script>
        <title>Element Plus demo</title>
      </head>
      <body>
        <div id="app">
          <el-button>{{ message }}</el-button>
        </div>
        <script>
          const App = {
            data() {
              return {
                message: "Hello Element Plus",
              };
            },
          };
          const app = Vue.createApp(App);
          app.use(ElementPlus);
          app.mount("#app");
        </script>
      </body>
    </html>