Form Layout
Form Layout allows you to build responsive forms with multiple columns, and to position input labels above or to the side of, the input.
new tab
private responsiveSteps: FormLayoutResponsiveStep[] = [
// Use one column by default
{ minWidth: 0, columns: 1 },
// Use two columns, if layout's width exceeds 500px
{ minWidth: '500px', columns: 2 },
];
render() {
return html`
<vaadin-form-layout .responsiveSteps="${this.responsiveSteps}">
<vaadin-text-field label="First name"></vaadin-text-field>
<vaadin-text-field label="Last name"></vaadin-text-field>
<!-- Stretch the username field over 2 columns -->
<vaadin-text-field colspan="2" label="Username"></vaadin-text-field>
<vaadin-password-field label="Password"></vaadin-password-field>
<vaadin-password-field label="Confirm password"></vaadin-password-field>
</vaadin-form-layout>
`;
}
Columns
By default, Form Layout has two columns, meaning it displays two input fields per line. When the layout width is smaller, it adjusts to a single-column layout.
Custom Layout
You can define how many columns Form Layout should use based on the screen width.
Important | Use the draggable split handle to resize Form Layout’s available space and test its responsiveness. |
new tab
private responsiveSteps: FormLayoutResponsiveStep[] = [
// Use one column by default
{ minWidth: 0, columns: 1 },
// Use two columns, if the layout's width exceeds 320px
{ minWidth: '320px', columns: 2 },
// Use three columns, if the layout's width exceeds 500px
{ minWidth: '500px', columns: 3 },
];
render() {
return html`
<vaadin-split-layout>
<vaadin-form-layout .responsiveSteps="${this.responsiveSteps}">
<vaadin-text-field label="First name"></vaadin-text-field>
<vaadin-text-field label="Last name"></vaadin-text-field>
<vaadin-email-field label="Email"></vaadin-email-field>
</vaadin-form-layout>
<div></div>
</vaadin-split-layout>
`;
}
A single-column layout is preferable to a multi-column one. A multi-column layout is more prone to causing confusion and being misinterpreted by the user.
However, closely related fields can be placed in a line without issues. Examples might be first and last name, address fields such as postal code and city, and ranged input for dates, time, and currency.
Column Span
When using a multi-column layout, you can define a colspan
for each component.
colspan
determines how many columns a component extends or stretches across.
For example, if you have a Form Layout with three columns and a component’s colspan
is set to 3, it takes up the entire width of the Form Layout.
Label Position
Input fields' built-in labels are positioned above the input.
Form Layout supports side-positioned labels, provided they are wrapped in Form Items and the label position is set to aside
.
The only reason for wrapping labels in Form Items is to put the labels to the side of the input.
Top
Users generally complete forms that have top-positioned labels more quickly, because they provide a consistent scanning pattern (top-down, as opposed to a zigzag), while minimizing the distance between the label and input.
Top-positioned labels are also less prone to causing layout issues due to variable label lengths, as happens in the case of multilingual applications.
However, they do result in vertically longer forms, which is why sectioning is important.
Side
Side-positioned labels help reduce a form’s overall height. This is especially useful for longer forms and/or when vertical space is limited. They are also often used when there is a need to compare numeric data.
new tab
<vaadin-form-layout>
<!-- Wrap fields into form items, which
displays labels on the side by default -->
<vaadin-form-item>
<label slot="label">Revenue</label>
<vaadin-text-field>
<span slot="suffix">EUR</span>
</vaadin-text-field>
</vaadin-form-item>
<vaadin-form-item>
<label slot="label">Expenses</label>
<vaadin-text-field>
<span slot="suffix">EUR</span>
</vaadin-text-field>
</vaadin-form-item>
<vaadin-form-item>
<label slot="label">Invoices</label>
<vaadin-text-field>
<span slot="suffix">EUR</span>
</vaadin-text-field>
</vaadin-form-item>
</vaadin-form-layout>
Aim for similar-length labels to keep the distance between the labels and input fields consistent. Inconsistent spacing can cause the user to complete the form more slowly.
Forms that use this position require more horizontal space, which is not always ideal in narrow forms. It is recommended to configure Form Layout to use top-positioned labels when the form has a narrow width.
Native Input Fields
Form Item allows you to set a label for any type of component that you want to use in a Form Layout. It supports both Vaadin components and native HTML components.
new tab
<vaadin-form-layout>
<vaadin-form-item>
<label slot="label">Revenue</label>
<input type="text" />
</vaadin-form-item>
</vaadin-form-layout>
Best Practices
Button Placement
Use the following guidelines for Button placement in forms:
Buttons should be placed below the form they are associated with.
Buttons should be aligned to the left.
Primary action first, followed by other actions, in order of importance.
For more information, see the Button documentation.
7B8E4F98-540C-4622-A39F-907C95E9DFFD