Table of contents
1.
Introduction
2.
Design System Resources
3.
Styling Views and Custom Components
3.1.
Implementation
3.2.
Java Code
3.3.
CSS code 
4.
Importing Style Sheets
4.1.
🍁 Bundled Style Sheets
4.1.1.
Global Scope
4.1.2.
Local Scope
4.2.
🍁External Style Sheets
4.3.
Java Code
4.4.
Java Code
5.
Frequently Asked Questions
5.1.
Is vaadin frontend or backend?
5.2.
Can we add custom CSS and JavaScript?
5.3.
Is Vaadin free to use?
6.
Conclusion
Last Updated: Mar 27, 2024

Styling Flow Application

Author Neha Chauhan
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Vaadin}> is a platform for developing web applications using Java. Vaadin Flow is an open-source framework that allows developers to build web applications with great UI (User Interface) without any knowledge of HTML or Javascript. The Design System Resources in Vaadin provide tools for organising the styles of the UI (User Interface). To separate the styling from the main logic, Vaadin uses CSS (Cascading Style Sheets) files in the development environment.

Styling Flow Application

This blog will discuss styling flow applications by adding CSS style sheets to the Vaadin application. We will also discuss, the two ways of importing style sheets while styling flow applications.

Design System Resources

There are mainly five modules in the Design System Resources of Vaadin:

⭐ Foundation

⭐ Figma Libraries

⭐ CSS Utility Classes 

⭐ Custom Themes

⭐ Theme Editor

These modules provide resources for system design. 

🛑 In the Foundation module, there are CSS Custom Properties for applying consistent Typographic styles (font styling), Fonts, Colors, Shapes and sizes across the application.

🛑 The Figma Libraries allow the developers to prototype and design application layouts in Figma. The components in the libraries use the default Lumo styling.

🛑 The CSS Utility Classes can be used to build user interfaces without using custom CSS.

🛑 We can also build custom themes by overriding the default Lumo theme. 

🛑 Theme Editor is an online tool used to create customized themes on top of Lumo Themes. 

 

Conversation Vaadin

Styling Views and Custom Components

The traditional way of styling the user interface is by including a CSS file in the code. Similarly, styling flow application is also done by adding a CSS file. The CSS classes are added to the view members, and these classes are targeted in the CSS style sheet.

The following example explains how to add a CSS style sheet - 

Implementation

Java Code

@CssImport("./styles/my-styles.css")

// This is the Java code for setting the elements 
public class MyView extends VerticalLayout {
  public MyView() {
   	
   	// setting a CSS class
    addClassName(“my-styles”);

	// creating a ‘title’ object for the H1 class
    // adding a ‘heading’ CSS class property to the ‘title’ object
    H1 title = new H1(“Coding Ninjas”);
   title.addClassName(“heading”);

	// creating a ‘text’ object for the Paragraph class
    Paragraph text = new Paragraph(“Don’t just be a coder, become a Ninja Coder!”);

	// creating a back button
    // adding a ‘back-button’ CSS class property to the ‘button’ object 
    Button button= new Button(“Back”);
    button.addClassName(“back-button”);

	add(heading, text, backButton);
  }
}
You can also try this code with Online Java Compiler
Run Code

CSS code 

// This is the ‘my-style.css’ file 
.heading {
  margin-top: 0;
}
p {
    font-size: var(--lumo-font-size-l, 1.2em);
}
.back-button {
  background-color: var(--lumo-shade-90pct);
}

To import the style sheets, use @CssImportat the beginning of the code. Another way of adding style sheets is by using @StyleSheet.

Importing Style Sheets

Style sheets can be of any one style, bundled or external. Bundling refers to adding multiple style sheets to the application resource folder. Style sheets can be imported from an external URL or from a URL from within the application.

 

Importing sheets

🍁 Bundled Style Sheets

Bundled style sheets are included in the application bundle and other client-side resources,  during a production build. Use the @CssImport annotation to import style sheets from the application source folder.

The @CssImport can be used to import style sheets in the global scope.

Global Scope

// Import a style sheet into the global scope
@CssImport("./styles/cn-shared-styles.css")
public class MyNinjaApplication extends Div {
}
You can also try this code with Online Java Compiler
Run Code


The @CssImport can be used to import style sheets that are component-specific. 

Local Scope

// Import a style sheet into the local scope of the Horizontal-layout component
@CssImport(value = "./styles/Horizontal-layout .css",
           themeFor = "vaadin-Horizontal-layout")
You can also try this code with Online Java Compiler
Run Code


The style sheet should be stored in the path - 

{project directory}/frontend/styles/my-styles.css

Bundling is advised for styles that change concurrently with application logic or component implementations since the browser can cache them as a single entity of linked resources.

🍁External Style Sheets

Import style sheets from the external URL or from a URL within your application using the @StyleSheet annotations. 

Java Code

//import the style sheet from a URL within the application
@StyleSheet("context://custom-font.css")
You can also try this code with Online Java Compiler
Run Code

Java Code

//import the style sheet from an external URL
@StyleSheet("https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css")
public class MyApplication extends Div {
}
You can also try this code with Online Java Compiler
Run Code


Without including the contents in the application bundle, styles can be imported using external/linked style sheets. By doing so, the style sheet may be loaded and cached by the browser without affecting other parts of the program. 

The URLs from within the application are prefixed with context://, which points to the root of the public resources folder of the application. The URLs should be placed in the public resources folder in the web application.

Frequently Asked Questions

 

FAQ Vaadin

Is vaadin frontend or backend?

Vaadin Flow is an open source framework that enables both server-side and client-side development using Java as the programming language.

Can we add custom CSS and JavaScript?

Yes. We can add our own CSS files and CSS class names to any component to set custom CSS properties. We can also use Theme Editor to create custom themes. We can also execute and include custom JavaScript when needed. These are the ways of styling flow applications. 

Is Vaadin free to use?

Yes. The Vaadin framework and its components are free to use for any purpose. We can develop fully-functional, complete web applications. 

Conclusion

Congratulations on finishing the blog! In this blog, we have discussed the concept of styling flow applications. We learned about using bundled and external style sheets. 

After reading about styling flow application, if you are more curious about other concepts related to Vaadin, please check our other blogs on Vaadin - 

🔥 Vaadin Environmental-Setup

🔥 Vaadin Architecture

🔥 Vaadin User Interface Components

🔥 Vaadin Board 

🔥 GWT vs Vaadin

If you liked our article, do upvote our article and help other ninjas grow💪. You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more!

Head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more!!

We wish you Good Luck!🎈 Please upvote our blog 🏆 and help other ninjas grow.

Live masterclass