Styling Applications

Learn how to apply app-specific styling across application views and application-specific components and how to use images and icons.

Prerequisites

You should be familiar with Importing Style Sheets to the different Style Scopes in your application.

Styling Views and Application-specific Components

Your views and application-specific components are usually built by composing existing lower-level components. Styling these components is a little different from styling the internals of built-in components. See Styling Components for more information on styling built-in components.

Server-Side Views and Custom Components

Server-side views and custom components (Java), extending from Component, are rendered in the global style scope. You can apply traditional CSS styling techniques for them.

The most common technique is to add CSS classes to the components in the view and target those classes in your style sheets.

MyView.java

Expand code

  1. @CssImport("./styles/my-view.css")
  2. public class MyView extends VerticalLayout {
  3. public MyView() {
  4. addClassName("my-view");
  5. H1 heading = new H1("My View Title");
  6. heading.addClassName("heading");
  7. Paragraph text = new Paragraph("Thanks for stopping by. Not much to see here. Click the button below to go back to start.");
  8. Button backButton = new Button("Go Back");
  9. backButton.addClassName("back-button");
  10. add(heading, text, backButton);
  11. }
  12. }

styles/my-view.css

Expand code

  1. .heading {
  2. margin-top: 0;
  3. }
  4. p {
  5. /* Assuming we are using the Lumo theme. If not, fall back to 1.2em */
  6. font-size: var(--lumo-font-size-l, 1.2em);
  7. }
  8. .back-button {
  9. background-color: var(--lumo-shade-90pct);
  10. }

Client-side Views, Templates and Components

Client-side view, templates, and components are usually authored using Polymer, extending PolymerElement. Polymer elements render their HTML template inside shadow DOM and create a new style scope.

The instructions in Styling Polymer Templates apply to client-side views.

If you wish to author your client-side views using some other technology (for example LitElement), refer to the styling instructions of that technology.

Using Images

Static images, which need to be accessible by the browser, should be placed in the /src/main/webapp/ folder. For Spring Boot -based projects use /src/main/resources/META-INF/resources/.

See Storing and Loading Resources for more information about how static resources are handled.

JavaJavaScriptCSS

In server-side views (Java), use the Image component.

StylingApplications.java

Expand code

  1. Image image = new Image("context://my-image.png", "Alternative text");

Using Icons

In general, icons are not different from images, as they show a graphic on the page. For bitmap-based icons (JPG, PNG, WebP, etc.), follow the above instructions for using images.

Since icons have some additional requirements on top of images, such as the need to be able to change their size and color using CSS, they are usually created using the SVG vector format and embedded directly inside the resulting HTML, instead of being referenced via CSS background-image: url(icon.svg) or HTML <img src="icon.svg">.

Vaadin has a component specifically for showing SVG icons. In addition, Vaadin offers a large collection of icons for you to use (with both SVG and font formats).

Note, that SVG icons are considered as the best practice. Font icons have some well documented caveats, especially regarding screen reader usage.

JavaJavaScript

In server-side views (Java), use the Icon component. There are two ways to create an icon from the Vaadin Icons collection.

The Icon component can be used in all the same places as any other component. Some components, such as Button, have API specifically for using icons.

StylingApplications.java

Expand code

  1. Icon edit = new Icon(VaadinIcon.EDIT);
  2. Icon close = VaadinIcon.CLOSE.create();

The Lumo theme has its own set of icons (see Lumo Icons), which you can also use.

JavaJavaScript

StylingApplications.java

Expand code

  1. Icon icon = new Icon("lumo", "clock");

Creating a Custom Icon Set

If you want to create your own set of icons to use, you first need to package the SVG icons into a bundle. That makes them usable in client-side views and the <iron-icon> component/element.

For server-side views, you can create a similar Enum as VaadinIcon to make it easy to use your custom icon set. See the implementation of Vaadin Icons for inspiration.