Skip to main content

Textarea

A textarea is where I provide a long answer to a question in an online form.

The Textarea component is used when longer form content needs to be entered into an online form.

Details

The Textarea extends the USWDS Text input component. Further customization is applied using the CSS modifier(s) .cfa-textarea 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

Textarea

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.

<textarea class="usa-textarea cfa-textarea" id="textarea-6c19331260c97"></textarea>

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.

{
  "textarea": {
    "modifier": "cfa-textarea",
    "id": "textarea-6c19331260c97"
  }
}

Guidance

Form groups. Form questions with textarea components always use the form group component to wrap the textarea 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 Textarea has been verified for WCAG 2.1 AA contrast minimum success.
  • Unchecked
    The Textarea examples pass manual audits (WCAG 2.1 AA using axe or Lighthouse).
  • Unchecked
    The Textarea achieves WCAG 2.1 AA resizing success.
    Resizing text up to 200% without loss of content or functionality.
  • Unchecked
    The Textarea passes keyboard interaction tests.
    No keyboard test has been created.
  • Unchecked
    The Textarea passes screen reader interaction tests.

  • Unchecked
    Guidance on ensuring Textarea 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-textarea

  • Sass stylesheet: ./_cfa-textarea.scss
  • Thymeleaf template fragment: ./cfa-textarea.th.html
  • Embedded Ruby (ERB) partial template: ./_cfa-textarea.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-lightest: 'gray-warm-4', // Affects the background color of the textarea
  $cfa-color-base-lighter: 'gray-warm-10', // Affects the color of the textarea inset border
  $cfa-color-base-ink: 'gray-warm-90'      // Affects the text and border color of the textarea
);
// 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-textarea-height: 15
);
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.

<textarea th:fragment="textarea(textarea)" th:if="${textarea}" class="usa-textarea" th:classappend="${textarea.modifier}" th:attr="id=${textarea.id},name=${textarea.name},aria-invalid=${textarea.ariaInvalid},aria-describedby=${textarea.ariaDescribedby},required=${textarea.required},pattern=${textarea.pattern},placeholder=${textarea.placeholder},value=${textarea.value},inputmode=${textarea.inputmode},maxlength=${textarea.maxlength}"></textarea>
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-textarea/cfa-textarea.th :: textarea(${textarea})}" />
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.

<textarea class="usa-textarea<% if textarea['modifier'] %> <%= textarea['modifier'] %><% end %>"
  <% if textarea['id'] %> id="<%= textarea['id'] %>"<% end %>
  <% if textarea['name'] %> name="<%= textarea['name'] %>"<% end %>
  <% if textarea['aria-invalid'] %> aria-invalid="<%= textarea['ariaInvalid'] %>"<% end %>
  <% if textarea['aria-describedby'] %> aria-describedby="<%= textarea['ariaDescribedby'] %>"<% end %>
  <% if textarea['required'] %> required<% end %>
  <% if textarea['pattern'] %> pattern="<%= textarea['pattern'] %>"<% end %>
  <% if textarea['placeholder'] %> placeholder="<%= textarea['placeholder'] %>"<% end %>
  <% if textarea['value'] %> value="<%= textarea['value'] %>"<% end %>
  <% if textarea['inputmode'] %> inputmode="<%= textarea['inputmode'] %>"<% end %>
  <% if textarea['maxlength'] %> maxlength="<%= textarea['maxlength'] %>"<% end %>></textarea>
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-textarea/_cfa-textarea.html.erb'), 0, 0, '@textarea').result_with_hash({textarea: textarea}) %>

uswds.codeforamerica.org

Learn more about Code for America by visiting codeforamerica.org