Skip to main content

Select

A select lets me choose one out of many options from a pop-up list.

The Select component allows users to choose one option from a list in a modal dialog. The full list of options is only visible once a user interacts with the select.

Details

The Select extends the USWDS Select component. Further customization is applied using the CSS modifier(s) .cfa-select to add styles defined in a custom stylesheet.

Modifier. A modifier is a class name that applies a variant, type, or extended style customization to modify the component's visual appearance.

Examples

Select

The HTML for the demonstration above is rendered using context passed to the component's Thymeleaf template fragment. Learn how to include component templates for Thymeleaf and Ruby in the source and usage section.

<select class="usa-select cfa-select" id="select-83d765a2a1f75" name="select['83d765a2a1f75']" required="true">
  <option selected="true">Click to select an income type</option>
  <option disabled="true">---</option>
  <option value="wages">wages, salaries, tips</option>
  <option value="self-employment">self-employment income</option>
  <option value="unemployment">unemployment benefits</option>
  <option value="cash-assistance">Cash Assistance grant</option>
  <option value="child-support">child support (received)</option>
  <option value="disability-medicaid">disability-related Medicaid</option>
  <option value="supplemental-security-income">Supplemental Security Income (SSI)</option>
  <option value="social-security-dependent">Social Security Dependent Benefits</option>
  <option value="social-security-disability">Social Security Disability Benefits</option>
  <option value="social-security-survivor">Social Security Survivor’s Benefits</option>
  <option value="social-security-retirement">Social Security Retirement Benefits</option>
  <option value="state-disability">State Disability Benefits</option>
  <option value="veteran">Veteran’s Pension or Benefits</option>
  <option value="pension">Government or Private Pension</option>
  <option value="deferred-comp">Withdrawals from Deferred Compensation (IRA, Keogh, etc.)</option>
  <option value="workers-comp">Worker’s Compensation</option>
  <option value="alimony">alimony (received)</option>
  <option value="boarder">boarder or lodger</option>
  <option value="gifts">gifts/contributions (received)</option>
  <option value="rental">rental income</option>
  <option value="investment">investment income (interest, dividends, and profit from selling stocks)</option>
</select>

Context is the information necessary to configure and render a component template to HTML. It may include plain text strings, HTML, class names, IDs or other HTML attribute values. The context here is defined as JSON but the root attributes are translated to different variable syntaxes for Thymeleaf and Ruby templates. Learn how to pass these variables to each component template in the source and usage section.

{
  "selectEl": {
    "modifier": "cfa-select",
    "id": "select-83d765a2a1f75",
    "name": "select['83d765a2a1f75']",
    "required": "true",
    "default": {
      "label": "Click to select an income type",
      "selected": "true"
    },
    "options": [
      {
        "label": "---",
        "disabled": "true"
      },
      {
        "value": "wages",
        "label": "wages, salaries, tips"
      },
      {
        "value": "self-employment",
        "label": "self-employment income"
      },
      {
        "value": "unemployment",
        "label": "unemployment benefits"
      },
      {
        "value": "cash-assistance",
        "label": "Cash Assistance grant"
      },
      {
        "value": "child-support",
        "label": "child support (received)"
      },
      {
        "value": "disability-medicaid",
        "label": "disability-related Medicaid"
      },
      {
        "value": "supplemental-security-income",
        "label": "Supplemental Security Income (SSI)"
      },
      {
        "value": "social-security-dependent",
        "label": "Social Security Dependent Benefits"
      },
      {
        "value": "social-security-disability",
        "label": "Social Security Disability Benefits"
      },
      {
        "value": "social-security-survivor",
        "label": "Social Security Survivor’s Benefits"
      },
      {
        "value": "social-security-retirement",
        "label": "Social Security Retirement Benefits"
      },
      {
        "value": "state-disability",
        "label": "State Disability Benefits"
      },
      {
        "value": "veteran",
        "label": "Veteran’s Pension or Benefits"
      },
      {
        "value": "pension",
        "label": "Government or Private Pension"
      },
      {
        "value": "deferred-comp",
        "label": "Withdrawals from Deferred Compensation (IRA, Keogh, etc.)"
      },
      {
        "value": "workers-comp",
        "label": "Worker’s Compensation"
      },
      {
        "value": "alimony",
        "label": "alimony (received)"
      },
      {
        "value": "boarder",
        "label": "boarder or lodger"
      },
      {
        "value": "gifts",
        "label": "gifts/contributions (received)"
      },
      {
        "value": "rental",
        "label": "rental income"
      },
      {
        "value": "investment",
        "label": "investment income (interest, dividends, and profit from selling stocks)"
      }
    ]
  }
}

Guidance

Form groups. Form questions with select components always use the form group component to wrap the select with a visible label element.

Refer to additional guidance on the USWDS documentation site.

USWDS documentation site

Additional references

