How to set css variables
Hey, how can i set css variables dynamically (data driven) during SSR?
5 Replies
Hi @Felix. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
To set CSS variables dynamically during SSR in Nuxt, you have several approaches:
Using v-bind in style blocks
You can use Vue's
v-bind
function in your style blocks to bind JavaScript variables to CSS properties:
This approach works with SSR because the value is computed on the server before rendering Stack Overflow Post > Pass a computed property to css url() with v-bind in Vue 3.
Using useHead composable
For global CSS variables, you can use the useHead
composable to inject styles with dynamic values:
This will inject the styles during SSR Docs > Getting-started > SEO and Meta.
Using a Nitro plugin for more control
For more advanced control, you can intercept the rendered HTML with a Nitro plugin:
This approach gives you full access to server-side data during rendering Docs > Getting-started > Styling.
Using runtime config
If your CSS variables depend on environment variables or configuration:
1. Define your variables in nuxt.config.ts
:
2. Access them in your component:
This ensures your CSS variables are set during SSR based on your runtime configuration Docs > Guide > Going-further > Runtime-config.
Choose the approach that best fits your specific use case and data source requirements.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223579303122567168> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@kapa.ai is it possible to keep the values reactive so that the theme can be adjusted at runtime?
Let me check the knowledge base for you again. One moment! :books:
----------------------
Yes, it's absolutely possible to keep CSS variables reactive in Nuxt so that the theme can be adjusted at runtime. Nuxt provides several approaches for this:
Using appConfig for reactive theming
Nuxt UI uses this approach for its theming system. The
app.config.ts
file allows you to define reactive configuration that can be updated at runtime with hot-module-replacement:
You can then access and update this configuration using the useAppConfig
composable and updateAppConfig
utility:
This approach is used by Nuxt UI for its color picker feature Getting-started > Theming.
Using public runtimeConfig
The public portion of runtimeConfig is reactive on the client side:
According to the documentation, "On client-side, only keys in runtimeConfig.public
are available, and the object is both writable and reactive" Docs > Guide > Going-further > Runtime Config.
Using useState for shared state
For theme values that need to be shared between server and client:
The useState
composable creates a reactive state that's shared between server and client Stack Overflow Post > Inconsistent variable values in Nuxt 3 components Part 2.
Dark mode toggle example
Here's a practical example for toggling dark mode:
This example uses the @nuxtjs/color-mode
module to handle theme switching Getting-started > Dark mode.
Remember that when updating CSS variables at runtime on the client side, you'll need to use JavaScript to update the variables, typically by modifying the style properties on the document root or a specific container element.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@223579303122567168> Kapa.ai is still learning and improving, please let me know how I did by reacting below.