Loading...
Skip to main content

Alert

Provide contextual feedback messages for typical user actions with the handful of available and flexible alert messages.

Default alert

Alerts are flex containers that can contain any length of text and an optional close button. They’re designed to be used with theme colors, but have neutral fallback colors when no theme is applied. For inline dismissal, use the dismissible prop. Add custom content as children and style it with components or className utilities as needed.

<Alert>
<p>
A simple alert—check it out!
</p>
</Alert>

Variants

Use any of our theme values for color mode adaptive, contextual styling.

<Stack gap={3}>
<Alert theme="primary">
<p>
A simple primary alert—check it out!
</p>
</Alert>
<Alert theme="accent">
<p>
A simple accent alert—check it out!
</p>
</Alert>
<Alert theme="secondary">
<p>
A simple secondary alert—check it out!
</p>
</Alert>
<Alert theme="success">
<p>
A simple success alert—check it out!
</p>
</Alert>
<Alert theme="danger">
<p>
A simple danger alert—check it out!
</p>
</Alert>
<Alert theme="warning">
<p>
A simple warning alert—check it out!
</p>
</Alert>
<Alert theme="info">
<p>
A simple info alert—check it out!
</p>
</Alert>
<Alert theme="inverse">
<p>
A simple inverse alert—check it out!
</p>
</Alert>
</Stack>

Color alone does not convey meaning to assistive technologies. Include visible text or additional visually hidden text to communicate the purpose of the message.

Live example

Click the button below to show an alert (hidden with inline styles to start), then dismiss (and destroy) it with the built-in close button.

Link color

Use the AlertLink component to quickly provide matching colored links within any alert.

<Stack gap={3}>
<Alert theme="primary">
<p>
A simple primary alert with

<AlertLink href="#link-color">
an example link
</AlertLink>
. Give it a click if you
like.
</p>
</Alert>
<Alert theme="accent">
<p>
A simple accent alert with{' '}
<AlertLink href="#link-color">
an example link
</AlertLink>

. Give it a click if you like.
</p>
</Alert>
<Alert theme="secondary">
<p>
A simple secondary alert with

<AlertLink href="#link-color">
an example link
</AlertLink>
. Give it a click if you
like.
</p>
</Alert>
<Alert theme="success">
<p>
A simple success alert with

<AlertLink href="#link-color">
an example link
</AlertLink>
. Give it a click if you
like.
</p>
</Alert>
<Alert theme="danger">
<p>
A simple danger alert with{' '}
<AlertLink href="#link-color">
an example link
</AlertLink>

. Give it a click if you like.
</p>
</Alert>
<Alert theme="warning">
<p>
A simple warning alert with

<AlertLink href="#link-color">
an example link
</AlertLink>
. Give it a click if you
like.
</p>
</Alert>
<Alert theme="info">
<p>
A simple info alert with{' '}
<AlertLink href="#link-color">
an example link
</AlertLink>
.
Give it a click if you like.
</p>
</Alert>
<Alert theme="inverse">
<p>
A simple inverse alert with

<AlertLink href="#link-color">
an example link
</AlertLink>
. Give it a click if you
like.
</p>
</Alert>
</Stack>

With icons

Render Icon inside Alert for an icon, and use flexbox utilities through className to align the content. Depending on the icon and content, you may need additional utilities or custom styles.

<Alert
className="align-items-center"
theme="primary"
>
<Icon
aria-label="Warning:"
name="exclamation-triangle-fill"
/>
<p>
An example alert with an icon
</p>
</Alert>

Dismiss

Use the dismissible prop to dismiss an alert inline. Here’s how:

  • dismissible adds a close button to the alert.
  • Use onClosed to synchronize application state after dismissal.

An alert fades out before it is removed. The wrapper adds its exit state for the length of the transition, so no extra classes are needed in your markup. Pass instant to remove the alert at once instead. See Transitions for the tokens that tune the fade.

You can see this in action with a live demo:

<Alert
dismissible
theme="warning"
>
<p>
Something’s wrong, check the fields below and try again.
</p>
</Alert>

When an alert is dismissed, the element is completely removed from the page structure. If a keyboard user dismisses the alert using the close button, their focus will suddenly be lost and, depending on the browser, reset to the start of the page/document. For this reason, we recommend including additional JavaScript that handles the onClosed callback and programmatically sets focus() to the most appropriate location in the page. If you’re planning to move focus to a non-interactive element that normally does not receive focus, make sure to add tabindex="-1" to the element.

Additional content

For simple vertical layouts, it should be enough to add the flex-column class to the alert.

<Alert
className="flex-column align-items-stretch"
theme="success"
>
<AlertHeading className="mb-2">
Well done!
</AlertHeading>
<p>
Aww yeah, you successfully read this important alert message. This example text is going
to run a bit longer so that you can see how spacing within an alert works with this kind
of content.
</p>
<hr className="my-3" />
<p>
Whenever you need to, be sure to use margin utilities to keep things nice and tidy.
</p>
</Alert>

More complex layouts may require additional HTML within the alert—like when mixing in icons and dismiss buttons.

<Alert
dismissible
theme="warning"
>
<div className="pe-3">
<AlertHeading className="mb-2">
More complex alert
</AlertHeading>
<p className="fs-sm mb-0">
Aww yeah, you successfully read this important alert message. This example text is
going to run a bit longer so that you can see how spacing within an alert works with
this kind of content.
</p>
</div>
</Alert>

CSS

Variables

Local CSS custom properties expose the component’s colors, spacing, borders, and transitions. Override them through style or a CSS module. The defaults below come from the Bootstrap stylesheet used by Idea React; see the upstream variable reference.

VariableDefault
--bs-alert-gapvar(--bs-spacer-3)
--bs-alert-bgvar(--bs-bg-1)
--bs-alert-padding-xvar(--bs-spacer)
--bs-alert-padding-yvar(--bs-spacer)
--bs-alert-colorinherit
--bs-alert-border-colorvar(--bs-border-color)
--bs-alert-border-radiusvar(--bs-radius-7)
--bs-alert-link-colorinherit
--bs-alert-transition-propertyopacity
--bs-alert-transition-duration0.15s
--bs-alert-transition-timinglinear

JavaScript behavior

The React wrapper manages dismissal and removes the alert after its fade transition. Do not initialize a Bootstrap plugin on the same element.

Events

onClose runs when dismissal begins, and onClosed runs after the transition and removal. Use onClosed to restore keyboard focus to an appropriate control, as in the live example.

Documentation navigation
On this page
On this page