Select
Examples
To render a select, apply the class chi-select
to a select
.
Base
<div class="chi-form__item">
<label class="chi-label" for="example-ba1">Label</label>
<select class="chi-select" id="example-ba1">
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
Disabled
<div class="chi-form__item">
<label class="chi-label" for="example-di1">Label</label>
<select class="chi-select" id="example-di1" disabled>
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
Required
Use the required
boolean attribute to indicate which select must be completed before submitting a form.
<div class="chi-form__item">
<label class="chi-label" for="example-re1">
Label
<abbr class="chi-label__required" aria-label="Required field">*</abbr>
</label>
<select class="chi-select" id="example-re1" required>
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
Optional
Use optional
to help emphasize that a select is not required and can be skipped.
<div class="chi-form__item">
<label class="chi-label" for="example-op1">
Label
<abbr class="chi-label__optional" aria-label="Optional field">(optional)</abbr>
</label>
<select class="chi-select" id="example-op1" required>
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
Help
Use chi-label
's attributes infoIcon
and infoIconMessage
to include a help icon that displays helpful information in a popover.
<div class="chi-form__item">
<chi-label for="example__help" info-icon info-icon-message="Helpful information goes here.">Label</chi-label>
<select class="chi-select" id="example__help">
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
<div class="chi-form__item">
<div class="chi-label__wrapper">
<label class="chi-label" for="example__help">Label</label>
<div class="chi-label__help">
<div class="chi-button -icon -flat -xs" id="example__help-button" data-target="#example__help-popover" aria-label="Help">
<div class="chi-button__content">
<i class="chi-icon icon-circle-info-outline" aria-hidden="true"></i>
</div>
</div>
<section class="chi-popover chi-popover--top" id="example__help-popover" aria-modal="true" role="dialog">
<div class="chi-popover__content">
<p class="chi-popover__text">Helpful information goes here.</p>
</div>
</section>
</div>
</div>
<select class="chi-select" id="example__help">
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
<script>chi.popover(document.getElementById('example__help-button'));</script>
Message
Add a message below a select to store descriptions, validation feedback, and other helpful information.
<div class="chi-form__item">
<label class="chi-label" for="example-me1">Label</label>
<select class="chi-select" id="example-me1">
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<div class="chi-label -status">Optional helper message</div>
</div>
Error
Use the -danger
state to provide feedback to users when a selection has not been made.
Once a selection has been made, the state must be removed. To meet accessibility requirements,
danger inputs must include an error message explaining the failure and/or how to correct it.
<div class="chi-form__item">
<label class="chi-label" for="example-er1">
Label
<abbr class="chi-label__required" aria-label="Required field">*</abbr>
</label>
<select class="chi-select -danger" id="example-er1" required>
<option value="">- Select -</option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<div class="chi-label -status -danger">
<i class="chi-icon icon-circle-warning" aria-hidden="true"></i>
Please select an option
</div>
</div>
Sizes
Selects support the following sizes: -xs
, -sm
, -md
,
-lg
, -xl
. The default size is -md
.
<!-- xs -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-xs">Label</label>
<select class="chi-select -xs" id="example-size-xs">
<option>-xs - Select -</option>
</select>
</div>
<!-- sm -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-sm">Label</label>
<select class="chi-select -sm" id="example-size-sm">
<option>-sm - Select -</option>
</select>
</div>
<!-- md -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-md">Label</label>
<select class="chi-select -md" id="example-size-md">
<option>-md - Select -</option>
</select>
</div>
<!-- lg -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-lg">Label</label>
<select class="chi-select -lg" id="example-size-lg">
<option>-lg - Select -</option>
</select>
</div>
<!-- xl -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-xl">Label</label>
<select class="chi-select -xl" id="example-size-xl">
<option>-xl - Select -</option>
</select>
</div>
Sizes
Selects support the following sizes: -xs
, -sm
, -md
,
-lg
. The default size is -md
.
<!-- xs -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-xs">Label</label>
<select class="chi-select -xs" id="example-size-xs">
<option>-xs - Select -</option>
</select>
</div>
<!-- sm -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-sm">Label</label>
<select class="chi-select -sm" id="example-size-sm">
<option>-sm - Select -</option>
</select>
</div>
<!-- md -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-md">Label</label>
<select class="chi-select -md" id="example-size-md">
<option>-md - Select -</option>
</select>
</div>
<!-- lg -->
<div class="chi-form__item">
<label class="chi-label" for="example-size-lg">Label</label>
<select class="chi-select -lg" id="example-size-lg">
<option>-lg - Select -</option>
</select>
</div>
Floating labels
Floating labels are a solution to keep the placeholder visible when no label is attached to the select.
Chi only supports floating labels on -lg
and -xl
selects.
<div id="floating-label-lg" class="chi-input__wrapper -floating-label">
<select class="chi-select -lg" id="floating-label-select-lg">
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<label for="floating-label-select-lg">Placeholder text</label>
</div>
<div id="floating-label-xl" class="chi-input__wrapper -floating-label">
<select class="chi-select -xl" id="floating-label-select-xl">
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<label for="floating-label-select-xl">Placeholder text</label>
</div>
<script>chi.floatingLabel(document.querySelectorAll('.-floating-label'));</script>
Floating labels
Floating labels are a solution to keep the placeholder visible when no label is attached to the select.
<div id="floating-label-md" class="chi-input__wrapper -floating-label">
<select class="chi-select -md" id="floating-label-portal-select-md">
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<label for="floating-label-portal-select-md">Placeholder text</label>
</div>
<div id="floating-label-lg" class="chi-input__wrapper -floating-label">
<select class="chi-select -lg" id="floating-label-portal-select-lg">
<option></option>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
<label for="floating-label-portal-select-lg">Placeholder text</label>
</div>
<script>chi.floatingLabel(document.querySelectorAll('.-floating-label'));</script>
Web Component
Properties
Accessibility
Keyboard Navigation
Key | Function |
---|---|
Tab | Moves focus to the next focusable element. |
Shift + Tab | Moves focus to the previous focusable element. |
Space ,Down Arrow | When focus is on the trigger, opens the dropdown. |
Up Arrow ,Down Arrow | Moves focus between the options in the dropdown. If focus is on the last/first option, moves focus to the first/last option respectively. |
Space ,Enter | Selects the option and closes the dropdown. |
Esc | Closes the dropdown and returns focus to the trigger. |
For comprehensive details on keyboard support, refer to our Keyboard Control Guide.
Visit WebAIM for keyboard techniques.
Find live examples in the A11y style guide.
Roles and attributes
The following list provides essential roles and attributes you should consider to ensure our components are fully accessible across various interface scenarios.
Attribute | Element | Usage |
---|---|---|
aria-label | div | Defines a string value that labels an interactive element. It is required props for select without text content. |
Resources
- Mozilla Resources for Developers: Information about the HTML select element.
- W3 Single-select Combobox: Single-select combobox widget that is functionally similar to an HTML
select
element. - MagentaA11y: How to test the select element.
Other recommendations
Explore additional accessibility tips in the general Accessibility Guide.
WCAG 2.2 Guidelines
- Non-text Content: All non-text content that is presented to the user has a text alternative that serves the equivalent purpose. (Level A)
- Info and Relationships: Information, structure, and relationships conveyed through presentation can be programmatically determined or are available in text. (Level A)
- Headings and Labels: Headings and labels describe topic or purpose. (Level AA)
- Labels or Instructions: Labels or instructions are provided when content requires user input. (Level A)
- Name, Role, Value: For all user interface components (including but not limited to: form elements, links and components generated by scripts), the name and role can be programmatically determined; states, properties, and values that can be set by the user can be programmatically set; and notification of changes to these items is available to user agents, including assistive technologies. (Level A)