Sunday, February 4, 2018

How to create alert messages with CSS

Error, Success, Warning, and Info Messages with CSS

In this tutorial we are going to learn How to create alert messages with CSS. This alert messages are used to notify the user about current status of their request. Here we have created four different type of alert messages :
  1. Error
  2. Success
  3. Warning
  4. Information 

How To Create an Alert Message Box

Lets see the complete example to display CSS Alert Messages when user click on buttons and hide the Alert Messages when user click on "X" Symbol.

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src=""></script>
        .alert {
            padding: 10px;
            color: white;
            opacity: 1;
            transition: opacity 0.6s;
            margin-bottom: 15px;
            width: 400px;
            display: none;
        .alert.error {
            background-color: #f44336;
        .alert.success {
            background-color: #4CAF50;
        } {
            background-color: #2196F3;
        .alert.warning {
            background-color: #ff9800;
        .closebtn {
            margin-left: 15px;
            color: white;
            font-weight: bold;
            float: right;
            font-size: 22px;
            line-height: 10px;
            cursor: pointer;
            transition: 0.3s;
        .closebtn:hover {
            color: black;
        button {
            border: none;
            cursor: pointer;
            color: white;
            padding: 10px 30px;
            border-radius: 2px;
            font-size: 22px;
            box-shadow: 2px 2px 4px rgba(0, 0, 0, .4);
            background: #5cd38c;
            margin: 3px;

        $(document).ready(function() {

            // on click remove alert message
            $(document).on('click', 'div', function() {

            $(".error").on("click", function() {
            $(".success").on("click", function() {
            $(".info").on("click", function() {
            $(".warning").on("click", function() {



    <h2>Alert Messages</h2>
    <p> when user click on button, it will display alert message </p>
    <p> when user click on X, it will hide alert message </p>
    <div class="alert error">
        <span class="closebtn">×</span>
        <strong>Danger!</strong> It's a Invalid Request.

    <div class="alert success">
        <span class="closebtn">×</span>
        <strong>Success!</strong> Your Request Processed successfully.

    <div class="alert info">
        <span class="closebtn">×</span>
        <strong>Information! We are providing new offers</strong> .

    <div class="alert warning">
        <span class="closebtn">×</span>
        <strong>Warning!</strong> Invalid Request, please contact adminstration.

    <button class="error"> Error </button>
    <button class="success"> Success </button>
    <button class="info"> Information </button>
    <button class="warning"> Warning </button>



This is all about Error, Success, Warning, and Information Alert Messages with CSS. Please free to ask questions in comment box if any ?

No comments:

Post a Comment