How can I use CSS for Vaadin components?
You can create you own custom theme. See https://vaadin.com/book/-/page/themes.creating.html how to do that.
In this theme you have a css style sheet where you can put your rules.
On every Component you can use the addStyleName function to add an additional class name:
Table table = new Table("MyCaption");
table.addStyleName("mystyle");
Now you can use this in your style sheet:
@import "../reindeer/styles.css";
.mystyle{
overflow: hidden !important;
}
How can i apply CSS to components in VAADIN 8
This is not related, but are you actually using a reindeer
theme?
Otherwise, you should put you styles in your own theme file (the default one generated from an archetype is mytheme.scss
)
It's suggested to leave styles.scss
as it is. Also, it's mentioned in comments section of the file:
This file prefixes all rules with the theme name to avoid causing conflicts with other themes. The actual styles should be defined in mytheme.scss
If you want, you could add your styles there as well under
.mytheme {
@include addons;
@include mytheme;
.testStyle{
color: red;
background: #012345;
background-color: #012345;
}
}
While it works, I will still suggest to add them to your custom scss file (Based on your folder name it is apptheme.scss
)
Mine mytheme.scss
looks like this:
@import "../valo/valo.scss";
@mixin mytheme {
@include valo;
.testStyle{
color: red;
background: #012345;
background-color: #012345;
}
}
After styles are applied, button looks like this:
Style files are located under webapp/VAADIN/themes/mytheme
Vaadin Fusion Styling of Sub-Components
As the answer made its way to me on a different path, here is the solution for SO:
You can only apply styles to Vaadin components and their sub-components using a custom theme.
Here is a slightly extended way to set this up using a parent theme.
- Create a local custom theme in your app
- path is
frontend/themes/<my-theme-name>/
- must contain a sub-directory
components
(to style Vaadin components) - must contain a
styles.css
(even empty) - must contain a
theme.json
with contents
but there are other keys for theme.json like{
"parent": "<my-parent-theme>"
}importCss
,documentCss
orassets
- the parent theme can be a pom dependency
- path is
- Use the custom theme in your Application.java:
@Theme(value = "<my-theme-name>")
public class Application extends SpringBootServletInitializer ... - Then you can add style files locally to style Vaadin components, e.g.
- add
frontend/themes/<my-theme-name>/components/vaadin-upload.css
:[part=file-list]::before {
content: "These are the current files:";
} frontend/themes/<my-theme-name>/components/vaadin-upload-file.css
:[part=name] {
background-color: yellow;
}
- add
This applies to Vaadin21 (e.g. in Vaadin19 using a parent theme is not working this way).
Related Topics
Regex Look-Behind Without Obvious Maximum Length in Java
How to Initialize List<String> Object in Java
Android - How to Take Screenshot Programmatically
Can't Get Download Url from Firebase Storge in Android
How to Kill Currently Running Task in Android
Get a JSON Object from a Http Response
How to Get the Selected Value of a Spinner
Is There Any Library or Algorithm for Persian (Shamsi or Jalali) Calendar in Android
How to Implement Enum with Generics
Right Way to Implement Equals Contract
Easiest Way to Convert a List to a Set in Java
Remove Padding/Margin from Javafx Label
Map.Clear() VS New Map:Which One Will Be Better
How to Skip Initial Data and Trigger Only New Updates in Firestore Firebase
Android Studio Cannot Resolve Symbol But Code Executes Correctly