Importing Style Sheets

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

Prerequisites

You should be familiar with Style Scopes, 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.

Example

JavaJavaScriptHTML

In server-side views (Java), use the @CssImport 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.

ImportingStyleSheets.java

Expand code

  1. // Import a style sheet into the global scope
  2. @CssImport("./styles/shared-styles.css")
  3. // Import a style sheet into the local scope of the TextField component
  4. @CssImport(value = "./styles/text-field.css",
  5. themeFor = "vaadin-text-field")
  6. // Link to external style sheets
  7. @StyleSheet("context://custom-font.css")
  8. @StyleSheet("https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
  9. public class MyApplication extends Div {
  10. }

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.

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.

JavaHTML

In server-side views (Java), the @CssImport 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).

ImportingStyleSheets.java

Expand code

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

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).

JavaHTML

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.

ImportingStyleSheets.java

Expand code

  1. // Link to external style sheets
  2. @StyleSheet("context://custom-font.css")
  3. @StyleSheet("https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
  4. public class MyApplication extends Div {
  5. }
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.

Component Styles

These instructions apply to components that use ThemableMixin, 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.

JavaJavaScript

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.

ImportingStyleSheets.java

Expand code

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

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.

JavaJavaScript

ImportingStyleSheets.java

Expand code

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