Vertical Layout
Vertical Layout places components top-to-bottom in a column. By default, it has a width of 100% and an undefined height. Its width is constrained by its parent component (i.e., it “fills” the available space). Whereas, its height is determined by the components it contains (i.e., it “hugs” its content).
See Horizontal Layout for information on placing components side-by-side.
new tab
VerticalLayout layout = new VerticalLayout();
Div item1 = new Div("Item 1");
item1.setClassName("example-item");
Div item2 = new Div("Item 2");
item2.setClassName("example-item");
Div item3 = new Div("Item 3");
item3.setClassName("example-item");
layout.add(item1, item2, item3);
Components in a Vertical Layout can be aligned vertically, as you’d expect. However and perhaps surprisingly, they can also be aligned horizontally in a Vertical Layout.
Vertical Alignment
You can position components at the top, middle, or bottom. You can also position them by specifying how the excess space in a layout is distributed among them.
new tab
VerticalLayout layout = new VerticalLayout();
layout.setJustifyContentMode(FlexComponent.JustifyContentMode.CENTER);
Div item1 = new Div("Item 1");
item1.setClassName("example-item");
Div item2 = new Div("Item 2");
item2.setClassName("example-item");
Div item3 = new Div("Item 3");
item3.setClassName("example-item");
layout.add(item1, item2, item3);
Value | Description |
---|---|
| Positions items at the top. |
| Centers items, vertically. |
| Positions items at the bottom. |
| Available space is distributed equally among items. However, no space is added before the first item, or after the last. |
| Available space is distributed equally among items. However, the space before the first item and after the last is half of that between items. |
| Available space is distributed equally among items. The space before the first item and after the last item is the same as between others. |
Horizontal Alignment
Components in a Vertical Layout are left-aligned by default, but can be centered, right-aligned or stretched horizontally.
new tab
VerticalLayout layout = new VerticalLayout();
layout.setAlignItems(FlexComponent.Alignment.CENTER);
Div item1 = new Div("Item 1");
item1.setClassName("example-item");
Div item2 = new Div("Item 2");
item2.setClassName("example-item");
Div item3 = new Div("Item 3");
item3.setClassName("example-item");
layout.add(item1, item2, item3);
Value | Description |
---|---|
| Left-aligns items for left-to-right language text (e.g., English). For right-to-left languages (e.g., Arabic, Hebrew), it right-aligns items. |
| Centers items, horizontally. |
| Right-aligns for left-to-right language text. For right-to-left languages, it left-aligns items. |
| Stretches horizontally items with undefined width. |
It’s also possible to align horizontally individual components by overriding the general alignment setting of the layout.
new tab
VerticalLayout layout = new VerticalLayout();
layout.setAlignItems(FlexComponent.Alignment.START);
Div item1 = new Div("Item 1");
item1.setClassName("example-item");
layout.add(item1);
layout.setAlignSelf(Alignment.END, item1);
Div item2 = new Div("Item 2");
item2.setClassName("example-item");
layout.add(item2);
layout.setAlignSelf(Alignment.CENTER, item2);
Div item3 = new Div("Item 3");
item3.setClassName("example-item");
layout.add(item3);
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 has spacing enabled by default, use setSpacing to disable it
VerticalLayout layoutWithoutSpacing = new VerticalLayout();
layoutWithoutSpacing.setSpacing(false);
Five different spacing theme variants are available:
new tab
@state()
private themeVariant = 'spacing-xl';
protected override render() {
return html`
<vaadin-vertical-layout
theme="${this.themeVariant} padding"
class="height-4xl"
style="align-items: stretch"
>
<div class="example-item">Item 1</div>
<div class="example-item">Item 2</div>
<div class="example-item">Item 3</div>
</vaadin-vertical-layout>
<vaadin-radio-group
label="Spacing variant"
.value="${this.themeVariant}"
@value-changed="${(event: RadioGroupValueChangedEvent) => {
this.themeVariant = event.detail.value;
}}"
>
<vaadin-radio-button value="spacing-xs" label="spacing-xs"></vaadin-radio-button>
<vaadin-radio-button value="spacing-s" label="spacing-s"></vaadin-radio-button>
<vaadin-radio-button value="spacing" label="spacing"></vaadin-radio-button>
<vaadin-radio-button value="spacing-l" label="spacing-l"></vaadin-radio-button>
<vaadin-radio-button value="spacing-xl" label="spacing-xl"></vaadin-radio-button>
</vaadin-radio-group>
`;
}
Theme Variant | Usage Recommendation |
---|---|
| Extra-small space between items. |
| Small space between items. |
| Medium space between items. |
| Large space between items. |
| Extra-large space between items. |
VerticalLayout layout = new VerticalLayout();
layout.setSpacing(false);
layout.getThemeList().add("spacing-xs");
Padding
Padding is the space allocated between the content in a layout and the outer border. This should not be confused with Margin, which is explained in the next section.
Padding can help distinguish the content in a layout from its surrounding. Padding is applied using the padding theme variant.
new tab
// VerticalLayout has padding enabled by default, use setPadding to disable it
VerticalLayout layoutWithoutPadding = new VerticalLayout();
layoutWithoutPadding.setPadding(false);
Margin
Margin is the space around a layout. This is different from Padding, which is explained in the previous section.
new tab
VerticalLayout layoutWithMargin = new VerticalLayout();
layoutWithMargin.setMargin(true);