Skip to main content
Login

Sign in to access your account

Alert - National Design System

An alert shows a short status message, such as a success, a warning or an error, in the page or as a toast

Overview

An alert shows a short status message. It has a status icon, a title, a description and a close button. data-status sets the icon and the colors. An alert comes in three forms:

  • A standard alert sits in a page section.
  • An inline alert is one line next to a form or an action, where space is limited.
  • A toast floats over the page after an action, such as save, delete or submit, and can close itself.

Write the HTML for an alert that is in the page when it loads. Call NDS.Alert.create() for an alert that appears later, such as after a failed request. A toast is made only with create(): its timer and placement need the script.

Pick another component when:

  • the user must make a decision before they continue: Modal. An alert never blocks the page.
  • the page or view cannot show its content: the user must sign in, has no permission, the item is not found, or the load failed with nothing else to show: Status Section. Inside a tab panel, use an alert: the other tabs still work

Markup

Options
Structure
Status
Actions
Position
More
Preview
<div class="nds-alert nds-card" data-status="info" role="alert"> <span class="nds-feedback nds-alert-icon nds-outline"> <span class="nds-feedback-icon"> <i class="nds-icon" aria-hidden="true"></i> </span> </span> <div class="nds-alert-content"> <div class="nds-alert-text"> <span class="nds-alert-title">Update available</span> <p class="nds-alert-description">A new version is ready. Update now to get the latest fixes.</p> </div> </div> <button class="nds-btn nds-subtle nds-icon-only nds-alert-close" aria-label="Close alert" data-i18n-aria-label="alert.close_alert"> <i class="nds-icon nds-hgi-cancel-01" aria-hidden="true"></i> </button> </div>

Behavior

Toast

A toast is an alert that floats at a corner of the screen and fades in, so it needs no target. Only NDS.Alert.create() makes one, with display: 'toast', because its placement and timer need the script. Top toasts sit below the sticky header, with the newest first. Bottom toasts stack with the newest last. On a phone, a toast takes the full width. A toast shows above an open Modal or Panel, and the user can hover it or close it there. To dock toasts in a place of your own, add a .nds-alert-placeholder with data-position.

Auto Close

A toast with a duration closes itself after that many milliseconds, and a ring on its close button counts down. The timer pauses while the pointer is over the toast or the keyboard focus is in it, and resumes when they leave. A click stops the timer for good, and the ring goes away: the toast then stays until the user closes it. On a touch screen, a tap does the same.

Built-in Features

  • Status Icon and Color

    data-status sets the icon, the stripe color and the tint. The icon element stays empty in the markup.

  • Close Button

    The close button removes the alert. It works on alerts in the page at load. For alerts you add later, call NDS.Alert.init(), or create them with NDS.Alert.create().

  • Safe Content

    create() escapes the title and the description. An action with an unsafe href, such as javascript:, becomes a plain button.

Best Practices

  • Pick the status that matches the message: success for a confirmation, info for news, warning for a risk, error for a failure, critical for a system-level emergency, and neutral for a general notice. Do not use a status for its color only.
  • Add nds-color in dense layouts, where the stripe alone does not stand out. An inline alert is always tinted, so it needs no nds-color.
  • Keep the description to one or two sentences. For more, add a link to a detail page.
  • Add actions when the user must respond, such as retry, undo or update. Do not add an action that only closes the alert; the close button does that.
  • Add a copy action for text the user will paste somewhere else, such as a code, a reference number or an error log.
  • Do not use an alert in place of a page the user cannot use, such as a sign-in gate or an access-denied page. Show a Status Section instead: there, the message is the page.
  • Do not use a toast for a critical error or a message the user must act on. A toast can close before the user reads it.
  • Give a toast a duration of 3000 to 5000 ms. Under 2000 ms is too short to read.
  • Prefer the logical toast positions (-start, -end), which follow the text direction. Use -left and -right only when the toast must stay at one physical edge.

API

Data Attributes

Attribute Element Effect
data-status .nds-alert The status: info, success, warning, error, critical or neutral. Sets the icon and the colors
data-position .nds-alert-placeholder Where toasts dock. Place your own placeholder with this attribute, and toasts for that position go into it instead of a new one
data-alert-close A button inside .nds-alert Closes the alert on click, as data-modal-close does on a modal. .nds-alert-close still works, and it also places the built close button and makes it MD. A size class on the button still wins
data-state .nds-toast Set by the script: toast-show, toast-hide and paused. Do not set it yourself

CSS Custom Properties

Property Default Controls
--alert-stripe var(--border-neutral-primary) Stripe color. Each status sets its own
--alert-icon-bg var(--background-neutral-light) Background of the icon circle. Each status sets its own
--progress-duration 4000ms Length of the countdown ring on .nds-alert-close. create() sets it from duration

An alert is a card, so the card color properties (--card-bg, --card-border-color and --card-title-color) also apply. See Cards.

Tokens

It reads these shared tokens. A change to one also changes other components: see Tokens.

Source: the var() reads in _sass/components/_alert.scss.

TokenPreviewValue
--background-default
Read by background-color

var(--colors-base-white)
Dark mode var(--colors-neutral-800)
--background-neutral-light
Read by background-color

