Basic Layouts
Vaadin features two basic layout components: Vertical Layout and Horizontal Layout. As their names suggest, they render their contents vertically and horizontally, respectively. Components are shown in the order they are added to either layout.
Vertical Layout
Vertical Layout places components top-to-bottom in a column. By default, it has 100% width and undefined height, meaning its width is constrained by its parent component and its height is determined by the components it contains.
new tab
VerticalLayout layout = new VerticalLayout();
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
layout.add(new LayoutItem("Item 4"));
Components in a Vertical Layout can be aligned both vertically and horizontally.
Vertical Alignment
You can position components at the top, middle, or bottom. Alternatively, you can position components by specifying how a layout’s excess space (if any) is distributed between them.
new tab
VerticalLayout layout = new VerticalLayout();
// Spacing can interfere with the alignment modes
layout.setSpacing(false);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
...
radioGroup.addValueChangeListener(e -> {
FlexComponent.JustifyContentMode mode = e.getValue().getMode();
layout.setJustifyContentMode(mode);
});
Value | Description |
---|---|
| Positions items at the top. |
| Vertically centers items. |
| Positions items at the bottom. |
| Available space is distributed evenly between items. No space is added before the first item, or after the last. |
| Available space is distributed evenly between items. The space before the first item, and after the last, is half of that between items. |
| Available space is distributed evenly between items. The space before the first item, between items and after the last item is the same. |
Horizontal Alignment
Components in a Vertical Layout can be left-aligned (default), centered, right-aligned or stretched horizontally.
new tab
VerticalLayout layout = new VerticalLayout();
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
...
radioGroup.addValueChangeListener(e -> {
FlexComponent.Alignment alignment = e.getValue().getAlignment();
layout.setAlignItems(alignment);
});
Value | Description |
---|---|
| Left-aligns (LTR) or right-aligns (RTL) items |
| Horizontally centers items |
| Right-aligns (LTR) or left-aligns (RTL) items |
| Stretches items with undefined width horizontally |
It is also possible to horizontally align individual components, overriding the alignment set by the parent layout.
new tab
LayoutItem item1 = new LayoutItem("Item 1");
VerticalLayout layout = new VerticalLayout();
layout.add(item1);
layout.add(new LayoutItem("Item 2", true));
layout.add(new LayoutItem("Item 3", true));
...
layoutRadioGroup.addValueChangeListener(e -> {
FlexComponent.Alignment alignment = e.getValue().getAlignment();
layout.setAlignItems(alignment);
});
...
itemRadioGroup.addValueChangeListener(e -> {
FlexComponent.Alignment alignment = e.getValue().getAlignment();
layout.setAlignSelf(alignment, item1);
});
Horizontal Layout
Horizontal Layout places components side-by-side in a row. By default, it has undefined width and height, meaning its size is determined by the components it contains.
new tab
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
layout.add(new LayoutItem("Item 4"));
Like Vertical Layout, Horizontal Layout enables both vertical and horizontal alignment of components.
Vertical Alignment
You can position components at the top, middle, or bottom. Items can alternatively be made to stretch vertically or positioned along the layout’s text baseline.
new tab
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
...
radioGroup.addValueChangeListener(e -> {
FlexComponent.Alignment alignment = e.getValue().getAlignment();
layout.setAlignItems(alignment);
});
Value | Description |
---|---|
| Stretches items with undefined height horizontally |
| Positions items at the top of the layout |
| Vertically centers items |
| Positions items at the bottom of the layout |
| Position items along the layout’s (text) baseline. |
It is also possible to vertically align individual components, overriding the alignment set by the parent layout.
new tab
LayoutItem item1 = new LayoutItem("Item 1");
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(item1);
layout.add(new LayoutItem("Item 2", true));
layout.add(new LayoutItem("Item 3", true));
...
layoutRadioGroup.addValueChangeListener(e -> {
FlexComponent.Alignment alignment = e.getValue().getAlignment();
layout.setAlignItems(alignment);
});
itemRadioGroup.addValueChangeListener(e -> {
FlexComponent.Alignment alignment = e.getValue().getAlignment();
layout.setAlignSelf(alignment, item1);
});
Horizontal Alignment
Components in a Horizontal Layout can be left-aligned, centered or right-aligned. Alternatively, you can position components by specifying how a layout’s excess space is distributed between them.
new tab
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
// Spacing can interfere with the alignment modes
layout.setSpacing(false);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
...
radioGroup.addValueChangeListener(e -> {
FlexComponent.JustifyContentMode mode = e.getValue().getMode();
layout.setJustifyContentMode(mode);
});
Value | Description |
---|---|
| Left-aligns (LTR) or right-aligns (RTL) items |
| Right-aligns (LTR) or left-aligns (RTL) items |
| Horizontally centers items |
| Available space is distributed evenly between items. No space is added before the first item, or after the last. |
| Available space is distributed evenly between items. The space before the first item, and after the last, is half of that between items. |
| Available space is distributed evenly between items. The space before the first item, between items and after the last item is the same. |
Spacing
Spacing is used to create space between components in the same layout. Spacing can help prevent misclicks and distinguish content areas.
new tab
VerticalLayout layout = new VerticalLayout();
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Spacing");
radioButtonGroup.setItems(ENABLED_OPTION, DISABLED_OPTION);
radioButtonGroup.setValue(ENABLED_OPTION);
radioButtonGroup.addValueChangeListener(
e -> layout.setSpacing(ENABLED_OPTION.equals(e.getValue())));
Warning
|
CAUTION: Combining spacing and alignment
Vertical Layout’s vertical alignment and Horizontal Layout’s horizontal alignment might not work properly when spacing is enabled.
If your layout requires both, consider disabling spacing and handling the alignment using CSS.
|
Five different spacing theme variants are available:
new tab
VerticalLayout layout = new VerticalLayout();
layout.setSpacing(false);
layout.getThemeList().add(SPACING_XL_THEME);
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Spacing variant");
radioButtonGroup
.setItems(SPACING_XS_THEME, SPACING_S_THEME, SPACING_THEME,
SPACING_L_THEME, SPACING_XL_THEME);
radioButtonGroup.setValue(SPACING_XL_THEME);
radioButtonGroup.addValueChangeListener(e -> {
layout.getThemeList().remove(e.getOldValue());
layout.getThemeList().add(e.getValue());
});
Theme variant | Usage recommendations |
---|---|
| Extra small space between items |
| Small space between items |
| Medium space between items |
| Large space between items |
| Extra large space between items |
Padding
Padding is the space between a layout’s outer border and its content. Padding can help distinguish a layout’s content from its surroundings. Padding is applied using the padding theme variant.
new tab
VerticalLayout layout = new VerticalLayout();
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Padding");
radioButtonGroup.setItems(ENABLED_OPTION, DISABLED_OPTION);
radioButtonGroup.setValue(ENABLED_OPTION);
radioButtonGroup.addValueChangeListener(
e -> layout.setPadding(ENABLED_OPTION.equals(e.getValue())));
Margin
Use margin to create space around a layout.
new tab
VerticalLayout layout = new VerticalLayout();
layout.setWidth("auto");
layout.setMargin(true);
layout.setAlignItems(FlexComponent.Alignment.STRETCH);
layout.add(new LayoutItem("Item 1"));
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Margin");
radioButtonGroup.setItems(ENABLED_OPTION, DISABLED_OPTION);
radioButtonGroup.setValue(ENABLED_OPTION);
radioButtonGroup.addValueChangeListener(
e -> layout.setMargin(ENABLED_OPTION.equals(e.getValue())));
Expanding Items
Components can be made to expand and take up any excess space a layout may have.
new tab
LayoutItem item1 = new LayoutItem("Item 1");
HorizontalLayout layout = new HorizontalLayout();
layout.setPadding(true);
layout.add(item1);
layout.add(new LayoutItem("Item 2"));
layout.add(new LayoutItem("Item 3"));
RadioButtonGroup<String> radioButtonGroup = new RadioButtonGroup<>();
radioButtonGroup.setLabel("Item sizing");
radioButtonGroup.setItems(DEFAULT_SIZE_OPTION, EXPANDED_SIZE_OPTION);
radioButtonGroup.setValue(DEFAULT_SIZE_OPTION);
radioButtonGroup.addValueChangeListener(e -> layout
.setFlexGrow(DEFAULT_SIZE_OPTION.equals(e.getValue()) ? 0 : 1,
item1));
When multiple components expand, they do so relative to each other. For example, two items with expand ratios of 2 and 1, will result in the first item taking up twice as much of the available space as the second item.
2482A61D-676F-4486-8450-B558D4FD49DD