Are you upgrading to v4 (healthcare v8, medicare v6)?
Take a look at our v4 buttons migration guide to get started.
Examples
The following tables give an overview of the different available button styles in the selected design system. Each button has three controllable traits that can be used in various combinations. They are:
- Variation: which affects the border and background styles
- Color: defined as either main or alternate
- Context: specified as either on-light or on-dark
See the examples in the following sections to learn how they can be applied.
Variation | Main (Default) | Alternate |
---|---|---|
Outline (Default) | ||
Solid | ||
Ghost |
Variation | Main (Default) | Alternate |
---|---|---|
Outline (Default) | ||
Solid | ||
Ghost |
Main buttons
Solid button
Outline button
Ghost button
Alternate buttons
Each button variation (outline, solid, ghost) has an alternate option. Alternate buttons can be styled differently than the main buttons.
Alternate solid button
Alternate outline button
Alternate ghost button
On dark background
Button sizes
Buttons can exist in two sizes other than default: "big" or "small".
Big button
Small button
Adding icons
- Add an inline SVG icon and it will become the same color as the button text. For the crispest icon rendering, ensure the icon has a square
viewBox
with values that are multiples of8
(ie.24x24
). - Use the margin utility class to add spacing between the icon and button text.
Code
React
The Button
component accepts its text as children (AKA inner HTML), which
means you can also pass in HTML or custom components. This gives you a lot of
flexibility and supports a variety of advanced use cases. The most common use
case would be passing in an SVG icon along with the text.
In addition to the supported props listed, you can also pass in additional
props, which will be passed to the rendered root component. For example,
you could pass in a target
prop to pass to the rendered anchor element.
Name | Type | Default | Description |
---|---|---|---|
children required | ReactNode | Label text or HTML | |
className | string | Additional classes to be added to the root button element. | |
disabled | boolean | ||
href | string | When provided, the root component will render as an <a> element
rather than button . | |
inputRef | ButtonRef | Access a reference to the button or a element | |
isAlternate | boolean | false | Applies the alternate color to a Button. By default, Button
uses the main color. |
onClick | (...args: any[]) => any | Returns the SyntheticEvent .
Not called when the Button is disabled. | |
onDark | boolean | false | Defines a color palette best used when Button is placed on a dark background-color. By default, Button uses a light color palette. |
size | ButtonSize | ||
type | "button" | "submit" | "reset" | 'button' as const | Button type attribute |
variation | ButtonVariation | A string corresponding to Button variation classes. | |
analytics | boolean | Analytics events tracking is enabled by default. Set this value to false to
disable tracking for this component instance. | |
analyticsLabelOverride | string | An override for the dynamic content sent to analytics services. By default this content comes from the heading. In cases where this component’s heading may contain sensitive information, use this prop to override what is sent to analytics. | |
analyticsEventTypeOverride | string | If you need the event_type to be overridden for your use case, you can provide
an alternate string here. Suggested values can be found in the EventType enum. | |
onAnalyticsEvent | (event: AnalyticsEvent) => void | Optional callback that will intercept analytics events for this component.
If none is specified, the design system will use the default analytics
function, which can be overwritten globally with setDefaultAnalyticsFunction . | |
analyticsParentHeading | string | If needed for analytics, pass heading text of parent component of button. | |
analyticsParentType | string | If needed for analytics, pass type of parent component of button. | |
This component passes any additional props to its underlying <button> or <a> element as attributes. See the corresponding MDN documentation for <button> and <a> for a list of valid attributes. |
Styles
The following CSS variables can be overridden to customize Button components:
Analytics
This component has analytics tracking available. Please see our developer documentation about using analytics in the design system.
Guidance
Buttons are promises to the user; they must deliver the promise they offer by doing what the button says it will do.
When to use
- Use buttons for the most important actions you want users to take on your site, such as "Download," "Sign up," or "Apply."
When to consider alternatives
- Less popular or less important actions may be visually styled as links.
- Buttons are for performing actions, not making choices. If you need your users to make a choice, use something else like radio buttons. Alternatively, if one choice is much less important then try styling it as a link instead.
Usage
- Avoid using too many buttons on a page. Aim to use only one button per page.
- Avoid similar styles elsewhere on the page that could be confused for buttons.
- Use buttons for the primary action and links for secondary actions.
Label text
- Use sentence case for button labels.
- Button labels should be as short as possible with “trigger words” that your users will recognize to clearly explain what will happen when the button is clicked (for example, “Save and continue,” “Download” or “Sign up”).
- Make the first word of the button’s label a verb. For example, instead of “Complaint Filing”, label the button “File a complaint.”
- If a button has an icon, it should still have accompanying text describing the action.
Destructive buttons
- Confirm the user meant to trigger a destructive action before following through with the action.
- Provide a method for a user to undo a destructive action.
Avoid using disabled buttons
Disabling a "button" to act as a guide to the level of form completion is a common anti-pattern (e.g., you aren't able to proceed in the next step until the form contains enough data). Why it's a problem:
- Buttons tend to have call-to-action text that matches what users want to do, so people try to interact with them.
- They have terrible color contrast.
- They don’t provide feedback and tell the user why they're disabled. They communicate that something is off, but very often that is not enough information. As a result, users are left wondering what’s actually missing, and consequently are locked out entirely.
- Assistive technologies like screen readers and switches are usually not even able to navigate to disabled buttons.
For these reasons, disabled buttons can be frustrating for all users but especially those with cognitive disabilities and those who use assistive technologies.
Accessibility
- Buttons should display a visible focus state when users tab to them.
- Create a button with a
<button>
or<a>
element to retain the native click functionality. Avoid using<div>
or<img>
tags to create buttons. Screen readers don't automatically know either is a usable button. - When styling links to look like buttons, remember that screen readers handle links slightly differently than they do buttons. Pressing the
Space
key triggers a button, but pressing theEnter
key triggers a link. - Dimmed or unavailable buttons using the
<a>
tag must have thearia-disabled
attribute set to true and itshref
attribute removed.- Unlike the
<button>
,disabled
is not a valid attribute for<a>
. To describe the disabled state to screen reader users, use thearia-disabled
attribute instead. - The
href
attribute is not required on<a>
, and when it's absent it doesn't create a hyperlink.
- Unlike the
- Dimmed or unavailable buttons using the
<button>
tag should have thedisabled
attribute applied. This removes native click and keypress events from the button. It also prevents automated scanners from logging a low contrast error. Finally, it announces the button as "dimmed" or "disabled" to screen readers, offering users additional context.
Content
- Describe what will happen, not the current state.
- Buttons always start with an action word describing the main thing users will do once selected.
- Pair it with a noun to help it be clearer. Example: Get tips.
- If in a form, it can be just the action word if it's navigating between steps. Example: Cancel, Back, Next.
- Use the least amount of words possible (no more than 6 with 2-4 being ideal).
Moving through a process or sequential steps after starting
For forward movement to the next page or step.
Learn more
- Beyond Blue Links: Making Clickable Elements Recognizable
- 7 Basic Best Practices for Buttons
- The Grammar of Interactivity
- GOV.UK navigation buttons discussion
Button alignment
ARIA
Component maturity
For more information about how we tested and validated our work for each checklist item, read our component maturity documentation.