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.
1<template>2 <lightning-button3 variant="brand"4 label="Start"5 title="Begins the task"6 onclick={handleClick}7 >8 </lightning-button>9</template>
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.
Component Styling
lightning-button implements the
buttons blueprint in the Salesforce Lightning Design System (SLDS).
Icons
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.
Use the variant attribute with one of these values to apply styling and animation.
base is a button without a border, which gives it the look of a plain text link.
neutral is the default variant, a plain uncolored button.
brand is a blue button, used to draw attention to the primary action on a page.
brand-outline is similar to brand but the color is used for the label and border only, not the button color.
destructive is a red button used to warn users that its action has a negative effect.
destructive-text is similar to destructive but only the label and border are red.
inverse uses the background color and light text, useful for dark backgrounds.
success is a green button used to indicate a successful action.
Animation
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.
1<template>2 <lightning-button3 variant="brand"4 label="Start"5 title="Begins the task"6 disable-animation7 onclick={handleClick}>8 </lightning-button>9</template>
We recommend setting this attribute only when you place lightning-button near other
buttons that aren't animated, for consistency.
Customize Component Styling
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 provide CSS custom properties that use the --slds-c-* prefix and they change styling for specific elements or properties of a component. Component styling hooks are supported for SLDS 1 only. See the SLDS 1 component blueprints for available component styling hooks.
lightning-button supports these component styling hooks.
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
Usage Considerations
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.
Accessibility
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.
1<h2 id="downloadLabel">Download Files</h2>2<h3 id="downloadDesc">View and make changes to your files</h3>3<lightning-button4 aria-labelledby="downloadLabel downloadDesc"5 icon-name="utility:download"6 variant="base"></lightning-button>
To use aria-label with additional descriptive text, use aria-describedby.
1<lightning-button2 aria-label="Close"3 aria-describedby="descriptionClose"4 icon-name="utility:close"5 variant="base"></lightning-button>678<div id="descriptionClose">Closing this window resets the form and9returns you back to the main page.</div>
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.
Reserved for internal use. If present, disables button animation.
icon-name
The Lightning Design System name of the icon. Names are written in the format 'utility:down' where 'utility' is the category, and 'down' is the specific icon to be displayed.
string
icon-position
Describes the position of the icon with respect to the button label. Options include left and right. This value defaults to left.
string
left
label
The text to be displayed inside the button.
string
name
The name for the button element. This value is optional and can be used to identify the button in a callback.
string
stretch
Setting it to true allows the button to take up the entire available width. This value defaults to false.
boolean
false
tab-index
Reserved for internal use only. Use the global tabindex attribute instead. 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.
number
type
Specifies the type of button. Valid values are button, reset, and submit. This value defaults to button.
string
button
value
The value for the button element. This value is optional and can be used when submitting a form.
string
variant
The variant changes the appearance of the button. Accepted variants include base, neutral, brand, brand-outline, destructive, destructive-text, inverse, and success. This value defaults to neutral.