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.

Open in a
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.

Open in a
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.JustifyContentMode mode = e.getValue().getMode();
    layout.setJustifyContentMode(mode);
});
ValueDescription

START (default)

Positions items at the top.

CENTER

Vertically centers items.

END

Positions items at the bottom.

BETWEEN

Available space is distributed evenly between items. No space is added before the first item, or after the last.

AROUND

Available space is distributed evenly between items. The space before the first item, and after the last, is half of that between items.

EVENLY

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.

Open in a
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);
});
ValueDescription

START (default)

Left-aligns (LTR) or right-aligns (RTL) items

CENTER

Horizontally centers items

END

Right-aligns (LTR) or left-aligns (RTL) items

STRETCH

Stretches items with undefined width horizontally

It is also possible to horizontally align individual components, overriding the alignment set by the parent layout.

Open in a
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.

Open in a
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.

Open in a
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);
});
ValueDescription

STRETCH (default)

Stretches items with undefined height horizontally

START

Positions items at the top of the layout

CENTER

Vertically centers items

END

Positions items at the bottom of the layout

BASELINE

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.

Open in a
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.

Open in a
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.JustifyContentMode mode = e.getValue().getMode();
    layout.setJustifyContentMode(mode);
});
ValueDescription

START (default)

Left-aligns (LTR) or right-aligns (RTL) items

END

Right-aligns (LTR) or left-aligns (RTL) items

CENTER

Horizontally centers items

BETWEEN

Available space is distributed evenly between items. No space is added before the first item, or after the last.

AROUND

Available space is distributed evenly between items. The space before the first item, and after the last, is half of that between items.

EVENLY

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.

Open in a
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:

Open in a
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 variantUsage recommendations

spacing-xs

Extra small space between items

spacing-s

Small space between items

spacing

Medium space between items

spacing-l

Large space between items

spacing-xl

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.

Open in a
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.

Open in a
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.

Open in a
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.