Annotation Interface StyleSheet


Loads a style sheet into the browser at runtime, by default as a <link rel="stylesheet"> element. With LoadMode.INLINE the contents of the file are inlined in a <style> element instead, and a style sheet with a layer() that is not inlined is loaded with an @import rule. The referenced file is served as a static resource by the servlet container; it is not bundled. Use CssImport when the CSS file should be processed as a bundle source by Vite at build time.

Source locations:

  • WAR-packaged applications: src/main/webapp/. The servlet container serves META-INF/resources/ only from JARs in WEB-INF/lib, not from the application's own classes.
  • Spring Boot applications: src/main/resources/META-INF/resources/, src/main/resources/public/ or src/main/resources/static/.
  • Add-on JARs: META-INF/resources/. Files placed there are served automatically by the servlet container when the JAR is on the classpath.

URL resolution is identical whether the annotation is placed on an AppShellConfigurator, a route component, or any other Component. See value() for the resolution rules.

When this annotation is placed on the AppShellConfigurator, the referenced style sheets are added globally to the <head> of the generated index.html during bootstrap, so LoadMode values are ignored.

This annotation is a recommended way to select a Vaadin theme and to import stylesheets. To be put on a class implementing AppShellConfigurator in this case. Example of usage:

     // theme selection
     @StyleSheet(Aura.STYLESHEET) // or Lumo.STYLESHEET
     public class Application implements AppShellConfigurator {
         ...
     }

     OR

     // styles.css from src/main/webapp/ (WAR) or
     // src/main/resources/META-INF/resources/ (Spring Boot)
     @StyleSheet("styles.css")
     public class Application implements AppShellConfigurator {
         ...
     }
     // and using @import in the styles.css:

     @import '@vaadin/aura/aura.css';
     // your custom styles ...
 

For adding multiple style sheets, you can use this annotation multiple times. It is guaranteed that dependencies will be loaded only once.

NOTE: while this annotation is not inherited using the @Inherited annotation, the annotations of the possible parent components or implemented interfaces are read when sending the dependencies to the browser.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static @interface 
    Internal annotation to enable use of multiple StyleSheet annotations.
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Style sheet file URL to load before using the annotated Component in the browser.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Name of the CSS cascade layer to load the style sheet into.
    Determines the dependency load mode.
  • Element Details

    • value

      String value
      Style sheet file URL to load before using the annotated Component in the browser.

      URL resolution rules, in order:

      • http://..., https://..., //... ? used unchanged.
      • context://foo.css ? resolved against the servlet context root (independent of the Vaadin servlet mapping).
      • base://foo.css ? resolved against the page's <base> URI, i.e. the Vaadin servlet mapping path. Use this as an explicit opt-in for servlet-mapping-relative loading.
      • /foo.css ? used unchanged as an absolute server path.
      • A URL with any other scheme (a value containing ://) ? used unchanged.
      • ./foo.css ? leading ./ is stripped, then treated as context-root relative.
      • foo.css, styles/foo.css ? context-root relative, i.e. equivalent to context://foo.css.
      • Values containing .. (path traversal) are rejected with a warning.
      Returns:
      a style sheet file URL
    • loadMode

      LoadMode loadMode
      Determines the dependency load mode. Refer to LoadMode for the details.
      Returns:
      load mode for the dependency
      Default:
      EAGER
    • layer

      String layer
      Name of the CSS cascade layer to load the style sheet into.

      Styles in a layer have lower priority than styles outside of any layer, regardless of selector specificity. Loading a theme into a layer, for example @StyleSheet(value = Aura.STYLESHEET, layer = "aura"), lets application styles override it without matching its selector specificity.

      The name follows the CSS <layer-name> syntax: one or more identifiers separated by dots, such as theme or theme.base. An empty value, the default, loads the style sheet without a layer.

      A style sheet is loaded only once per URL, so the same URL declared with different layers stays in the layer it was first loaded into.

      Returns:
      the cascade layer name, or an empty string to not use a layer
      Since:
      25.4
      Default:
      ""