# Styles
The article guides you through defining and structuring your styles for creating themeable web components with the UI5 Web Components framework and tools.
## CSS Variables
[CSS variables](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties) (also known as Custom properties) are entities that represent specific values to be reused throughout a document.
They are particularly beneficial for implementing theming in web design due to their reusability nature.
Unsurprisingly, UI5 Web Components theming relies entirely on CSS Variables.
## Themes
A theme is a collection of CSS Variables, each representing a specific design property such as color, font size, or spacing. Switching between themes simply involves updating the values of these variables. The underlying CSS rules or styles remain the same, but they reference different variable values depending on the active theme, allowing for a seamless transition between different visual styles.
UI5 Web Components comes with several built-in themes:
- **Quartz themes:** Quartz Light, Quartz Dark, Quartz High Contrast Black and Quartz High Contrast White
- **Horizon themes:** Horizon Morning, Horizon Evening, Horizon High Contrast Black and Horizon High Contrast White
To implement these themes, the UI5 Web Components uses and depends on the `SAP CSS variables`, available in the [@sap-theming/theming-base-content](https://www.npmjs.com/package/@sap-theming/theming-base-content) package.
**Note:** The [theming-base-content](https://github.com/SAP/theming-base-content/) project is developed by SAP and provides color, font, and metric definitions of SAP themes to be used by application UIs and UI frameworks.
The package provides collections of CSS variables per theme - one for `Morning Horizon`, one for `Quartz Light` and so on.
Let's exlore a small part of these collections. You will notice that both collections include the same set of variables, but with different values:
```css
/* Horizon */
root {
--sapBrandColor: #0070f2;
--sapHighlightColor: #0064d9;
--sapBaseColor: #fff;
--sapShellColor: #fff;
--sapBackgroundColor: #f5f6f7;
--sapTextColor: #1d2d3e;
```
```css
/* Quartz */
:root {
--sapBrandColor: #0a6ed1;
--sapTextColor: #32363a;
--sapHighlightColor: #0854a0;
--sapBaseColor: #fff;
--sapShellColor: #354a5f;
--sapBackgroundColor: #f7f7f7
```
So, to make the UI5 Web Components themeable, internally, in the styles of all our web components we use these CSS Variables:
```css
/* ui5-text web component */
:host {
font-size: var(--sapFontSize);
font-family: var(--sapFontFamily);
color: var(--sapTextColor);
}
```
Instead of having multiple CSS files per theme, we create a single web components CSS file used in all themes and apply one or the other CSS variables collection for the respective theme.
**Note:** It's highly likely that when developing web components for SAP applications, it's often necessary to implement and support the same SAP themes. **Here comes the UI5 Web Components framework and tools providing out-of-the-box theming setup**.
## Themeable Web Components
Now that we've explained the essential ingredients for theming, let's explore the practical steps involved.
If you already went through the [Create Web Components Project](./01-package.md) article at the beginning of the `Development` section and kickstarted a project, you probably noticed many theming-related files in the `src/themes` folder.
These CSS files are part of the theming setup that's in place after the project initialization is done via `npm init @ui5/create-webcomponents-package` (as explained [here](./01-package.md)).
The theming setup is based on having a single web component CSS file, containing all CSS rules, that will be used for all themes. Some CSS Variables are global, such as `--sapBrandColor`, `--sapBackgroundColor`, and `--sapTextColor`, and automatically included by the framework and available for usage (as explained in the previous section). Furthermore, they are required to implement the standard SAP themes.
In addition, you can define your own CSS Variables and provide different values for them for the different themes. Set these CSS Variables in the `parameters-bundle.css` file for each theme. These files are the entry points for the styles build. Once you define them, the framework will be responsible for applying the respective CSS Variables according to the configured theme.
| File | Purpose |
| --------------------------------------------------- | -------------------------------------------------------------------------------------------------- |
| `src/themes/MyComponent.css` | The web component CSS file with all CSS rules, used in all themes and inserted in the shadow root. |
| `src/themes/sap_horizon/parameters-bundle.css` | Values for the component-specific CSS Variables for the `sap_horizon` theme |
| `src/themes/sap_horizon_dark/parameters-bundle.css` | Values for the component-specific CSS Variables for the `sap_horizon_dark` theme |
| `src/themes/sap_horizon_hcb/parameters-bundle.css` | Values for the component-specific CSS Variables for the `sap_horizon_hcb` theme |
| `src/themes/sap_horizon_hcw/parameters-bundle.css` | Values for the component-specific CSS Variables for the `sap_horizon_hcw` theme |
| `src/themes/sap_fiori_3/parameters-bundle.css` | Values for the component-specific CSS Variables for the `sap_fiori_3` theme |
**Note:** It's up to you whether to put the CSS Variables directly in the `parameters-bundle.css` files for the different themes or to import them from separate `.css` files.
Practically speaking, the theming setup appears as follows:
- The web component styles:
```css
/* src/themes/MyComponent.css */
:host {
color: var(--sapTextColor); /* using global vars */
border-color: var(--my-component-border-color); /* using component-specific vars */
}
```
- Component-specific variables (for example `sap_horizon`):
```css
/* src/themes/sap_horizon/parameters-bundle.css */
:host {
--my-component-border-color: blue;
}
```
- Component-specific variables (for example `sap_horizon_dark`):
```css
/* src/themes/sap_horizon_dark/parameters-bundle.css */
:host {
--my-component-border-color: lightblue;
}
```
**Note:** Variables should be defined on `:host` only if `cssVariablesTarget: "host"` is configured in `package-scripts.cjs`. If using `cssVariablesTarget: "root"` (or no configuration), define variables on `:root` instead.
The last piece is to connect the web component styles (the main file used in all themes) via the [@customElement](./02-component.md) decorator:
```ts
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
// Styles
import myStyles from "./generated/themes/MyComponent.css.js";
@customElement({
tag: "my-component",
styles: myStyles,
})
class MyComponent extends UI5Element {
}
```
**That's it! The framework will connect the styles with the web component and automatically apply the appropriate CSS variables for the `sap_horizon` and `sap_horizon_dark` themes**. This allows developers to focus on writing CSS without worrying about building, fetching, or loading these styles.
## CSS Variables Target
The `cssVariablesTarget` configuration in `package-scripts.cjs` determines where your component-specific CSS variables (defined in `parameters-bundle.css`) are injected:
- **`cssVariablesTarget: "host"`** - CSS variables are added to each component's shadow root. This provides better encapsulation as variables are scoped to each component instance.
- **`cssVariablesTarget: "root"`** (or omitted) - CSS variables are added to the global `:root` selector. This makes variables available globally across the entire document.
```js
// package-scripts.cjs
const options = {
cssVariablesTarget: "host",
};
const scripts = getScripts(options);
module.exports = {
scripts
};
```
When using `cssVariablesTarget: "host"`, component-specific variables are injected into each shadow root, enabling customization via `--sap*` variables on a component or specific scope level. This allows for more granular theming control, where different instances of the same component can be styled differently based on their context.
```html