Blazorise Alert component
Draw attention to important information with contextual feedback messages.
Use the Alert component for confirmations, warnings, and errors that users need to notice within a page.
Structure
An Alert contains a message, an optional description, and an optional close button.
<Alert>Contains the feedback message.<AlertMessage>The main message.<AlertDescription>Optional supporting details.<CloseButton>An optional control for dismissing the alert.
When to use
- When you need to show alert messages to users.
- When you need a persistent static container which is closable by user actions.
Examples
Basic
<Alert Color="Color.Success" Visible> <AlertMessage>Well done!</AlertMessage> <AlertDescription>You successfully read this important alert message.</AlertDescription> </Alert>
Custom colorsAdded in v2.3
Use a custom CSS color to style the alert background, border, and text.<Alert Color="@("#0F766E")" Visible> <AlertMessage>Export ready.</AlertMessage> <AlertDescription>Your report is ready to download.</AlertDescription> </Alert> <Alert Color="@CssColor.Variable("--accent", "#7C3AED")" Visible> <AlertMessage>Draft saved.</AlertMessage> <AlertDescription>You can continue editing whenever you are ready.</AlertDescription> </Alert> <Alert Color="@(new Color(CssColor.Rgb(234, 88, 12)))" Visible> <AlertMessage>Review needed.</AlertMessage> <AlertDescription>Check the selected files before continuing.</AlertDescription> </Alert>
Close button
Adding aCloseButton automatically applies the padding and positioning needed for dismissal.
An explicit Dismisable value overrides this behavior: use true for custom close controls or false to opt out of automatic styling.
The close button's AutoClose parameter controls whether clicking it hides the alert.
<Alert Color="Color.Success" @bind-Visible=""> <AlertMessage> Alert Link. </AlertMessage> <AlertDescription> Lorem ipsum dolor sit amet, consectetur adipisicing elit. </AlertDescription> <CloseButton /> </Alert>
@code { bool visible = true; }
Extra content
<Alert> can also contain additional HTML elements like headings and paragraphs, which will be styled with the appropriate color matching the variant.
Big one!
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum.
<Alert Color="Color.Info" @bind-Visible="" Dismisable> <Heading Size="HeadingSize.Is4" TextColor="TextColor.Success"> Big one! <CloseButton /> </Heading> <Paragraph> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Cras mattis consectetur purus sit amet fermentum. </Paragraph> <Paragraph> <Button Color="Color.Info">Wanna do this</Button> <Button Color="Color.Light">Or do this</Button> </Paragraph> </Alert>
@code { bool visible = true; }
Usages
Two-way binding
To show alert just setVisible attribute to true.
<Alert Color="Color.Success" @bind-Visible=""> <AlertMessage> Alert test. </AlertMessage> </Alert> <Button Clicked="" Color="Color.Primary">Toggle alert</Button>
@code { bool visible = true; Task OnButtonClick() { visible = !visible; return Task.CompletedTask; } }
Programmatically
<Alert @ref="myAlert" Color="Color.Success"> <AlertMessage> Alert test. </AlertMessage> </Alert> <Button Clicked="" Color="Color.Primary">Show alert</Button>
@code{ Alert myAlert; Task OnButtonClick() { return myAlert.Show(); } }
AnimationAdded in v2.4
SetAnimated="false" for immediate changes, or use AnimationDuration to adjust the duration in milliseconds.
A null duration preserves the provider's default; zero or a negative value disables animations.
Toggle the alert or use its close button to see the entrance and exit effects. The alert stays in layout until its exit animation finishes. Animations also work without a close button, and the initial visibility is applied immediately. Reduced-motion preferences disable the effects.
VisibleChanged reports the requested visibility without waiting for animation completion.
The effects use CSS display transitions and starting styles. Unsupported effects fall back to immediate visibility changes.
<Div Flex="Flex.Wrap.AlignItems.Center" Gap="Gap.Is3" Margin="Margin.Is3.FromBottom"> <Field Margin="Margin.Is0" Width="Width.Auto"> <Switch @bind-Value="animated">Animate transitions</Switch> </Field> <Field Margin="Margin.Is0" Width="Width.Auto"> <Switch @bind-Value="dismissable">Show close button</Switch> </Field> <Field Flex="Flex.InlineFlex.AlignItems.Center" Gap="Gap.Is2" Margin="Margin.Is0" Width="Width.Auto"> <FieldLabel Margin="Margin.Is0">Animation duration</FieldLabel> <FieldBody> <Select TValue="int?" @bind-Value="animationDuration" Width="Width.Auto"> <SelectItem TValue="int?" Value="@((int?)null)">Provider default</SelectItem> <SelectItem TValue="int?" Value="0">Immediate</SelectItem> <SelectItem TValue="int?" Value="100">100 ms</SelectItem> <SelectItem TValue="int?" Value="200">200 ms</SelectItem> <SelectItem TValue="int?" Value="400">400 ms</SelectItem> <SelectItem TValue="int?" Value="1000">1000 ms</SelectItem> </Select> </FieldBody> </Field> <Button Color="Color.Primary" Clicked="@(() => visible = !visible)">Toggle alert</Button> </Div> <Alert Color="Color.Success" Dismisable="" @bind-Visible="visible" Animated="" AnimationDuration=""> <AlertMessage>Settings saved</AlertMessage> <AlertDescription>The machine is ready for the next production run.</AlertDescription> @if ( dismissable ) { <CloseButton /> } </Alert>
@code { private bool animated = true; private int? animationDuration = 400; private bool dismissable = true; private bool visible = true; }
Best Practices
Make Messages Actionable
Write a concise message that explains what happened and, when applicable, what the user should do next. Match the color to the message severity, but include meaningful text or an appropriate icon so color is not the only indication of status.
Feedback Pattern
Alerts are appropriate for information that belongs in the current page and may need to remain visible. Make an alert dismissible only when it can be safely ignored, and keep blocking errors visible until the problem is resolved. For brief feedback about a completed action, use a Toast instead.
API
See the API reference for the parameters, events, methods, and related types available to the components covered on this page.