← Back to Bootstrap Course | Chapter 7: Components — Cards & Alerts | Lesson 4 of 7

Alert types

Alerts show short, coloured messages about success, warnings or errors.

In this page:

  1. Alert types
Syntax
markup
<div class="alert alert-success" role="alert">Message</div>
<div class="alert alert-danger" role="alert">Message</div>

Alert types

Use .alert with a contextual class such as .alert-success, .alert-warning, .alert-danger or .alert-info and role="alert".

Links inside use .alert-link. You can add headings and paragraphs.

Colour should be supported by text, since not all users see colour.

Note: role="alert" makes screen readers announce the message.

Example: Alert types

markup
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="container">
  <div class="alert alert-success" role="alert">Saved! Your changes were stored.</div>
  <div class="alert alert-warning" role="alert">Warning: disk space is low.</div>
  <div class="alert alert-danger" role="alert">Error: could not connect. <a href="#" class="alert-link">Try again</a></div>
  <div class="alert alert-info" role="alert">Tip: press ? to see shortcuts.</div>
</div>
</body>
</html>
Live Example
Related Topics
Common Mistakes
  1. Missing role alert
  2. Relying only on colour
  3. Using alerts for permanent content
Chapter Summary
  • alert plus contextual class
  • role="alert" for accessibility
  • alert-link styles links
  • Colour needs supporting text
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.