Accessibility

  • Unchecked
    Customization to the visual appearance of the Select has been verified for WCAG 2.1 AA contrast minimum success.
  • Unchecked
    The Select examples pass manual audits (WCAG 2.1 AA using axe or Lighthouse).
  • Unchecked
    The Select achieves WCAG 2.1 AA resizing success.
    Resizing text up to 200% without loss of content or functionality.
  • Unchecked
    The Select passes keyboard interaction tests.
    No keyboard test has been created.
  • Unchecked
    The Select passes screen reader interaction tests.

  • Unchecked
    Guidance on ensuring Select accessibility has been provided in this documentation.

Refer to additional accessibility guidance on the USWDS documentation site.

Checklist Key

  • Passes
    Passes
  • Unchecked
    Unchecked

Design library component

Source and usage

Package: @codeforamerica/uswds/packages/cfa-select

  • Sass stylesheet: ./_cfa-select.scss
  • Thymeleaf template fragment: ./cfa-select.th.html
  • Embedded Ruby (ERB) partial template: ./_cfa-select.html.erb
Packages are collections of functionality that make up a component. Typically, they include stylesheets, templates, and scripts. Learn more about packages on the USWDS documentation site.
Sass theme settings

Below is a demonstration of customizing the component theme settings. Refer to the theme and package-level settings documentation.

// Theme-level settings
@use 'cfa-uswds-theme' with (
  // Global theme settings that affect the component, changing these will affect other components
  $cfa-color-base-ink: 'gray-warm-90' // Affects the text and border color of the select
);
// Package-level settings
@use 'cfa-core' with (
  $cfa-form-elements-border-width: 2px,
  $cfa-form-elements-padding-y: 2,
  $cfa-form-elements-padding-x: 2.5,
  $cfa-select-height: 8
);
USWDS theme settings

Refer to the usage documentation for additional USWDS theme settings.

@use "uswds-core" with ( /* additional USWDS theme settings */ );
Thymeleaf template fragment

This is the pre-rendered template fragment from the package. It is the same template used to render the demonstrations above. You may copy and paste from this example or use the template using the th:block th:replace tag. See the example below.

<select th:fragment="select(selectEl)" class="usa-select" th:classappend="${selectEl.modifier}" th:attr="id=${selectEl.id},name=${selectEl.name},aria-invalid=${selectEl.ariaInvalid},aria-describedby=${selectEl.ariaDescribedby},required=${selectEl.required}">
  <option th:if="${selectEl.default}" th:attr="value=${selectEl.default.value},selected=${selectEl.default.selected}" th:text="${selectEl.default.label}">Label</option>
  <option th:each="option: ${selectEl.options}" th:attr="value=${option.value},selected=${option.selected},disabled=${option.disabled}" th:text="${option.label}">Label</option>
</select>
Template fragment inclusion

Below is an example of how to use the fragment from the package directory using the th:block th:replace inclusion tag. Replace the fragment parameters using your variables or context.

<th:block th:replace="~{packages/cfa-select/cfa-select.th :: select(${selectEl})}" />
ERB template partial

This is the pre-rendered partial template from the package. You may copy and paste from this example or use the template directly from the path.

<select class="usa-select<% if selectEl['modifier'] %> <%= selectEl['modifier'] %><% end %>"
  <% if selectEl['id'] %> id="<%= selectEl['id'] %>"<% end %>
  <% if selectEl['name'] %> name="<%= selectEl['name'] %>"<% end %>
  <% if selectEl['ariaInvalid'] %> aria-invalid="<%= selectEl['ariaInvalid'] %>"<% end %>
  <% if selectEl['ariaDescribedby'] %> aria-describedby="<%= selectEl['ariaDescribedby'] %>"<% end %>
  <% if selectEl['required'] %> required="<%= selectEl['required'] %>"<% end %>>
  <% if selectEl['default'] %><option
    <% if selectEl['default']['value'] %> value="<%= selectEl['default']['value'] %>"<% end %>
    <% if selectEl['default']['selected'] %> selected="<%= selectEl['default']['selected'] %>"<% end %>>
    <%= selectEl['default']['label'] %>
  </option>
  <% end %>
  <% selectEl['options'].each do |option| %>
  <option
    <% if option['value'] %> value="<%= option['value'] %>"<% end %>
    <% if option['selected'] %> selected="<%= option['selected'] %>"<% end %>
    <% if option['disabled'] %> disabled="<%= option['disabled'] %>"<% end %>>
    <%= option['label'] %></option>
  <% end %>
</select>
Partial render

Below is an example of how to include the partial in a view from the package directory using the Ruby ERB class. Replace the argument values using your variables or context. In a Rails environment, the render method can be used instead with the same hash values.

<%= ERB.new(File.read('packages/cfa-select/_cfa-select.html.erb'), 0, 0, '@select').result_with_hash({selectEl: selectEl}) %>

uswds.codeforamerica.org

Learn more about Code for America by visiting codeforamerica.org