---
title: Examples
slug: wip/pWwR-examples
createdAt: 2023-07-20T14:45:03.180Z
---

# Examples

## Basic Usage

A label can be provided by nesting it inside the checkbox.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/wvyGOOr/f259091b7579d828d8415e28d9fe92ff?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;158px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## States

The Checkbox supports a wide range of different states.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/wvyGObr/104880288a9f184ab6b4cbd0047800fa?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;287px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Prefixes

The Checkbox accepts any content to be displayed as a decorative prefix such as images, texts, or other visual elements that help users to distinguish a group of options more clearly.

:::hint{type="info"}
**☝️ Watch out**

The prefix is a decorative element only and should not contain any meaningful information necessary to understand or distinguish an option. Hence the content of a prefix isn't accessible to assistive technologies.
:::

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/abqpgay/75042ea30249b29ec8e485ae8d056a07?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;367px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Help Text

The help text serves the purpose of guiding a user's decision by providing additional information.

Even though established as a common design pattern, checkbox labels should not contain any interactive elements like links as it is considered harmful to accessibility. Instead links to other resources should be provided using the help text.

A help text can either be passed as an attribute for simple string-based content or via a slot for rich formatted content.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/KKBVzZE/cac95f6173f46def805be213fb3d06d1?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;407px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Required and Optional

Adding a required or optional label helps users to be guided while filling out a form. Usually, only Checkboxes that are an exception to the majority of other form fields in a form are marked as required or optional. For example, a Checkbox is marked as "Required" when the majority of the fields in the same form are optional and vise versa.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/OJwWBRY/a05d3984a07d4fb0189261a33edb9a56?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;295px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Truncation

Checkboxes can be forced into a single line without compromising accessibility. The full content of the label is still associated with the Checkbox and is displayed after a view seconds on hover gesture.

:::hint{type="info"}
**💡 Did you know**

Truncating a checkbox even works when used in conjunction with a Prefix.
:::

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/ZErWZEw/5497651e3054b351f615f7a427a7c2a5?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;159px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Hit Area

When used as part of a group inside a container, the usability of Checkboxes can be improved by increasing their interactive hit area beyond the visible label covering the entire row. That allows users to make quick and easy selections across all options within a group without worrying about individual hit areas defined by varying label lengths.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/NWBVbjV/45d28c62de13dfb92ffc80852aaba6dc?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;384px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Validation

A Checkbox's validity can either be determined automatically when used inside a form or can be controlled from outside based on custom rules and application business logic.

### Inside a form

Checkboxes seamlessly integrate into a form's validation workflows and validate when a form is submitted. When no custom validation message is provided, the Checkbox falls back to the browser's native validation message.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/VwQmZPW/fdf77ef7cfb8b838c6b040805e789747?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;259px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

### Inside an application

Checkboxes can be individually flagged as invalid based on custom rules or other business logic of an application. When flagged as invalid, a Checkbox expects a descriptive validation message to be set alongside.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/poZgbMP/7db5a679a1141099dc0a0a8b1ca6ccab?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;322px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Listening to Events

The Checkbox supports the most commonly used events.

:::hint{type="info"}
**💡 Did you know?**

Some native events like `change` are blocked by the Shadow DOM boundary of the web component standard. Hence some of them are proxied through custom events.

Find out more: [Form Input Events & API](https://cosmos.redbull.design/wip/form-input-events-and-api)
:::

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/qBxZwbO/406233072446dae4c86afac2b7da225f?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;436px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

## Validation API

Similar to the native one, Cosmos' Checkbox exposes public methods to interact with the validity state of the component. The methods are: `inputCheckValidity`, `inputReportValidity` and `inputSetCustomValidity`. The methods are asynchronous and must be waited for.

:::Iframe{iframeHeight="0" code="<iframe title=&#x22;Embeded content from codepen.io&#x22; class=&#x22;no-border max-full-width full-height flex-grow&#x22; src=&#x22;https://codepen.io/redbulldesign/embed/WNMXRKE/7c5e4cda1d9fc6a1b08e56ed5de0e397?default-tab=html%2Cresult&#x26;amp;theme-id=39623&#x26;amp;editable=true&#x22; height=&#x22;387px&#x22; loading=&#x22;lazy&#x22;></iframe>"}

:::

