Overview
A button is a <button> or an <a> with the nds-btn class, a variant class and a label. Add an icon before or after the label, or show the icon only. The variant sets the emphasis, from primary for the main action to transparent for the least. Size and shape are more classes, and a state is an attribute, such as disabled. A button needs no JavaScript.
A button can also open a Dropmenu or copy text with Copy.
Pick another component when:
- the click only switches the view on the page: Content Switcher
- the action must wait before the next press, such as resend code: Cooldown Button
Markup
Variants
A row on .nds-btn changes every button in a group. A row on .nds-btn:first-child changes one button. The builder uses the first one, but on a page it goes on the button you mean, such as the chosen one in a group. Two rows with the same Group and Option are one choice: write both. :not(.nds-progress), :not(.nds-ellipsis) and :not(:has(> .nds-avatar)) mean the option is not for a progress, a More or a user button. Direction goes on a group or on a More button: make the change that fits the markup.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Standard (default) | — | — | One button |
| Structure | Group | canon #btn-group |
— | A row of related buttons joined into one strip. The two ends keep the rounded corners |
| Structure | Progress (hint: A ring around an icon button) | canon #btn-progress |
— | A ring around an icon-only button, such as the close button of a toast that closes itself. The ring is aria-hidden, so the label names the action. A disabled progress button keeps its colors, so disabled can stop a second press while the ring runs |
| Structure | More (hint: Three dots, for a menu of more actions) | canon #btn-more |
— | A square button with three dots, for a menu of more actions. The label is hidden and names the button for screen readers |
| Structure | User | canon #btn-user |
— | A user’s photo and name, such as the account button in a header. The avatar takes the icon size. Add Menu for a button that opens the account menu |
| Variant | Primary (default) | .nds-primary |
.nds-btn |
The main action in a view. Use one per view |
| Variant | Neutral | .nds-neutral |
.nds-btn |
A strong action next to the primary one. In dark mode it takes the on-color look |
| Variant | Secondary | .nds-secondary |
.nds-btn |
A supporting action, with a light fill |
| Variant | Secondary outline | .nds-secondary-outline |
.nds-btn |
A supporting action, with a border and no fill |
| Variant | Subtle | .nds-subtle |
.nds-btn |
A low-emphasis action, such as cancel or dismiss. The fill shows on hover |
| Variant | Transparent | .nds-transparent |
.nds-btn |
The lowest emphasis. No fill in any state. Only the text color changes on hover |
| Size | LG (default) | — | — | 40px high. It needs no class |
| Size | MD | .nds-md |
.nds-btn |
32px high |
| Size | SM | .nds-sm |
.nds-btn |
24px high |
| Content | Label (default) | — | — | Text only |
| Content | Icon and label | canon #btn-icon |
.nds-btn:not(.nds-progress):not(.nds-ellipsis):not(:has(> .nds-avatar)) (start) |
An icon before the label |
| Content | Label and icon | canon #btn-icon |
.nds-btn:not(.nds-progress):not(.nds-ellipsis):not(:has(> .nds-avatar)) |
An icon after the label. .nds-trail-icon does the same with the icon first in the markup |
| Content | Icon above label | canon #btn-icon |
.nds-btn:not(.nds-progress):not(.nds-ellipsis):not(:has(> .nds-avatar)) (start) |
The icon stacks above the label |
| Content | Icon above label | .nds-col |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
The same. The padding is equal on all sides, the label can wrap to two lines, and the button fills the height of its row |
| Content | Icon only | canon #btn-icon |
.nds-btn:not(.nds-progress):not(.nds-ellipsis):not(:has(> .nds-avatar)) (start) |
A square button with only the icon |
| Content | Icon only | .nds-icon-only |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
The same. Keep the label: it is hidden on screen and names the button for screen readers. On a user button, it shows only the photo |
| Built-in icon | None (default) | — | — | No drawn icon |
| Built-in icon | Menu (hint: A chevron after the label, for a button that opens a menu) | .nds-menu-btn |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
A chevron after the label. It turns when the button has data-state~="open" or aria-expanded="true", which the Dropmenu sets |
| Built-in icon | Next | .nds-next |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
An arrow that points forward in the reading direction |
| Built-in icon | Previous | .nds-prev |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
An arrow that points back in the reading direction. The arrow comes before the label |
| Built-in icon | Up | .nds-up |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
An arrow that points up |
| Built-in icon | Down | .nds-down |
.nds-btn:not(.nds-progress):not(.nds-ellipsis) |
An arrow that points down |
| State | None (default) | — | — | Ready to press |
| State | Selected | [data-state~="selected"] |
.nds-btn:first-child:not(.nds-progress) |
The chosen button, such as a pressed toggle or the chosen button in a group |
| State | Selected | [aria-pressed="true"] |
.nds-btn:first-child:not(.nds-progress) |
Set it with data-state~="selected". It tells screen readers the button is on |
| State | Disabled | [disabled] |
.nds-btn:first-child:not(.nds-progress) |
The action is not available. On an <a>, use aria-disabled="true" |
| State | Loading | .nds-loading |
.nds-btn:first-child:not(.nds-progress) |
A spinner replaces the content while a request runs. The button keeps its width. In JavaScript, call NDS.state.add(btn, 'loading') |
| State | Loading | [disabled] |
.nds-btn:first-child:not(.nds-progress) |
The same. disabled stops a second press, and a loading button keeps its colors |
| Ring | Countdown (default) | --progress-duration: 4000ms |
.nds-progress |
The ring fills once over this time |
| Ring | Fixed value (hint: The ring shows a set percentage, not a countdown) | .nds-progress-static |
.nds-progress |
The ring shows a fixed part, such as upload progress |
| Ring | Fixed value (hint: The ring shows a set percentage, not a countdown) | --progress-value: 25 |
.nds-progress |
The same. The value is a percentage, from 0 to 100 |
| Direction | Horizontal (default) | — | — | The buttons sit in a row, or the three dots of a More button |
| Direction | Vertical | .nds-vertical |
.nds-btn-group |
The buttons stack in a column. The top and bottom keep the rounded corners |
| Direction | Vertical | .nds-vertical |
.nds-btn.nds-ellipsis |
On a More button, the three dots stand in a column, for a menu at the end of a row or a card |
| Seamless | Seamless (hint: No divider between the buttons) | .nds-seamless |
.nds-btn-group |
No divider between the buttons. For subtle icon buttons that read as one set of tools |
| Destructive | Destructive (hint: Error colors for delete and remove) | .nds-destructive |
.nds-btn:not(.nds-neutral) |
Error colors, for delete, remove or any action that cannot be undone. It works with every variant except neutral |
| On color | On color (hint: For a deep primary or dark background) | .nds-oncolor |
.nds-btn |
For a button on a deep primary or dark background. Inside an on-color area (data-theme="dark oncolor"), the button already takes this look: skip the class |
| Indicator | Indicator (hint: A bar at the bottom edge on hover and when selected) | .nds-indicator |
.nds-btn |
A bar at the bottom edge. It shows on hover, focus and the selected state, as in a tab or a menu bar |
| Circle | Circle (hint: Round ends) | .nds-circle |
.nds-btn:not(.nds-btn-group > .nds-btn) |
Round ends. An icon-only button becomes a circle. No effect in a group, which owns its corners |
| Full width | Full width | .nds-full |
.nds-btn:not(.nds-btn-group > .nds-btn) |
Fills the width of its container, with the content centered |
| Full width | Full width | .nds-full |
.nds-btn-group |
On a group, the group fills the width and its buttons share it equally. Put it on the group, not on its buttons |
| Primary text | Primary text (hint: Brand-color text, no fill) | .nds-color |
.nds-transparent:not(.nds-destructive):not(.nds-oncolor) |
The text takes the brand primary color, and goes back to the default color on hover |
| Badge | Badge (hint: A count over the icon corner) | canon #btn-badge |
i.nds-icon |
A count over the corner of the icon. It goes inside the <i> icon element, never beside it, in data-count: CSS draws it, so it never joins the button’s text. It takes the button’s size and stays inside the button’s top edge. The icon is aria-hidden, so screen readers skip the count: put it in the label too. NDS.setBadge(button, count) does both |
Built-in Features
-
Pure CSS
No JavaScript needed. Every variant, size and state comes from classes and attributes.
-
Six Variants
Primary, neutral, secondary, secondary outline, subtle and transparent, each with its hover, pressed, selected, focused and disabled look.
-
Three Sizes
24px, 32px and 40px high. The label, the icon and the indicator scale with the size.
-
Layout Options
An icon before or after the label, icon only, label only, stacked, circle and full width, and a loading state.
-
Accessibility
A focus ring for keyboard users, thicker borders in high contrast mode, no motion when the user asks for reduced motion, and plain print styles.
-
Composable
Combines with destructive, on color, loading, progress, status, badge, indicator and groups.
Best Practices
- Use a
<button>for an action on the page, and an<a class="nds-btn">when the click opens another page. The classes are the same. - Use a button group for a short row of related actions.
- Use one primary button per view, for the main action. Use neutral, secondary or secondary outline for the actions beside it, and subtle or transparent for cancel and dismiss.
- Use destructive for delete and other actions that cannot be undone. Ask the user to confirm in a Modal first.
- Add
nds-oncolorto a button on a deep primary or dark background. Without it, the button can be hard to see. - Write
type="submit"on the button that sends a form. Writetype="button"on every other button in a form, or it sends the form too. - Keep the
nds-labelspan on an icon-only button. It is hidden on screen and names the button for screen readers. A button with no label element needs anaria-label. - Add
nds-loadinganddisabledwhile a request runs, so a second press does not send it again. Aform.nds-formdoes this for its submit button: see Text Fields, Submit Validation. - Keep labels short: a verb and a noun, such as “Save draft”. A long label does not wrap, except in a stacked button.
- Use a status for feedback on the button itself, and remove it after about two seconds. For a message the user must read, use an Alert.
API
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-trail-icon |
.nds-btn |
Puts the icon after the label, with the icon first in the markup |
nds-lead-icon |
.nds-btn |
Puts the icon before the label. Use it to undo a reversed order, such as the one nds-prev sets |
nds-label-only |
.nds-btn, or any parent |
Hides every icon inside, so the same markup shows labels only |
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-state |
.nds-btn |
selected, open, active or current gives the selected look. open and opening also turn the menu chevron. loading is the same as nds-loading, and disabled the same as the disabled attribute. hover, pressed and focused show that look without a pointer or a keyboard, for a design review |
data-status |
.nds-btn |
Feedback for a moment after an action: success, error, info or warning. It sets the colors in every state, swaps each nds-icon icon for the status icon, and colors the border of a secondary outline button. Copy, Share and the accessibility panel set success for about two seconds. Set it for your own actions with NDS.status, as in the example below. Do not write it in the markup |
aria-expanded |
.nds-menu-btn |
true turns the chevron, the same as data-state~="open" |
aria-disabled |
.nds-btn |
true gives the disabled look and blocks clicks. Use it on an <a>, which has no disabled attribute |
CSS Custom Properties
Set these on the button itself. There, a size property wins over the size classes. A --btn-size, --btn-gap or --btn-indicator-size set on a parent does not reach a button, because buttons sit inside other components. The variant classes set the colors on the button, so a color set on a parent does not reach it either.
| Property | Default | Controls |
|---|---|---|
--btn-size |
40px |
Height, and the width of an icon-only button. The label and icon sizes follow it. nds-md and nds-sm set 32px and 24px |
--btn-height |
var(--btn-size) |
Height alone, without the label and icon sizes |
--btn-height-min |
fit-content |
Minimum height |
--btn-width |
fit-content |
Width |
--btn-padding |
(size - 8px) / 2 |
Padding on the start and end sides |
--btn-gap |
var(--spacing-md) |
Space between the icon and the label. nds-md sets var(--spacing-sm), nds-sm sets var(--spacing-xs) |
--btn-radius |
var(--radius-sm) |
Corner radius |
--btn-FS, --btn-LH |
from --btn-size |
Label font size and line height |
--btn-icon-size |
size / 2 + 4px, size / 2 + 2px on a progress button |
Icon size |
--btn-bg, --btn-bg-hover, --btn-bg-active, --btn-bg-selected, --btn-bg-disabled |
set by the variant | Background in each state |
--btn-color, --btn-color-hover, --btn-color-disabled |
set by the variant | Text and icon color |
--btn-border, --btn-border-hover, --btn-border-active, --btn-border-selected, --btn-border-disabled |
set by the variant | Border color in each state |
--btn-indicator-size |
3px, 2px on SM |
Height of the indicator bar |
--btn-loading-color, --btn-loading-track |
set by the variant | Spinner colors |
--btn-group-radius |
var(--radius-md) |
Corner radius of a group, the same at every button size |
--progress-duration |
4000ms |
Time the ring takes to fill |
--progress-value |
0 |
Fill of a nds-progress-static ring, from 0 to 100 |
--progress-size |
size - 8px |
Size of the ring |
--progress-circumference |
62.83 |
Length of the ring for a fixed value. It matches stroke-dasharray on the circle: change it only with the circle’s radius |
Tokens
Set a token at :root, or on a wrapper to reach every button inside it. Give a token with a Dark mode value a dark override too: see Tokens.
Source: the button group in _sass/tokens/_components.scss.
| Token | Preview | Value |
|---|---|---|
--button-background-neutral-default | var(--colors-neutral-950) | |
--button-background-neutral-hovered | var(--colors-neutral-800)Dark mode var(--colors-neutral-900) | |
--button-background-neutral-pressed | var(--colors-neutral-600) | |
--button-background-neutral-selected | var(--colors-neutral-700) | |
--button-background-primary-default | var(--colors-primary-600) | |
--button-background-primary-hovered | var(--colors-primary-700) | |
--button-background-primary-pressed | var(--colors-primary-900) | |
--button-background-primary-selected | var(--colors-primary-800) | |
--button-background-secondary-default | var(--colors-neutral-100)Dark mode var(--colors-neutral-700) | |
--button-background-secondary-hovered | var(--colors-neutral-100)Dark mode var(--colors-neutral-700) | |
--button-background-secondary-pressed | var(--colors-neutral-200)Dark mode var(--colors-neutral-600) | |
--button-background-secondary-selected | var(--colors-neutral-200)Dark mode var(--colors-neutral-600) | |
--button-background-danger-primary-default | var(--colors-red-600) | |
--button-background-danger-primary-hovered | var(--colors-red-700) | |
--button-background-danger-primary-pressed | var(--colors-red-900) | |
--button-background-danger-primary-selected | var(--colors-red-800) | |
--button-label-danger-oncolor-default | var(--colors-red-200) | |
--button-label-danger-oncolor-hovered | var(--colors-red-300) | |
--button-label-danger-oncolor-pressed | var(--colors-red-400) | |
--button-label-danger-oncolor-selected | var(--colors-red-300) | |
--button-background-danger-secondary-default | var(--colors-red-50)Dark mode var(--colors-red-alpha-10) | |
--button-background-danger-secondary-hovered | var(--colors-red-100)Dark mode var(--colors-red-alpha-20) | |
--button-background-danger-secondary-pressed | var(--colors-red-200)Dark mode var(--colors-red-950) | |
--button-background-danger-secondary-selected | var(--colors-red-100)Dark mode var(--colors-red-alpha-20) | |
--button-label-danger-secondary-default | var(--colors-red-700)Dark mode var(--colors-red-300) | |
--button-label-danger-transparent-default | var(--colors-red-600)Dark mode var(--colors-red-300) | |
--button-label-danger-transparent-hovered | var(--colors-red-700)Dark mode var(--colors-red-400) | |
--button-label-danger-transparent-pressed | var(--colors-red-900)Dark mode var(--colors-red-500) | |
--button-label-danger-transparent-selected | var(--colors-red-800)Dark mode var(--colors-red-400) | |
--button-background-danger-secondary-oncolor-default | var(--colors-red-50) | |
--button-background-danger-secondary-oncolor-hovered | var(--colors-red-100) | |
--button-background-danger-secondary-oncolor-pressed | var(--colors-red-200) | |
--button-background-danger-secondary-oncolor-selected | var(--colors-red-100) | |
--button-label-danger-secondary-oncolor-default | var(--colors-red-600) | |
--button-label-danger-secondary-oncolor-hovered | var(--colors-red-700) | |
--button-label-danger-secondary-oncolor-pressed | var(--colors-red-900) | |
--button-label-danger-secondary-oncolor-selected | var(--colors-red-800) | |
--button-border-danger-oncolor | var(--colors-red-200) | |
--button-background-oncolor-default | var(--colors-base-white) | |
--button-background-oncolor-hovered | var(--colors-alpha-white-80) | |
--button-background-oncolor-pressed | var(--colors-alpha-white-60) | |
--button-background-oncolor-selected | var(--colors-alpha-white-70) | |
--button-background-oncolor-disabled | var(--colors-alpha-white-20) | |
--button-background-transparent-default | var(--colors-alpha-white-0) | |
--button-background-transparent-hovered | var(--colors-alpha-white-20) | |
--button-background-transparent-pressed | var(--colors-alpha-white-40) | |
--button-background-transparent-selected | var(--colors-alpha-white-30) | |
--button-label-transparent-oncolor-default | var(--text-oncolor-primary) | |
--button-label-transparent-oncolor-hovered | var(--colors-primary-400) | |
--button-label-transparent-oncolor-pressed | var(--colors-primary-300) | |
--button-label-transparent-oncolor-selected | var(--colors-primary-400) | |
--button-indicator-default | var(--colors-neutral-400) | |
--button-indicator-pressed | var(--colors-neutral-800)Dark mode var(--background-surface-oncolor) | |
--button-indicator-selected | var(--colors-neutral-800)Dark mode var(--background-surface-oncolor) | |
--button-indicator-primary | var(--colors-primary-400) | |
--button-indicator-mark | var(--background-primary)Dark mode var(--colors-primary-500) | |
--button-indicator-oncolor-default | var(--colors-alpha-white-40) | |
--button-indicator-oncolor-pressed | var(--colors-base-white) | |
--button-indicator-oncolor-selected | var(--colors-base-white) | |
--button-status-success-bg | var(--background-success-light)Dark mode var(--colors-green-alpha-10) | |
--button-status-success-text | var(--text-success)Dark mode var(--colors-green-400) | |
--button-status-error-bg | var(--background-error-light)Dark mode var(--colors-red-alpha-10) | |
--button-status-error-text | var(--text-error)Dark mode var(--colors-red-400) | |
--button-status-info-bg | var(--background-info-light)Dark mode var(--colors-blue-alpha-10) | |
--button-status-info-text | var(--text-info)Dark mode var(--colors-blue-400) | |
--button-status-warning-bg | var(--background-warning-light)Dark mode var(--colors-yellow-alpha-10) | |
--button-status-warning-text | var(--text-warning)Dark mode var(--colors-yellow-400) | |
--button-border-danger | var(--colors-red-200)Dark mode var(--colors-red-300) | |
--button-border-error | var(--border-error-light)Dark mode var(--colors-red-400) | |
--button-border-success | var(--border-success-light)Dark mode var(--colors-green-400) | |
--button-border-warning | var(--border-warning-light)Dark mode var(--colors-yellow-400) | |
--button-border-info | var(--border-info-light)Dark mode var(--colors-blue-400) |
It also reads these shared tokens. A change to one also changes other components.
Source: the var() reads in _sass/components/_buttons.scss.
| Token | Preview | Value |
|---|---|---|
--background-primaryRead by --btn-loading-color | var(--colors-primary-600) | |
--background-errorRead by background-color | var(--colors-red-600) | |
--background-disabledRead by --btn-bg-disabled | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) | |
--text-defaultRead by --btn-color, --btn-loading-color, --btn-color-hover | var(--colors-base-black)Dark mode var(--colors-neutral-200) | |
--text-oncolor-primaryRead by --btn-color, --btn-loading-color, color | var(--colors-base-white) | |
--text-default-disabledRead by --btn-color-disabled | var(--colors-neutral-400) | |
--text-default-oncolor-disabledRead by --btn-color-disabled | var(--colors-alpha-white-40) | |
--border-oncolorRead by --btn-border, --btn-border-hover, --btn-border-active, --btn-border-selected | var(--colors-alpha-white-40) | |
--border-neutral-primaryRead by --btn-border, --btn-border-active, --btn-border-selected | var(--colors-neutral-300)Dark mode var(--colors-neutral-600) | |
--border-neutral-secondaryRead by --btn-border-hover, --btn-border-disabled | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) | |
--divider-colorRead by border-inline-start-color, border-block-start-color | var(--colors-alpha-black-10)Dark mode var(--colors-alpha-white-10) |
JavaScript
A button has no script of its own. Set its state with NDS.state and its status with NDS.status.