Changing Background Color in Vuetify.js and Nuxt.js

Vuetify.js is a Material Design component framework that allows customizing colors easily via themes. However, one color that is missing in the theme config is the background color. In this article we will fix that via CSS variables!

We will be using Nuxt.js — a meta-framework for Vue.js. However, very similar techniques can be applied to vanilla Vuetify.js.

TL;DR — go straight to the final code on Codesandbox:

As described in the official Vuetify.js documentation, it’s possible to choose between a light and dark theme and set the basic colors in the nuxt.config.js file vuetify section:

However, there’s nothing mentioned about changing the background color. Does this mean that we are stuck with a white background in light theme and a dark gray background in dark theme? Of course not!

Pure CSS Solution

The simplest way to change the background color is to set the style of the app element in default.vue layout:

This works just fine for simple applications, but wouldn’t it be nice to control all the colors centrally in the nuxt.config.js file? We can combine the above code with what we’ve learnt in my Advanced Color Management article and define a custom background color as follows:

Note that we also need to enable the customProperties option to use the new color variable in CSS (see Vuetify.js documentation).

Now all we need to do is apply the background color style to the app HTML element. We can do this directly in default.vue layout as shown above, by changing the layout style to:

Note that I set the fallback values for my variable. It’s useful to do that in case we forget to configure the background color in theme — otherwise we will just see a generic white background.

Canonical SASS Solution

There exists an alternative canonical solution using SASS variables, which may work better with existing Vuetify.js components that define their own backgrounds.

First we need to include the custom SASS variables file in the nuxt.config.js file:

Note that we need to enable the treeShake option for the SASS variables to be taken into account.

Next we need to create the assets/variables.scss file with the following contents:

Voilà, and that’s it! You can find a complete example based on the above idea in this repo:

Cover photo by David Pisnoy on Unsplash.

Full-Stack Developer / Agilist / Free and Open Source Software Fan.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store