Skip to content

Alert ​

A callout for important information, with an icon, title and description, in default and destructive styles.

  • Version 1.0.0
  • Bootstrap 5.3+
  • Requires Bootstrap CSS + Bootstrap JS bundle
  • Source on GitHub

Installation ​

bash
npx bootcn add alert
bash
pnpm dlx bootcn add alert
bash
yarn dlx bootcn add alert
bash
bunx --bun bootcn add alert

The CLI also installs the design tokens and these components: none. It prints the <link> and <script> tags to add to your layout.

Usage ​

Use an alert for information the user should notice on the page. Add role="alert" only when you insert the alert into the page in response to something the user did (so screen readers announce it at once); static alerts need no role. The dismissible version uses Bootstrap's alert plugin (alert-dismissible fade show and data-bs-dismiss="alert").

Accessibility ​

  • The title and description are plain text, read in order.
  • The destructive alert is marked by its words as well as its color.
  • The close button has an accessible name ("Dismiss").

Dependencies ​

  • Bootstrap 5.3+ CSS and the JS bundle.
  • themes/tokens/tokens.css.

Source ​

The exact files the CLI installs (alert@1.0.0). View on GitHub · Report an issue

html
<div class="alert bootcn-alert">
  <svg
    class="bootcn-icon"
    width="16"
    height="16"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
    focusable="false"
  >
    <circle cx="12" cy="12" r="9" />
    <path d="M12 16v-4" />
    <path d="M12 8h.01" />
  </svg>
  <div class="bootcn-alert__body">
    <p class="bootcn-alert__title">Heads up</p>
    <p class="bootcn-alert__description">You can add components to your project with the CLI.</p>
  </div>
</div>

<div class="alert bootcn-alert bootcn-alert--destructive mt-3">
  <svg
    class="bootcn-icon"
    width="16"
    height="16"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
    focusable="false"
  >
    <circle cx="12" cy="12" r="9" />
    <path d="M12 8v4" />
    <path d="M12 16h.01" />
  </svg>
  <div class="bootcn-alert__body">
    <p class="bootcn-alert__title">Payment failed</p>
    <p class="bootcn-alert__description">
      Your card was declined. Check the details and try again.
    </p>
  </div>
</div>

<div class="alert alert-dismissible fade show bootcn-alert mt-3">
  <svg
    class="bootcn-icon"
    width="16"
    height="16"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    stroke-width="2"
    stroke-linecap="round"
    stroke-linejoin="round"
    aria-hidden="true"
    focusable="false"
  >
    <circle cx="12" cy="12" r="9" />
    <path d="m9 12 2 2 4-4" />
  </svg>
  <div class="bootcn-alert__body">
    <p class="bootcn-alert__title">Profile saved</p>
    <p class="bootcn-alert__description">Your changes are live.</p>
  </div>
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Dismiss"></button>
</div>
css
.bootcn-alert {
  --bs-alert-bg: var(--bootcn-background);
  --bs-alert-color: var(--bootcn-foreground);
  --bs-alert-border-color: var(--bootcn-border-color);
  --bs-alert-border-radius: var(--bootcn-radius-lg);
  --bs-alert-padding-x: 1rem;
  --bs-alert-padding-y: 0.875rem;
  --bs-alert-margin-bottom: 0;

  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  font-size: 0.875rem;
}

.bootcn-alert > .bootcn-icon {
  flex: none;
  margin-top: 0.125rem;
}

.bootcn-alert__body {
  min-width: 0;
}

.bootcn-alert__title {
  margin: 0;
  font-weight: 500;
  line-height: 1.25rem;
}

.bootcn-alert__description {
  margin: 0.25rem 0 0;
  color: var(--bootcn-muted-foreground);
}

.bootcn-alert--destructive {
  --bs-alert-color: var(--bootcn-destructive-text);
  --bs-alert-border-color: var(--bs-danger-border-subtle);
}

.bootcn-alert--destructive .bootcn-alert__description {
  color: var(--bootcn-destructive-text);
}

.bootcn-alert.alert-dismissible .btn-close {
  padding: 1.125rem 1rem;
}

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.