> Markdown version of [Stylesheets](https://vaadin.com/docs/next/styling/stylesheets). Section index: [llms.txt](https://vaadin.com/docs/next/styling/llms.txt)

# Stylesheets

In most project configurations, [stylesheets](https://vaadin.com/docs/next/styling/stylesheets.md) are placed in the `src/main/resources/META-INF/resources` folder. In non-Spring projects packaged as WAR files they are placed in the `src/main/webapp` folder.

In both cases they are loaded with the `@StyleSheet` annotation.

Usually, you should apply `@StyleSheet` annotations to the main application class that implements the `AppShellConfigurator` interface. This ensures that the CSS in it is applied globally, up-front, when the application UI is loaded into the browser.

```java
@StyleSheet("styles.css")
public class Application implements AppShellConfigurator {
 ...
}
```

You can split your CSS over multiple stylesheets, and load them either with separate `@StyleSheet` annotations, or load additional stylesheets using the CSS `@import` rule.

src/main/resources/META-INF/resources/styles.css

```css
@import "additional-styles.css";

/* your CSS goes here */
```

Stylesheets are applied to your UI in the order in which the `@StyleSheet` annotations and `@import` rules occur in your code.

## <a id="automatic-browser-cache-invalidation"></a>Automatic Browser Cache Invalidation

In production mode, stylesheets loaded via `@StyleSheet` automatically get a content-based hash appended to the URL (e.g., `styles.css?v-c=abc123`). This ensures browsers cache stylesheets efficiently and automatically reload them when the file content changes after a deployment. No action is needed from developers.

## <a id="custom-vaadinservlet-url-mapping"></a>Custom VaadinServlet URL Mapping

Stylesheets referenced by `@StyleSheet` annotation are loaded by the servlet container. If the application is configured to use a custom URL mapping for the `VaadinServlet` (e.g. `vaadin.url-mapping` setting in Spring applications), the referenced stylesheet must be prefixed by the `context://` protocol.

```java
@StyleSheet("context://styles.css")
public class Application implements AppShellConfigurator {}
```

## <a id="external-stylesheets"></a>External Stylesheets

All methods mentioned before for loading stylesheets also allow you to load stylesheets from external URIs, including other domains, by supplying an absolute path to `@StyleSheet` or `@import`.

```java
@StyleSheet("styles.css")
@StyleSheet("https://example.com/external-styles.css")
public class Application implements AppShellConfigurator {
 ...
}
```

`src/main/resources/META-INF/resources/styles.css`

```css
@import "https://example.com/external-styles.css";
```

You can [read more about the @import at-rule on MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/@import).

## <a id="lazy-loading-stylesheets"></a>Lazy-Loading Stylesheets

If you want to delay the loading of a particular stylesheet until it’s needed – for example if the stylesheet is big and only needed in a particular view or Flow component – you can apply the `@StyleSheet` annotation to its class instead of the main application class.

```java
@StyleSheet("calendar-view.css")
public class CalendarView extends VerticalLayout {
  ...
}
```

The stylesheet remains loaded even after the user navigates away from the view, or when the component is no longer displayed. This means you still need to ensure that the styles in it are scoped appropriately. You can use CSS class names to scope styles to a particular view or other part of the UI.

```java
@StyleSheet("calendar-view.css")
public class CalendarView extends VerticalLayout {
  public CalendarView() {
    addClassName("calendar-view");
  }
}
```

`src/main/resources/META-INF/resources/calendar-view.css`

```css
.calendar-view {
  /* style rules in here only apply to CalendarView */
  vaadin-button {
    --aura-accent-color: green;
  }
}
```

> **Note: CSS Class Names**
>
> CSS class names are identifier-attributes applied to HTML elements that scope CSS styling to them. The same class name can be applied to multiple elements, and each element can have multiple class names. They have nothing to do with Java or TypeScript classes.

Stylesheets can also be [loaded and unloaded dynamically](https://vaadin.com/docs/next/styling/advanced/dynamic-stylesheets.md) through Java code.

## <a id="loading-into-a-css-cascade-layer"></a>Loading into a CSS Cascade Layer (V25.4 pre-release)

By default, a stylesheet’s rules compete with your application’s other styles based on selector specificity. To load a stylesheet, such as a theme, so that its styles always lose to your application’s own styles regardless of specificity, put it in a CSS cascade layer using the `layer` attribute of `@StyleSheet`.

```java
@StyleSheet(value = Aura.STYLESHEET, layer = "aura")
@StyleSheet("styles.css")
public class Application implements AppShellConfigurator {
 ...
}
```

The layer name follows the CSS `<layer-name>` syntax: one or more identifiers separated by dots, such as `theme` or `theme.base`. Leaving `layer` unset, the default, loads the stylesheet without a layer.

A stylesheet is loaded only once per URL. If the same URL is declared with more than one layer, it is loaded into the first layer declared.

## <a id="react-views"></a>Stylesheets in React Views

The stylesheets that the `@StyleSheet` annotations of the application shell class load apply to React views as well.

A React view can also import a stylesheet from its TypeScript file. Place the stylesheet in the `src/main/frontend` folder, for example next to the view. Vaadin bundles it with the code of the view, and it applies when that code has loaded. Like a lazily loaded stylesheet, it stays loaded after the user navigates away, so use CSS class names to scope its styles.

`src/main/frontend/views/calendar.tsx`

```tsx
import './calendar.css';

export default function CalendarView() {
  return <div className="calendar-view">...</div>;
}
```

A stylesheet whose file name ends with `.module.css` is a CSS module, which scopes its class names to the files that import it. Vaadin gives each class a unique name, and the import returns an object that maps the class names in the file to the unique names:

`src/main/frontend/views/calendar.tsx`

```tsx
import styles from './calendar.module.css';

export default function CalendarView() {
  return <div className={styles.calendar}>...</div>;
}
```

TypeScript reports an error for these imports unless it has type declarations for them. Add the declarations to the `types.d.ts` file in the project folder, after the ones that Vaadin generates there. Keep the declaration of `*.module.css` first:

`types.d.ts`

```typescript
declare module '*.module.css' {
  const classes: Readonly<Record<string, string>>;
  export default classes;
}

declare module '*.css' {}
```

## <a id="spring-security"></a>Spring Security

If you are using [Spring Security](https://vaadin.com/docs/next/flow/security/spring.md) in your Vaadin application, you need to ensure that your stylesheets are accessible to unauthenticated users. The default Vaadin Spring Security configuration only permits access to `src/main/resources/META-INF/resources/styles.css` as well as the Aura and Lumo theme stylesheets.

To allow access to more stylesheets, you can modify your Spring Security configuration like so:

```java
public class SecurityConfig {
    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.with(VaadinSecurityConfigurer.vaadin(), configurer -> {
            ...
        });

// tag::snippet[]
        // Permit access to common static resource paths
        http.authorizeHttpRequests(auth -> {
            auth.requestMatchers(PathRequest.toStaticResources().atCommonLocations()).permitAll();
        });
// end::snippet[]

        return http.build();
    }

    ...
}
```

This allows access to common locations for static resources, one of which is the `css` path. To make stylesheets available under that path, place them in the `src/main/resources/META-INF/resources/css` folder. For example, if you place a stylesheet in `src/main/resources/META-INF/resources/css/additional-styles.css`, you can import it in your main stylesheet like so:

`src/main/resources/META-INF/resources/styles.css`

```css
@import "css/additional-styles.css";
```

When using lazy-loading, you can load the stylesheet with `@StyleSheet` like so:

```java
@StyleSheet("css/additional-styles.css")
public class SomeView extends VerticalLayout {
    ...
}
```

`caa5a55d-8bd0-425b-94dc-521a018a2e1b`
