Skip to main content

Components

Alerts

Contextual feedback messages in all eleven theme colors, dismissible through a native dialog form.

How it works

A static alert is the same <div class="alert"> you know. A dismissible one is a <dialog open class="alert alert-dismissible">. Its close button sits inside a <form method="dialog">; submitting that form removes the open attribute and the alert disappears. Nothing else is involved.

  • Baseline: <dialog open> + <form method="dialog"> (every current engine).
  • Enhancement: none needed. .fade is accepted for markup parity but a closed dialog is display: none immediately, so there is no fade-out.
  • Fallback: a dialog without open is hidden, so the alert never flashes back after being closed.
Do not use data-bs-dismiss="alert"; it is inert. The theme resets dialog.alert to flow in the document like a normal block (no centering, no max-width) in scss/components/_alert.scss.

Examples

Alerts take one of the contextual classes. There are eleven: Bootstrap's eight plus the brand's gold, navy and cream. Each uses the matching *-bg-subtle, *-border-subtle and *-text-emphasis colors, which flip automatically in dark mode.

HTML
<div class="alert alert-primary" role="alert">A simple primary alert — check it out!</div>
<div class="alert alert-secondary" role="alert">A simple secondary alert — check it out!</div>
<div class="alert alert-success" role="alert">A simple success alert — check it out!</div>
<div class="alert alert-danger" role="alert">A simple danger alert — check it out!</div>
<div class="alert alert-warning" role="alert">A simple warning alert — check it out!</div>
<div class="alert alert-info" role="alert">A simple info alert — check it out!</div>
<div class="alert alert-light" role="alert">A simple light alert — check it out!</div>
<div class="alert alert-dark" role="alert">A simple dark alert — check it out!</div>
<div class="alert alert-gold" role="alert">A simple gold alert — title-belt yellow, for a new champion.</div>
<div class="alert alert-navy" role="alert">A simple navy alert — bracket-board blue, for the tale of the tape.</div>
<div class="alert alert-cream" role="alert">A simple cream alert — program paper, for the quiet notes.</div>
Color alone does not convey meaning. Add the context in the text itself, or with a .visually-hidden prefix, so assistive technology and colour-blind readers get it too.

Live example

The same thing, but as a dialog. Click the close button and it is gone. There is no event to hook, so the removal is final for the page's lifetime — reload to bring it back.

Three count. Your changes were saved and the referee's hand hit the mat.
HTML
<dialog open class="alert alert-success alert-dismissible fade" aria-label="Success">
  <strong>Three count.</strong> Your changes were saved and the referee's hand hit the mat.
  <form method="dialog">
    <button type="submit" class="btn-close" aria-label="Close"></button>
  </form>
</dialog>

Use .alert-link on anchors inside an alert to match the text emphasis colour.

HTML
<div class="alert alert-primary" role="alert">A simple primary alert with <a href="#!" class="alert-link">an example link</a>. Give it a click if you like.</div>
<div class="alert alert-success" role="alert">A simple success alert with <a href="#!" class="alert-link">an example link</a>. Give it a click if you like.</div>
<div class="alert alert-danger" role="alert">A simple danger alert with <a href="#!" class="alert-link">an example link</a>. Give it a click if you like.</div>
<div class="alert alert-gold" role="alert">A simple gold alert with <a href="#!" class="alert-link">an example link</a>. Give it a click if you like.</div>
<div class="alert alert-navy" role="alert">A simple navy alert with <a href="#!" class="alert-link">an example link</a>. Give it a click if you like.</div>

Additional content

Alerts can hold headings (.alert-heading), paragraphs and dividers.

HTML
<div class="alert alert-success" role="alert">
  <h4 class="alert-heading">Well done!</h4>
  <p>Aw yeah, you successfully read this important alert message. This example text runs a bit longer so you can see how spacing works inside an alert.</p>
  <hr>
  <p class="mb-0">Whenever you need to, use margin utilities to keep things tidy.</p>
</div>

Icons

Inline an SVG and use flexbox utilities to line it up with the text. The theme ships no icon font; any inline SVG inherits currentColor.

HTML
<div class="alert alert-primary d-flex align-items-center" role="alert">
  <svg class="flex-shrink-0 me-2" width="24" height="24" viewBox="0 0 16 16" fill="currentColor" role="img" aria-label="Info:"><path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2"/></svg>
  <div>An example alert with an icon</div>
</div>

Define the symbols once and reuse them with <use>:

