> Markdown version of [Checkbox](https://vaadin.com/docs/v23/components/checkbox). Section index: [llms.txt](https://vaadin.com/docs/v23/components/llms.txt)

# Checkbox

Checkbox is an input field representing a binary choice. Checkbox Group is a group of related binary choices.

**TypeScript** — `checkbox-basic.ts`

```html
<vaadin-checkbox label="I accept the terms and conditions"></vaadin-checkbox>
```

**Java** — `CheckboxBasic.java`

```java
Checkbox checkbox = new Checkbox();
checkbox.setLabel("I accept the terms and conditions");

add(checkbox);
```

**TypeScript** — `checkbox-group-basic.ts`

```html
<vaadin-checkbox-group
  label="Export data"
  .value="${this.value}"
  @value-changed="${(event: CheckboxGroupValueChangedEvent) => {
    this.value = event.detail.value;
  }}"
  theme="vertical"
>
  <vaadin-checkbox value="0" label="Order ID"></vaadin-checkbox>
  <vaadin-checkbox value="1" label="Product name"></vaadin-checkbox>
  <vaadin-checkbox value="2" label="Customer"></vaadin-checkbox>
  <vaadin-checkbox value="3" label="Status"></vaadin-checkbox>
</vaadin-checkbox-group>
```

**Java** — `CheckboxGroupBasic.java`

```java
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Export data");
checkboxGroup.setItems("Order ID", "Product name", "Customer",
        "Status");
checkboxGroup.select("Order ID", "Customer");
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
add(checkboxGroup);
```

Use Checkbox Group to group related items. Individual checkboxes should be used for options that aren’t related to each other in any way.

## <a id="common-input-field-features"></a>Common Input Field Features

Checkbox Group includes all shared input field features.

- [See shared input field features](https://vaadin.com/docs/v23/components/input-fields.md)

## <a id="states"></a>States

### <a id="disabled"></a>Disabled

Disable a field to mark it as currently unavailable. Disabled state is used for fields that aren’t editable and don’t need to be readable. Disabled elements can’t be focused and may be inaccessible to assistive technologies like screen readers.

Disabling can be preferable to hiding an element to prevent changes in layout when the element’s visibility changes, and to make users aware of its existence even when currently unavailable.

**TypeScript** — `checkbox-disabled.ts`

```html
<vaadin-checkbox-group label="Departments" theme="vertical" disabled>
  <vaadin-checkbox value="engineering" label="Engineering"></vaadin-checkbox>
  <vaadin-checkbox value="human-resources" label="Human Resources"></vaadin-checkbox>
  <vaadin-checkbox value="marketing" label="Marketing"></vaadin-checkbox>
  <vaadin-checkbox value="operations" label="Operations"></vaadin-checkbox>
  <vaadin-checkbox value="sales" label="Sales"></vaadin-checkbox>
</vaadin-checkbox-group>
```

**Java** — `CheckboxDisabled.java`

```java
CheckboxGroup<String> disabledCheckGroup = new CheckboxGroup<>();
disabledCheckGroup.setLabel("Departments");
disabledCheckGroup.setItems("Engineering", "Human Resources",
        "Marketing", "Operations", "Sales");
disabledCheckGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
disabledCheckGroup.setEnabled(false);
add(disabledCheckGroup);
```

> **Note: Read-only state**
>
> Checkbox doesn’t support read-only state.

### <a id="indeterminate"></a>Indeterminate

The indeterminate state can be used for a parent checkbox to show that there is a mix of checked and unchecked child items in a list, and to change the state of all child items at once.

**TypeScript** — `checkbox-indeterminate.ts`

```html
<vaadin-checkbox
  label="Notify users"
  .checked="${selectedIds.length === items.length}"
  .indeterminate="${selectedIds.length > 0 && selectedIds.length < items.length}"
  @change="${(e: Event) => {
    this.selectedIds = (e.target as HTMLInputElement).checked
      ? this.items.map((person) => String(person.id))
      : [];
  }}"
></vaadin-checkbox>

<vaadin-checkbox-group
  label="Users to notify"
  theme="vertical"
  .value="${this.selectedIds}"
  @value-changed="${(event: CheckboxGroupValueChangedEvent) => {
    this.selectedIds = event.detail.value;
  }}"
>
  ${items.map(
    (person) => html`
      <vaadin-checkbox
        .value="${String(person.id)}"
        label="${person.firstName} ${person.lastName}"
      ></vaadin-checkbox>
    `
  )}
</vaadin-checkbox-group>
```

**Java** — `CheckboxIndeterminate.java`

```java
Checkbox checkbox = new Checkbox("Notify users");

CheckboxGroup<Person> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Users to notify");
checkboxGroup.setItemLabelGenerator(
        person -> person.getFirstName() + " " + person.getLastName());
checkboxGroup.setItems(items);
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
checkboxGroup.addValueChangeListener(event -> {
    if (event.getValue().size() == items.size()) {
        checkbox.setValue(true);
        checkbox.setIndeterminate(false);
    } else if (event.getValue().size() == 0) {
        checkbox.setValue(false);
        checkbox.setIndeterminate(false);
    } else {
        checkbox.setIndeterminate(true);
    }
});
checkbox.addValueChangeListener(event -> {
    if (checkbox.getValue()) {
        checkboxGroup.setValue(new HashSet<>(items));
    } else {
        checkboxGroup.deselectAll();
    }
});
checkboxGroup.select(items.get(0), items.get(2));
add(checkbox, checkboxGroup);
```

## <a id="orientation"></a>Orientation

The component’s default orientation is horizontal but vertical orientation is recommended whenever possible as it’s easier for the user to scan a vertical list of options:

**TypeScript** — `checkbox-vertical.ts`

```html
<vaadin-checkbox-group label="Working days" theme="vertical">
  <vaadin-checkbox value="mon" label="Monday"></vaadin-checkbox>
  <vaadin-checkbox value="tue" label="Tuesday"></vaadin-checkbox>
  <vaadin-checkbox value="wed" label="Wednesday"></vaadin-checkbox>
  <vaadin-checkbox value="thu" label="Thursday"></vaadin-checkbox>
  <vaadin-checkbox value="fri" label="Friday"></vaadin-checkbox>
  <vaadin-checkbox value="sat" label="Saturday"></vaadin-checkbox>
  <vaadin-checkbox value="sun" label="Sunday"></vaadin-checkbox>
</vaadin-checkbox-group>
```

**Java** — `CheckboxVertical.java`

```java
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Working days");
checkboxGroup.setItems("Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday");
checkboxGroup.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);
add(checkboxGroup);
```

In cases where vertical space needs to be conserved, horizontal orientation can be used, but no more than 3 options are recommended:

**TypeScript** — `checkbox-horizontal.ts`

```html
<vaadin-checkbox-group label="Permissions">
  <vaadin-checkbox value="read" label="Read"></vaadin-checkbox>
  <vaadin-checkbox value="edit" label="Edit"></vaadin-checkbox>
  <vaadin-checkbox value="delete" label="Delete"></vaadin-checkbox>
</vaadin-checkbox-group>
```

**Java** — `CheckboxHorizontal.java`

```java
CheckboxGroup<String> checkboxGroup = new CheckboxGroup<>();
checkboxGroup.setLabel("Permissions");
checkboxGroup.setItems("Read", "Edit", "Delete");
add(checkboxGroup);
```

## <a id="best-practices"></a>Best Practices

### <a id="labeling"></a>Labeling

Aim for short and descriptive labels using positive wording. **Avoid negations**.

`checkbox-labeling.ts`

```typescript
@customElement('checkbox-labeling')
export class Example extends LitElement {
  protected override createRenderRoot() {
    const root = super.createRenderRoot();
    // Apply custom theme (only supported if your app uses one)
    applyTheme(root);
    return root;
  }

  protected override render() {
    return html`<vaadin-checkbox label="Yes, I agree"></vaadin-checkbox>`;
  }
}
```

It’s important to provide labels for Checkbox Groups to clearly distinguish adjacent groups.

**TypeScript** — `checkbox-adjacent-groups.ts`

```html
<vaadin-vertical-layout>
  <vaadin-checkbox-group label="Manufacturer" theme="vertical">
    <vaadin-checkbox value="0" label="Akuchi"></vaadin-checkbox>
    <vaadin-checkbox value="1" label="Broek"></vaadin-checkbox>
    <vaadin-checkbox value="2" label="Wulf"></vaadin-checkbox>
  </vaadin-checkbox-group>

  <vaadin-checkbox-group label="Status" theme="vertical">
    <vaadin-checkbox value="0" label="In progress"></vaadin-checkbox>
    <vaadin-checkbox value="1" label="Done"></vaadin-checkbox>
    <vaadin-checkbox value="2" label="Cancelled"></vaadin-checkbox>
  </vaadin-checkbox-group>
</vaadin-vertical-layout>
```

**Java** — `CheckboxAdjacentGroups.java`

```java
CheckboxGroup<String> manufacturer = new CheckboxGroup<>();
manufacturer.setLabel("Manufacturer");
manufacturer.setItems("Akuchi", "Broek", "Wulf");
manufacturer.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);

CheckboxGroup<String> status = new CheckboxGroup<>();
status.setLabel("Status");
status.setItems("In progress", "Done", "Cancelled");
status.addThemeVariants(CheckboxGroupVariant.LUMO_VERTICAL);

add(manufacturer, status);
```

## <a id="related-components"></a>Related Components

| Component                                                                    | Usage recommendations                                                                                                                                          |
| ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [Select](https://vaadin.com/docs/v23/components/select.md)                   | A field for selecting an item from a list of options which are presented in an overlay. Recommended when there is insufficient space for a Radio Button Group. |
| [Combo Box](https://vaadin.com/docs/v23/components/combo-box.md)             | A filterable, lazy loading alternative to Select, recommended for ten or more items.                                                                           |
| [List Box](https://vaadin.com/docs/v23/components/list-box.md)               | Scrollable list of options. Supports single and multi-select.                                                                                                  |
| [Radio Button Group](https://vaadin.com/docs/v23/components/radio-button.md) | Corresponding component for mutually exclusive options, or “single-select”.                                                                                    |

`F86F2BE5-1BDA-4E79-BD9E-6CE12742450B`