var(--colors-neutral-100)
Dark mode var(--colors-alpha-white-10)
--background-success
Read by --alert-stripe
var(--colors-green-600)
--background-success-faint
var(--colors-green-25)
Dark mode var(--colors-green-alpha-10)
--background-info
Read by --alert-stripe
var(--colors-blue-600)
--background-info-faint
var(--colors-blue-25)
Dark mode var(--colors-blue-alpha-10)
--background-warning
Read by --alert-stripe
var(--colors-yellow-600)
--background-warning-faint
var(--colors-yellow-25)
Dark mode var(--colors-yellow-alpha-10)
--background-error
Read by --alert-stripe
var(--colors-red-600)
--background-error-faint
var(--colors-red-25)
Dark mode var(--colors-red-alpha-10)
--text-primary-paragraph
Read by color

var(--colors-neutral-700)
Dark mode var(--colors-neutral-300)
--text-success
var(--colors-green-700)
Dark mode var(--colors-green-400)
--text-info
var(--colors-blue-700)
Dark mode var(--colors-blue-400)
--text-warning
var(--colors-yellow-700)
Dark mode var(--colors-yellow-400)
--text-error
var(--colors-red-700)
Dark mode var(--colors-red-400)
--border-neutral-light
Read by --alert-stripe

var(--colors-neutral-300)
Dark mode var(--colors-neutral-400)
--border-neutral-primary
Read by --alert-stripe

var(--colors-neutral-300)
Dark mode var(--colors-neutral-600)
--featuredicons-background-error-light
Read by --alert-icon-bg

var(--background-error-light)
Dark mode var(--colors-red-alpha-10)
--featuredicons-background-info-light
Read by --alert-icon-bg

var(--background-info-light)
Dark mode var(--colors-blue-alpha-10)
--featuredicons-background-warning-light
Read by --alert-icon-bg

var(--background-warning-light)
Dark mode var(--colors-yellow-alpha-10)
--featuredicons-background-success-light
Read by --alert-icon-bg

var(--background-success-light)
Dark mode var(--colors-green-alpha-10)
--featuredicons-background-neutral-light
Read by --alert-icon-bg

var(--background-neutral-light)
Dark mode var(--colors-alpha-white-10)
--link-neutral
Read by --link-color

var(--colors-neutral-700)
Dark mode var(--colors-neutral-200)
--link-neutral-hovered
Read by --link-hover

var(--colors-neutral-500)
Dark mode var(--colors-neutral-400)
--link-neutral-pressed
Read by --link-pressed

var(--colors-neutral-400)
Dark mode var(--colors-neutral-500)
--link-neutral-focused
Read by --link-focused

var(--colors-neutral-700)
Dark mode var(--colors-neutral-200)
--link-neutral-visited
Read by --link-visited

var(--colors-neutral-600)
Dark mode var(--colors-neutral-300)

JavaScript

Method Effect
NDS.Alert.create(options) Builds an alert, inserts it and returns the element. Before the alert’s bundle loads, the call loads it and returns a promise of the element: await the call when you use what it returns
NDS.Alert.dismiss(elOrSelector) Removes one alert. A toast fades out first
NDS.Alert.dismissAll(container) Removes every alert inside a container, given as an element or a selector
NDS.Alert.init() Wires the close button on alerts added to the page after load
Option Default Effect
status 'info' The status, as in data-status
title '' Title text. Leave it empty for no title
description '' Description text
display 'default' 'default', 'inline' or 'toast'
target null Element or selector to insert into. A toast needs none. The alert has no outer margin: in a container with no gap, add nds-block to the element create() returns
prepend false Insert at the start of target, not at the end
closable true Show the close button. A toast with a duration always has one
shadow true for a toast, else false Add .nds-shadow
color false Add .nds-color
id null Id of the alert element. The new alert replaces an alert in the page with the same id, so a repeated toast does not stack
position 'top' Toast position: top or bottom, with an optional -start, -end, -left or -right. -start and -end follow the text direction, so -start is the left in English and the right in Arabic. top and bottom alone sit at the end side
duration 0 Toast auto-close time in ms. 0 never closes
actions [] Action objects (next table)
Action key Effect
label Button text
variant, size Button classes, nds-{variant} nds-{size}. Default subtle and sm
class Replaces the button classes. Use 'nds-link' for a text link
href, target Makes the action a link. target adds rel="noopener noreferrer"
onClick(alertEl) Runs on click
dismiss true closes the alert after the click
copy, copyTarget Copies this text, or the text of the element this selector finds. Adds the copy icon, a checkmark flash and a screen-reader announcement

Alerts fire no events. Use onClick on an action. The full API is in the banner of _js/nds-alert.js.

var alert = NDS.Alert.create({ status: 'error', title: 'Request failed', description: 'The request could not be completed.', target: '#messages', prepend: true, actions: [ { label: 'Retry', variant: 'primary', onClick: function () { retry(); }, dismiss: true }, { label: 'Copy error log', copy: error.stack } ] }); // Later NDS.Alert.dismiss(alert); NDS.Alert.dismissAll('#messages');
Last Modified Date: 05/11/2026 - 03:30 PM
Was this page useful?
60% of users said Yes from 2843 Feedbacks