Docs

Vertical Layout

Vertical Layout places components top-to-bottom in a column.

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.

Source code
VerticalLayoutBasic.java
vertical-layout-basic.tsx
vertical-layout-basic.ts

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.

Source code
VerticalLayoutVerticalAlignment.java
vertical-layout-vertical-alignment.tsx
vertical-layout-vertical-alignment.ts
Value Description

START (default)

Positions items at the top.

CENTER

Centers items, vertically.

END

Positions items at the bottom.

BETWEEN

Available space is distributed equally among items. However, no space is added before the first item, or after the last.

AROUND

Available space is distributed equally among items. However, the space before the first item and after the last is half of that between items.

EVENLY

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.

Source code
VerticalLayoutHorizontalAlignment.java
vertical-layout-horizontal-alignment.tsx
vertical-layout-horizontal-alignment.ts
Value Description

START (default)

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.

CENTER

Centers items, horizontally.

END

Right-aligns for left-to-right language text. For right-to-left languages, it left-aligns items.

STRETCH

Stretches horizontally items with undefined width.

It’s also possible to align horizontally individual components by overriding the general alignment setting of the layout.

Source code
VerticalLayoutIndividualAlignment.java
vertical-layout-individual-alignment.tsx
vertical-layout-individual-alignment.ts

Spacing

Spacing is used to create gaps between components in the same layout. Spacing can help prevent misclicks and distinguish content areas.

Source code
VerticalLayoutSpacing.java
vertical-layout-spacing.tsx
vertical-layout-spacing.ts

Spacing can be customized with a specific value or a CSS custom property:

Source code
Java
VerticalLayout layout = new VerticalLayout();

layout.setSpacing(12, Unit.PIXELS);

layout.setSpacing("var(--vaadin-gap-l)");
Java
tsx
tsx
HTML
HTML

For available CSS custom properties that can be used as spacing values, see gap properties from the base styles and space properties from the Lumo theme.

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 style variant.

Source code
VerticalLayoutPadding.java
vertical-layout-padding.tsx
vertical-layout-padding.ts

Margin

Margin is the space around a layout. This is different from Padding, which is explained in the previous section.

Source code
VerticalLayoutMargin.java
vertical-layout-margin.tsx
vertical-layout-margin.ts

Wrapping

By default, components in a layout either shrink or overflow when there isn’t enough vertical space. Enable wrapping to allow components to flow onto a new column when space runs out, preventing overflow.

Source code
VerticalLayoutWrapping.java
vertical-layout-wrapping.tsx
vertical-layout-wrapping.ts

Troubleshooting

Component is Smaller than its Specified Size

In some situations, a component with a specific, fixed size is rendered smaller than that size (and its size may vary depending on the size of the UI).

This is usually caused by the component being placed in the same Horizontal or Vertical Layout as another component with 100% (or “full”) size along the same axis.

The reason for this behavior is a combination of two aspects of Horizontal Layout and Vertical Layout:

  • 100% width or height actually means the full width or height of the layout, rather than whatever space is available after any fixed-size items.

  • By default, children of these layouts are allowed to shrink below their specified size. While this allows full-size items to shrink below 100% to make room for other items, it also makes fixed-size items shrink a bit.

There are three main ways to solve this issue:

Prevent the fixed-size element from shrinking

By setting the flex-shrink value of the fixed size component to 0, it is prevented from shrinking below that size.

Source code
Java
VerticalLayout layout = new VerticalLayout(fixedSizeComponent, fullSizeComponent);
fixedSizeComponent.setHeight("200px");
fullSizeComponent.setHeightFull();

layout.setFlexShrink(fixedSizeComponent, 0);
// or
fixedSizeComponent.getStyle().setFlexShrink("0");
Java
tsx
tsx
HTML
HTML

Use Flex-Grow Instead of 100% Size

Instead of setting a 100% (or “full”) size, you can make a component take all available space by setting its flex-grow value to 1.

Source code
Java
VerticalLayout layout = new VerticalLayout(fixedSizeComponent, fullSizeComponent);
fixedSizeComponent.setHeight("200px");

layout.setFlexGrow(fullSizeComponent, 1);
// or
fullSizeComponent.getStyle().setFlexGrow("1");
Java
tsx
tsx
HTML
HTML

Enable Layout Improvements to Prevent Shrinking (Flow only, experimental)

Instead of adjusting individual components, you can enable the layoutComponentImprovements feature flag. With this flag, the Flow APIs setWidthFull, setHeightFull, and setSizeFull are rewired to automatically apply flex: 1 to the component. This prevents fixed-size components from shrinking and makes the full-size component take up the remaining space in the layout.

Component Overflows its Specified Size

This is most commonly noticed on scroll containers like Scroller and TabSheet, or elements that have been scroll-enabled through CSS, but it can occur in other situations as well. The problem often causes extra undesired scrollbars to appear.

This is caused by the default minimum size of a layout item to be equal to the size of its contents.

There are three main ways to solve this issue:

Set an Appropriate Minimum Size

Set the minimum size to 0 or any other specific size.

Source code
Java
overFlowingComponent.setMinHeight("0");
Java
tsx
tsx
HTML
HTML

Prevent Overflow

You can prevent the component from overflowing by setting the CSS overflow property to hidden. Be aware that this will also clip outlines and box-shadows, such as those used for focus rings.

Source code
Java
overFlowingComponent.getStyle().setOverflow(Overflow.HIDDEN);
Java
tsx
tsx
HTML
HTML

Enable Layout Improvements to Allow Shrinking (Flow only, experimental)

The same layoutComponentImprovements feature flag described above also addresses overflow. When enabled, the Flow APIs setWidthFull, setHeightFull, and setSizeFull are rewired to set the minimum size of nested Horizontal and Vertical Layouts to 0, allowing them to shrink below the size of their contents.

A Nested Layout Takes All the Space

When you nest a Vertical Layout or Horizontal Layout inside another layout, its default size — not only the sizes you set explicitly — affects how space is shared. A Vertical Layout defaults to width: 100%, so when nested as a child of a Horizontal Layout it claims the full width of the row and starves its siblings. (A nested Horizontal Layout defaults to hugging its content instead.)

A typical symptom is a text column that should grow and truncate with an ellipsis, but instead collapses to zero width: a sibling layout’s implicit 100% width has consumed the row. Adding the usual min-width: 0 ellipsis recipe to the text column isn’t enough on its own, because the sibling’s default width is the real cause.

To fix this, size both children explicitly: let the column that should only take the space it needs hug its content, and give the growing column flex: 1 1 0 together with width: 0, min-width: 0, and overflow: hidden so it can shrink below its content size and truncate.

Source code
Java
HorizontalLayout row = new HorizontalLayout(textColumn, metaColumn);

// The growing column truncates with an ellipsis
textColumn.getStyle().setFlexGrow("1");   // grow to fill free space
textColumn.getStyle().setFlexShrink("1"); // allow shrinking
textColumn.getStyle().setFlexBasis("0");  // start from zero width
textColumn.setWidth("0");
textColumn.setMinWidth("0");
textColumn.getStyle().setOverflow(Overflow.HIDDEN);

// The meta column hugs its content instead of filling the row
metaColumn.setWidth(null);
Java
tsx
tsx
HTML
HTML

73cc0e40-d39a-11ed-afa1-0242ac120002

Updated