Skip to main content

Blazor Alerts

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

Parameters

NameTypeDefaultRequiredDescriptionAdded Version
ChildContentRenderFragment?✔️Gets or sets the content to be rendered within the component.1.0.0
ColorAlertColorAlertColor.NoneGets or sets the alert color.1.0.0
DismissableboolfalseIf true, shows an inline close button.1.0.0

Methods

NameDescriptionAdded Version
CloseAsyncCloses an alert by removing it from the DOM.1.0.0

Callback Events

NameDescriptionAdded Version
OnCloseFires immediately when the close instance method is called.1.0.0
OnClosedFired when the alert has been closed and CSS transitions have completed.1.0.0

Examples

Alerts

Alerts are available for any length of text, as well as an optional close button. For proper styling, use one of the eight colors.

Blazor Bootstrap: Alert Component
<div>
<Alert Color="AlertColor.Primary"> A simple primary alert - check it out! </Alert>
<Alert Color="AlertColor.Secondary"> A simple secondary alert - check it out! </Alert>
<Alert Color="AlertColor.Success"> A simple success alert - check it out! </Alert>
<Alert Color="AlertColor.Danger"> A simple danger alert - check it out! </Alert>
<Alert Color="AlertColor.Warning"> A simple warning alert - check it out! </Alert>
<Alert Color="AlertColor.Info"> A simple info alert - check it out! </Alert>
<Alert Color="AlertColor.Light"> A simple light alert - check it out! </Alert>
<Alert Color="AlertColor.Dark"> A simple dark alert - check it out! </Alert>
</div>

See alerts demo here.

Additional Content

Alerts can also contain additional HTML elements like headings, paragraphs and dividers.

Blazor Bootstrap: Alert Component - Alerts with additional content
<div>
<Alert Color="AlertColor.Success">
<h4 class="alert-heading">Well done!</h4>
<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>
<p class="mb-0">Whenever you need to, be sure to use margin utilities to keep things nice and tidy.</p>
</Alert>
</div>

See alerts with additional content demo.

Icons

Similarly, you can use Bootstrap Icons to create alerts with icons.

Blazor Bootstrap: Alert Component - Alerts with an icon
<div>
<Alert Color="AlertColor.Primary"> <Icon Name="IconName.InfoCircleFill" class="me-2"></Icon>An example alert with an icon </Alert>
<Alert Color="AlertColor.Success"> <Icon Name="IconName.CheckCircleFill" class="me-2"></Icon>A simple success alert with an icon </Alert>
<Alert Color="AlertColor.Danger"> <Icon Name="IconName.ExclamationTriangleFill" class="me-2"></Icon>A simple danger alert with an icon </Alert>
<Alert Color="AlertColor.Warning"> <Icon Name="IconName.ExclamationTriangleFill" class="me-2"></Icon>A simple warning alert with an icon </Alert>
</div>

See alerts with an icon demo.

Dismissing

  1. Using the Dismissable="true", it's possible to dismiss any alert inline.

It's possible to dismiss any alert inline. Here's how:

Blazor Bootstrap: Alert Component - Dismissing
<div>
<Alert Color="AlertColor.Warning" Dismissable="true"> <strong>Holy guacamole!</strong> You should check in on some of those fields below. </Alert>
</div>
  1. Manually we can close an alert with button click.
Blazor Bootstrap: Alert Component - Dismissing manually
<div>
<Alert @ref="warningAlert" Color="AlertColor.Warning">
<strong>Holy guacamole!</strong> You should check in on some of those fields below. <Button Color="ButtonColor.Primary" @onclick="CloseAlert">Close</Button>
</Alert>
</div>
@code {
Alert warningAlert;

private async Task CloseAlert() => await warningAlert?.CloseAsync();
}

See dismissable alerts demo.

NOTE

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 subscribing to the OnClosed callback event and programmatically sets focus to the most appropriate location on the page.