> Markdown version of [Importing Style Sheets](https://vaadin.com/docs/v14/flow/styling/importing-style-sheets). Section index: [llms.txt](https://vaadin.com/docs/v14/flow/llms.txt)

# Importing Style Sheets

Import additional style sheets to customize component themes and style application views.

## <a id="prerequisites"></a>Prerequisites

You should be familiar with [Style Scopes](https://vaadin.com/docs/v14/flow/styling/style-scopes.md), to know whether you need to import a style sheet to the global scope or to a component scope.

To learn how static resources are handled, including where CSS files should be placed in your project, see [Storing and Loading Resources](https://vaadin.com/docs/v14/flow/importing-dependencies/tutorial-ways-of-importing.md).

## <a id="example"></a>Example

In server-side views (Java), use the [`@CssImport`](https://vaadin.com/api/platform/com/vaadin/flow/component/dependency/CssImport.html) annotation to import local/bundled style sheets and the `@StyleSheet` annotation to import external/linked style sheets. The `@CssImport` annotation can also be used to import component-specific style sheets.

**Java** — `ImportingStyleSheets.java`

```java
// Import a style sheet into the global scope
@CssImport("./styles/shared-styles.css")

// Import a style sheet into the local scope of the TextField component
@CssImport(value = "./styles/text-field.css",
           themeFor = "vaadin-text-field")

// Link to external style sheets
@StyleSheet("context://custom-font.css")
@StyleSheet("https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
public class MyApplication extends Div {
}
```

In client-side views (JavaScript), you need to write style sheets as JavaScript modules. You can only import local/bundled, component-specific style sheets using this method.

**JavaScript** — `ImportStyleSheets.js`

```javascript
// Import the function that allows you to register style sheets for components
import { registerStyles } from '@vaadin/vaadin-themable-mixin/register-styles.js';

// Import the tagged template function that is used to define
// a multi-line CSS string
import { css } from '@vaadin/vaadin-themable-mixin/register-styles.js';

// Define and register a style sheet for the <vaadin-text-field> component
registerStyles('vaadin-text-field', css`
  [part="input-field"] {
    border: 1px solid;
    ...
  }
`);
```

In HTML templates, use the standard `<link rel="stylesheet">` and `<style>` elements.

**HTML** — `ImportStyleSheets.html`

```html
<link rel="stylesheet" type="text/css" href="/custom-font.css">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<style>
  /* Your CSS here */
</style>
```

## <a id="global-styles"></a>Global Styles

There are two ways to import style sheets to the global scope:

1. Local/bundled style sheets, which are loaded with the application’s frontend bundle, together with all JavaScript.

2. External/linked style sheets, which are not bundled with the application’s frontend bundle but loaded and cached separately by the browser.

### <a id="global-bundled"></a>Local Style Sheets

Local/bundled style sheets are inlined to the application bundle during a production build, together with other client-side resources. Bundling is recommended for styles that change together with the application logic or component implementations, as the browser can cache them as a single unit made of closely related resources.

In server-side views (Java), the [`@CssImport`](https://vaadin.com/api/platform/com/vaadin/flow/component/dependency/CssImport.html) annotation will handle all the boilerplate for you, you only need to reference a regular CSS file.

> **Caution:** The `@CssImport` annotation does not work in Vaadin 14 compatibility mode (Bower and HTML imports).

**Java** — `ImportingStyleSheets.java`

```java
// Import a style sheet into the global scope
@CssImport("./styles/shared-styles.css")
public class MyApplication extends Div {
}
```

When importing style sheets in client-side views, you need to take polyfilled browsers into consideration – namely Internet Explorer 11. Those browsers do not have native style encapsulation via shadow DOM and require that styles are scoped (using CSS selectors) manually or via polyfills.

One way to scope global styles and prevent them from affecting Polymer-based components in polyfilled browsers is to use the `<custom-style>` element from the Polymer library.

See the [API documentation for `<custom-style>`](https://polymer-library.polymer-project.org/3.0/api/elements/custom-style)

**HTML** — `ImportStyleSheets.html`

```html
<script type="module">
  import '@polymer/polymer/lib/elements/custom-style.js';
</script>

<custom-style>
  <style>
    /* The CSS here will be scoped and won't
       affect internal component styles */
  </style>
</custom-style>
```

### <a id="global-linked"></a>External Style Sheets

External/linked style sheets can be used to import styles without inlining the contents to the application bundle. This allows the browser to load and cache the style sheet separately from the rest of the application.

External style sheets need to be accessible from a URL, and therefore need to be placed in the public resource folder in your web application. They can also come from outside your web application, for example from a different domain or a Content Delivery Network (CDN).

In server-side views (Java), the `@StyleSheet` annotation can be used to import style sheets from an external URL, or from a URL within your application. The latter type of URLs are prefixed with `context://`, which points to the root of the public resources folder of your application.

**Java** — `ImportingStyleSheets.java`

```java
// Link to external style sheets
@StyleSheet("context://custom-font.css")
@StyleSheet("https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
public class MyApplication extends Div {
}
```

In client-side templates (HTML), use the standard `<link rel="stylesheet">` element to import external style sheets.

**HTML** — `ImportStyleSheets.html`

```html
<link rel="stylesheet" type="text/css" href="/custom-font.css">
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
```

> **Caution:** The CSS rules in external style sheets are not protected from overriding component-specific styles in browsers without native style encapsulation (shadow DOM) – namely Internet Explorer 11. Use local style sheets in cases where this might be an issue, for example when including CSS which is not maintained by you.

## <a id="component-styles"></a>Component Styles

These instructions apply to components that use [`ThemableMixin`](https://github.com/vaadin/vaadin-themable-mixin/), including all Vaadin components.

Component styles are scoped per component and allow you to customize components without worrying about side-effects to other parts of your application. Component styles are always inlined to the application bundle – they can’t be external/linked style sheets.

Component-scoped style sheets **apply to all instances of the component** across the entire application.

In server-side views, use the `@CssImport` annotation. Refer to a `.css` file and specify the tag/element name of the component you wish to apply the style sheet to.

**Java** — `ImportingStyleSheets.java`

```java
// Import a style sheet into the local scope of the TextField component
@CssImport(value = "./styles/text-field.css",
           themeFor = "vaadin-text-field")
```

In client-side views, use the `registerStyles` function. Specify the tag/element name of the component and provide string of CSS that you wish to apply to the component.

You **need to call `registerStyles` before the component it targets** is registered and upgraded by the browser. In other words, before the first instantiation of the component.

**JavaScript** — `ImportStyleSheets.js`

```javascript
// Import the function that allows you to register style sheets for components
import { registerStyles } from '@vaadin/vaadin-themable-mixin/register-styles.js';

// Import the tagged template function that is used to define
// a multi-line CSS string
import { css } from '@vaadin/vaadin-themable-mixin/register-styles.js';

// Define and register a style sheet for the <vaadin-text-field> component
registerStyles('vaadin-text-field', css`
  [part="input-field"] {
    border: 1px solid;
    ...
  }
`);
```

You can use the same style sheet for multiple components simultaneously by providing a space-separated list of component names instead of a single component name. Wildcard element names are supported as well, for example, `vaadin-*-overlay`.

**Java** — `ImportingStyleSheets.java`

```java
@CssImport(value = "./styles/shared-overlays.css",
           themeFor = "vaadin-select-overlay vaadin-combo-box-overlay")
```

**JavaScript** — `ImportStyleSheets.js`

```javascript
// Register the same style sheet for both the <vaadin-select-overlay>
// and the <vaadin-combo-box-overlay> elements
registerStyles('vaadin-select-overlay vaadin-combo-box-overlay', css`
  /* Styles which will be imported in
     vaadin-select-overlay and vaadin-combo-box-overlay
     local style scopes */
`);
```

`C8AD563D-0B35-4BA2-B81F-C73BD3FC488E`
