Skip to content
Related Articles

Related Articles

Improve Article
Bootstrap 4 | Alerts
  • Last Updated : 31 May, 2019

We often see certain alerts on some websites before or after completing an action. These alert messages are highlighted texts that are important to take in consideration while performing a process. Bootstrap allows showing these alert messages on the website using predefined classes.

The .alert class followed by contextual classes are used to display the alert message on website. The alert classes are: .alert-success, .alert-info, .alert-warning, .alert-danger, .alert-primary, .alert-secondary, .alert-light and .alert-dark.

Syntax:

<div class="alert> Contents... <div>

Example:




<!DOCTYPE html> 
<html lang="en"
<head
    <title>Bootstrap Alerts</title
      
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  
    <link rel="stylesheet" href=
      
    <script src=
    </script>
      
    <script src=
    </script>
      
    <script src=
    </script>
</head
  
<body
    <h1 style="color:green;text-align:center;">
        GeeksforGeeks
    </h1
          
    <div class="container">        
        <div class="alert alert-success">
            <strong>Success alert!</strong>
        </div>
          
        <div class="alert alert-info">
            <strong>Info alert!</strong>
        </div>
          
        <div class="alert alert-warning">
            <strong>Warning alert!</strong>
        </div>
          
        <div class="alert alert-danger">
            <strong>Danger alert!</strong>
        </div>
          
        <div class="alert alert-primary">
            <strong>primary alert!</strong>
        </div>
          
        <div class="alert alert-secondary">
            <strong>Secondary alert!</strong>
        </div>
          
        <div class="alert alert-light">
            <strong>Light alert!</strong>
        </div>
          
        <div class="alert alert-dark">
            <strong>Dark alert!</strong>
        </div>
    </div>
</body
  
</html>                        

Output:



Closing Alerts: The .alert-dismissible class is used within .container class to close the alert message. Then use class=”close” and data-dismiss=”alert” to link a button element.

Syntax:

<div class="alert alert_type alert-dismissible">
    <button type="button" class="close" data-dismiss="alert">x</button>
    Alert message
<div>

Example:




<!DOCTYPE html> 
<html lang="en"
<head
    <title>Bootstrap Alerts</title
      
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  
    <link rel="stylesheet" href=
      
    <script src=
    </script>
      
    <script src=
    </script>
      
    <script src=
    </script>
</head
  
<body
    <h1 style="color:green;text-align:center;">
        GeeksforGeeks
    </h1
          
    <div class="container">        
        <div class="alert alert-success alert-dismissible">
            <strong>Success alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-info alert-dismissible">
            <strong>Info alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-warning alert-dismissible">
            <strong>Warning alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-danger alert-dismissible">
            <strong>Danger alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-primary alert-dismissible">
            <strong>primary alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-secondary alert-dismissible">
            <strong>Secondary alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-light alert-dismissible">
            <strong>Light alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-dark alert-dismissible">
            <strong>Dark alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
    </div>
</body
  
</html>                    

Output:

Animated Alerts: The .fade and .show classes are used to add the fading effect of animation when closing the alert message.

Syntax:

<div class="alert alert_type alert-dismissible fade show">
    <button type="button" class="close" data-dismiss="alert">x</button>
    Alert message
<div>

Example:




<!DOCTYPE html> 
<html lang="en"
<head
    <title>Bootstrap Alerts</title
      
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
  
    <link rel="stylesheet" href=
      
    <script src=
    </script>
      
    <script src=
    </script>
      
    <script src=
    </script>
</head
  
<body
    <h1 style="color:green;text-align:center;">
        GeeksforGeeks
    </h1
          
    <div class="container">        
        <div class="alert alert-success alert-dismissible fade show">
            <strong>Success alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-info alert-dismissible fade show">
            <strong>Info alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-warning alert-dismissible fade show">
            <strong>Warning alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-danger alert-dismissible fade show">
            <strong>Danger alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-primary alert-dismissible fade show">
            <strong>primary alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-secondary alert-dismissible fade show">
            <strong>Secondary alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-light alert-dismissible fade show">
            <strong>Light alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
          
        <div class="alert alert-dark alert-dismissible fade show">
            <strong>Dark alert!</strong>
            <button type="button" class="close" data-dismiss="alert">
                  ×
            </button>
        </div>
    </div>
</body
  
</html>                    

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :