Button
lightning-button
A clickable element used to perform an action.
For Use In
Lightning Experience, Experience Builder Sites, Salesforce Mobile App, Lightning Out (Beta), Standalone Lightning App, Mobile Offline
A lightning-button component represents a button element that executes an
action. Use lightning-button where users need to:
- Submit or reset a form
- Begin a new task
- Trigger a new UI element to appear on the page
- Specify a new or next step in a process
Use the type attribute to specify button, submit, or reset. The default type is button and doesn't need to be specified.
The submit and reset types create buttons for submitting and resetting form data.
Use these button types in lightning-record-edit-form and the HTML form element. The reset type button only deletes the values in the form fields without interacting with the database. For information about resetting the form fields to their initial values, see
lightning-record-edit-form .
For the default button, clicking the button triggers the click event. Use an onclick handler to perform the button's action.
You can create a button with a label only, or add the icon-name attribute
for a button with a label and icon. To create an icon-only
button, use the lightning-button-icon component instead.
Here's an example that creates a button with the brand variant, and displays
label text on the button. The title attribute provides tooltip text for the button.
Here's another example that creates a button with the brand variant, with a label
and icon. The icon is positioned to the left
of the label by default, so the example uses icon-position to display it on the right.
You can retrieve the button that's clicked by using event.target. For
example, to retrieve the label on the button, use
event.target.label.
lightning-button implements the
buttons blueprint in the Salesforce Lightning Design System (SLDS).
Use the icon-name attribute to add a utility icon to the button.
The SLDS utility icon category provides nearly 200 utility
icons that can be used in lightning-button along with label text. Although
SLDS provides several categories of icons, only the utility category can be used in lightning-button.
Visit https://lightningdesignsystem.com/icons/#utility to view the utility icons.
Use the variant attribute with one of these values to apply styling and animation.
baseis a button without a border, which gives it the look of a plain text link.neutralis the default variant, a plain uncolored button.brandis a blue button, used to draw attention to the primary action on a page.brand-outlineis similar tobrandbut the color is used for the label and border only, not the button color.destructiveis a red button used to warn users that its action has a negative effect.destructive-textis similar todestructivebut only the label and border are red.inverseuses the background color and light text, useful for dark backgrounds.successis a green button used to indicate a successful action.
Buttons are animated by default. For the base variant, an underline is drawn when you mouse over the button. For the other variants, a ripple of contrasting color displays when you click the button.
When a button is disabled, the animation is disabled as well.
If your browser or operating system is set to reduce animation, the button animations don't display.
To prevent animation for a button, use the disable-animation attribute.
We recommend setting this attribute only when you place lightning-button near other
buttons that aren't animated, for consistency.
To apply additional styling, use the SLDS utility classes with the class attribute.
This example adds a margin to the left of the second button using an SLDS class.
You can also use the stretch attribute to expand the button to the full available width. In this example, the button stretches horizontally to fit the full <div> container.
Component styling hooks use the --slds-c-* prefix and change styling for specific elements or properties of a component. Component-specific styling isn’t recommended, because it’s unsupported for SLDS 2, but existing customizations still work with the original SLDS. If you use component styling hooks, limit the components to SLDS themes until SLDS 2 and the Salesforce Cosmos theme become generally available.
See Buttons: Styling Hooks Overview for documentation on component-specific hooks for this component. Custom properties for buttons work only with particular lightning-button variants.
| CSS Custom Property | lightning-button Variants |
|---|---|
--slds-c-button-* | all |
--slds-c-button-color-* | base |
--slds-c-button-text-color-* | neutral (default), base, and brand-outline |
--slds-c-button-neutral-* | neutral (default) |
--slds-c-button-brand-* | brand |
--slds-c-button-outline-brand-* | brand-outline |
--slds-c-button-destructive-* | destructive |
--slds-c-button-text-destructive-* | destructive-text |
--slds-c-button-inverse-* | inverse |
--slds-c-button-success-* | success |
For more information, see Style Components Using Lightning Design System Styling Hooks in the Lightning Web Components Developer Guide.
To create an icon-only button, use the lightning-button-icon component instead. Creating an icon-only button using lightning-button results in additional spacing next to the icon. To create a button that triggers a menu, use lightning-button-menu.
Icons are not available in Lightning Out, but they are available in Lightning Components for Visualforce and other experiences.
If you're creating forms to interact with Salesforce records, consider using lightning-record-form or lightning-record-edit-form.
On mobile screens, we recommend setting the button size to 44x44 px. If you have more than one target on a screen that performs the same action, only one of the targets need to meet the target size of 44x44 px. For more information, see Follow Accessible Mobile Design Guidelines.
Buttons must have an accessible name to enable assistive technology to describe the button's purpose. Provide this name using the label attribute and make it a clear call to action, for example, "Edit record".
If you create an icon-only button using lightning-button, provide an accessible name using the aria-label or aria-labelledby attribute. We recommend that you use one of the attributes but not both.
To provide a text label that's not visible on the screen, use aria-label.
To associate the button with text from another element, use aria-labelledby.
To use aria-label with additional descriptive text, use aria-describedby.
lightning-button provides many variants that add color to a button, which convey different meaning on a button. Use the variants together with clear text on the button to match the meaning you are trying to convey via color. For example, if you use the destructive button to point out a potential warning, make sure the text communicates the same message.
To inform screen readers that a button is disabled, include the disabled attribute.
Use the following accessibility and aria attributes on lightning-button.
| Attribute | Type | Description |
|---|---|---|
| accesskey | string | A shortcut key to activate or place focus on the button. |
| aria-atomic | boolean | Specifies whether the screen reader should always present the live region as a whole, even if only part of the region changes. The default is false. |
| aria-controls | ID reference list | An element ID or a space-separated list of element IDs whose presence or content is controlled by this button. |
| aria-describedby | ID reference list | An element ID or a space-separated list of element IDs that provide a descriptive label or description for the button. |
| aria-expanded | boolean | Indicates whether a collapsible element that's controlled by the button is expanded or collapsed. To reference the controlled element, use aria-controls. |
| aria-haspopup | token | Indicates that the button has an interactive popup element. Valid values are 'true', 'dialog', 'menu', 'listbox', 'tree', and 'grid'. |
| aria-label | string | Provides an assistive label where a visible label cannot be used. |
| aria-labelledby | ID reference list | Specifies the ID or list of IDs of the element or elements that contain visible descriptive text to describe the button. |
| aria-live | token | Indicates the button can dynamically update without a page reload, and specifies how the change is announced by assistive technologies. Possible values include off, polite, and assertive. The default is off. For the screen reader to announce changes when the user is idle, use polite. For immediate notifications, use assertive. |
| tabindex | integer | Specifies whether the button is focusable during tab navigation. Set tab index to -1 to prevent focus on the button during tab navigation. The default value is 0, which makes the button focusable during tab navigation. For more information see tabindex. |
For more information, see the WAI-ARIA Specification.