Style and theme the UI
The Expivi web components don't come with built-in styles. They render plain HTML with xpv-… class names, and a separate stylesheet, the Expivi theme, styles those classes. That gives you three levels of control:
- Adjust the theme with CSS variables: colors, fonts and corner radius.
- Override specific parts by styling the
xpv-…classes. - Replace the theme with your own styles.
The finished code of this tutorial is the theming.html page of the example apps: see theming.css.
How the theme is scoped
You load the theme with one import:
import '@expivi/sdk/theme/style.css';
The theme is built with Tailwind CSS. Every rule in it, including its CSS reset, only applies inside elements with the data-xpv-theme-prefix attribute. That keeps the theme from changing the rest of your page.
<div data-xpv-theme-prefix>
<xpv-layout-composer></xpv-layout-composer>
</div>
Most components add the attribute to themselves: <xpv-product-visualizer>, <xpv-price-breakdown>, <xpv-bom-breakdown>, <xpv-request-quote> and <xpv-form-renderer>. <xpv-layout-composer> doesn't, so always put it inside an element with the attribute.
The theme includes a CSS reset (Tailwind's "preflight") for everything inside data-xpv-theme-prefix. It removes the styling of buttons, headings and lists, and its selectors are more specific than a single class. If you put your own buttons or headings inside a themed element, they lose their styles. Put the attribute on the containers of the Expivi components, not on your whole page.
Adjust the theme with CSS variables
The theme takes its colors, fonts and sizes from CSS variables. Override them to apply your brand.
The theme defines its variables on every element with data-xpv-theme-prefix, including the ones the components add to themselves. So setting a variable on :root or on a parent element has no effect. Use a selector that targets those elements and is more specific than [data-xpv-theme-prefix], for example by adding a class of your own:
@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap');
/* `.brand-theme` is a class on a parent element, such as <main class="brand-theme">. */
.brand-theme[data-xpv-theme-prefix],
.brand-theme [data-xpv-theme-prefix] {
/* Colors are space-separated RGB channels, so the theme can add transparency to them. */
--xpv-color-primary-rgb: 13 115 98;
--xpv-color-primary-dark-rgb: 8 84 72;
--xpv-color-secondary-rgb: 225 245 240;
/* Typography */
--xpv-font-family-sans: 'Nunito', system-ui, sans-serif;
/* Shape */
--xpv-border-radius-xs: 2px;
--xpv-border-radius-sm: 4px;
--xpv-border-radius-md: 6px;
--xpv-border-radius-lg: 10px;
}
Load this stylesheet after the Expivi theme. Load your font yourself; the theme only refers to it by name.
These are the variables you change most often:
| Variable | Used for |
|---|---|
--xpv-color-primary-rgb | The main brand color: selected options, buttons, links |
--xpv-color-primary-dark-rgb | Hover and active states of primary elements |
--xpv-color-secondary-rgb | A second, lighter accent color |
--xpv-color-success-rgb, --xpv-color-info-rgb, --xpv-color-warning-rgb, --xpv-color-error-rgb | Messages and validation |
--xpv-font-family-sans | The font of all components |
--xpv-border-radius-xs to --xpv-border-radius-lg | Corner radius, from small controls to large panels |
--xpv-spacing | The base spacing; most other spacing sizes are calculated from it |
--xpv-color-image-canvas-bg | The background behind a 2D visualization |
Color variables come in pairs, such as --xpv-color-primary-rgb and --xpv-color-primary. Set the -rgb variable, with three numbers separated by spaces; the other one is calculated from it.
To see all variables, open node_modules/@expivi/sdk/dist/libs/theme/theme.config.css, or inspect a component in your browser's developer tools and look at the variables on its [data-xpv-theme-prefix] element.
Override specific parts
For changes the variables don't cover, style the components' classes. The class names follow the pattern xpv-<component>__<part>, such as xpv-component__label for the label above an option. Find them with your browser's developer tools.
.brand-theme .xpv-component__label {
text-transform: uppercase;
letter-spacing: 0.04em;
font-size: 13px;
}
Include the parent class (here .brand-theme) in your selectors, so they are more specific than the theme's.
Use the theme in your own Tailwind build
If your site uses Tailwind CSS 3, you can add the Expivi theme's Tailwind configuration as a preset. Your own UI then gets utility classes that use the same CSS variables as the Expivi components, such as bg-primary and text-primary-dark.
import expiviTheme from '@expivi/sdk/theme/theme.config.mjs';
export default {
presets: [expiviTheme],
content: ['./index.html', './src/**/*.{js,ts,vue,tsx}'],
};
bg-primary then compiles to background-color: rgb(var(--xpv-color-primary-rgb)). Outside the themed elements, define the variables you use yourself, for example on :root.
Replace the theme
Because the components have no built-in styles, you can leave out @expivi/sdk/theme/style.css and write all styles yourself. This gives you full control, but you have to style every component you use, including every option control the layout composer can render. Use the default theme as a reference for the class names and states. For most sites, adjusting the variables and overriding a few classes is enough.