Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Alerts and Errors

Bootstrap Alerts and Errors

Introduction

Bootstrap lets you define styles for success, warning, and error messages on a website or app. In this tutorial, you will learn how to do this.

Create a simple alert message

Using the CSS class "alert", located at lines 2123 to 2175 in bootstrap.css (version 2.0.1), you can create a simple alert message. You can add an optional close icon to it.

When you click the close icon in the alert box, the alert box closes. To achieve this interactive effect, you must add two JavaScript files, jquery.js and alert.js. You can add them before the closing tag of the body element.

Bootstrap example of creating a simple alert message

Example

    

     Basic alert message example

    

    

    

    

        

            

                ×

                Warning! Best check yo self, you're not looking too good.

            

        

    

Note that the code between lines 18 and 21 is required. These are only for demonstration purposes.

Output

basic-alert

View online

View the example above in different browser windows.

Extending the simple alert message

With two more CSS classes, "alert-block" and "alert-heading", you can extend the simple alert message demonstrated earlier. It gives you better control over the text to display, and you can add a text heading before the alert text.

When you click the close icon in the alert box, the alert box closes. To achieve this interactive effect, you must add two JavaScript files, jquery.js and alert.js. You can add them before the closing tag of the body element.

Bootstrap example of extending a simple alert message

Example

    

     Example of extending a simple alert message

    

    

    

    

        

            

                ×

                

Warning!

                What are you doing?! this will delete all files!!

            

        

    

Output

extended-alert

View online

View the example above in different browser windows.

Creating alerts for errors, success, and information

Bootstrap lets you create appropriate alerts when errors or dangers, successes, and information occur. For errors, you need the CSS class "alert-error". For success, you need the "alert-success" class. For information, you need the "alert-info" class. Of course, as explained in the earlier examples, you need the JS files jquery.js and alert.js.

Bootstrap alert examples for error, success, and info

Example

    

     Bootstrap alert examples for error, success, and info

    

    

    

    ×

    Error!This is a fatal error.

    ×

    Success!You have successfully done it.

    ×

    Info!Watch this, but you may forget.

Output

alert-error-success-info

View online

View the example above in different browser windows.

Click here to download all the HTML, CSS, JS, and image files used in this tutorial.