HTML
<svg xmlns="http://www.w3.org/2000/svg" class="d-none">
  <symbol id="alerts-icon-check" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0m-3.97-3.03a.75.75 0 0 0-1.08.022L7.477 9.417 5.384 7.323a.75.75 0 0 0-1.06 1.06L6.97 11.03a.75.75 0 0 0 1.079-.02l3.992-4.99a.75.75 0 0 0-.01-1.05z"/></symbol>
  <symbol id="alerts-icon-info" viewBox="0 0 16 16"><path d="M8 16A8 8 0 1 0 8 0a8 8 0 0 0 0 16m.93-9.412-1 4.705c-.07.34.029.533.304.533.194 0 .487-.07.686-.246l-.088.416c-.287.346-.92.598-1.465.598-.703 0-1.002-.422-.808-1.319l.738-3.468c.064-.293.006-.399-.287-.47l-.451-.081.082-.381 2.29-.287zM8 5.5a1 1 0 1 1 0-2 1 1 0 0 1 0 2"/></symbol>
  <symbol id="alerts-icon-exclaim" viewBox="0 0 16 16"><path d="M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5m.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2"/></symbol>
</svg>
<div class="alert alert-primary d-flex align-items-center" role="alert">
  <svg class="flex-shrink-0 me-2" width="24" height="24" fill="currentColor" role="img" aria-label="Info:"><use href="#alerts-icon-info"/></svg>
  <div>An example alert with an icon</div>
</div>
<div class="alert alert-success d-flex align-items-center" role="alert">
  <svg class="flex-shrink-0 me-2" width="24" height="24" fill="currentColor" role="img" aria-label="Success:"><use href="#alerts-icon-check"/></svg>
  <div>An example success alert with an icon</div>
</div>
<div class="alert alert-warning d-flex align-items-center" role="alert">
  <svg class="flex-shrink-0 me-2" width="24" height="24" fill="currentColor" role="img" aria-label="Warning:"><use href="#alerts-icon-exclaim"/></svg>
  <div>An example warning alert with an icon</div>
</div>
<div class="alert alert-danger d-flex align-items-center" role="alert">
  <svg class="flex-shrink-0 me-2" width="24" height="24" fill="currentColor" role="img" aria-label="Danger:"><use href="#alerts-icon-exclaim"/></svg>
  <div>An example danger alert with an icon</div>
</div>

Dismissing

To make an alert dismissible:

  1. Use <dialog open> instead of <div>. The open attribute is what shows it.
  2. Add .alert-dismissible for the extra right padding.
  3. Put a <button type="submit" class="btn-close"> inside a <form method="dialog">. The form is display: contents, so the button is positioned top-right as usual.
Heads up. The steel cage is still being bolted together. Bell time may slip.
HTML
<dialog open class="alert alert-warning alert-dismissible fade" aria-label="Warning">
  <strong>Heads up.</strong> The steel cage is still being bolted together. Bell time may slip.
  <form method="dialog">
    <button type="submit" class="btn-close" aria-label="Close"></button>
  </form>
</dialog>

Any button in the form closes the dialog, so a text button works as well as the icon. Stacked alerts each need their own dialog.

Card subject to change

The main event has been moved to the second hour. Tickets remain valid.

HTML
<dialog open class="alert alert-navy alert-dismissible" aria-label="Notice">
  <h4 class="alert-heading">Card subject to change</h4>
  <p class="mb-2">The main event has been moved to the second hour. Tickets remain valid.</p>
  <form method="dialog" class="d-flex gap-2">
    <button type="submit" class="btn btn-sm btn-navy">Got it</button>
    <button type="submit" class="btn-close" aria-label="Close"></button>
  </form>
</dialog>
Because the alert is a <dialog>, it has an implicit dialog role. Give it an aria-label or aria-labelledby. Do not add role="alert" on top.

Accessibility

  • Static alerts keep role="alert" only when they appear dynamically after load; for content present on load, prefer a plain <div> or role="status".
  • A closed <dialog> is removed from the accessibility tree, exactly like a removed node.
  • The close button is a real <button> with an aria-label; focus returns to the document after submit.
  • Dark mode: the .btn-close icon inverts automatically through --bs-btn-close-filter.

CSS

All of Bootstrap's --bs-alert-* variables apply. The theme uses the 2 px bracket radius.

.alert {
  --bs-alert-padding-x: 1rem;
  --bs-alert-padding-y: 1rem;
  --bs-alert-margin-bottom: 1rem;
  --bs-alert-border-radius: 2px;          // $alert-border-radius: $border-radius-sm
}

Additional rules from scss/components/_alert.scss:

dialog.alert               // reset to a normal in-flow block
dialog.alert:not([open])   // hidden once dismissed
.alert-dismissible > form  // display: contents, so .btn-close positions against the alert

The contextual variants are generated from $theme-colors; adding a key there (see Color) yields a matching .alert-* class.