Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to change the Background Color after Clicking the Button in JavaScript?
Next article icon

How to Change the Color of the Alert Box in JavaScript ?

Last Updated : 26 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Alert boxes, often utilized in JavaScript applications, provide users with important notifications or messages. However, their default appearance may not always blend seamlessly with the overall design of your website or application.

JavaScript offers several methods to customize alert boxes as listed below.

Table of Content

  • Using display property to create custom alert
  • Using visibility property to create custom alert

Using display property to create custom alert

This method involves crafting a custom alert-like dialog using HTML, CSS, and JavaScript. By constructing the dialog from scratch, developers gain complete control over its appearance, including its color.

Example: The below code example will explain how you can create your custom alert dialog.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport" content= "width=device-width, initial-scale=1.0">     <title>Custom Alert Box</title>     <link rel="stylesheet" href="style.css"> </head>  <body style="text-align: center;">      <h2>         Creating a custom alert box     </h2>     <button class="open-btn" onclick="showAlert()">         Show Alert     </button>      <div id="customAlert" class="custom-alert">         <span class="close-btn" onclick="closeAlert()">             &times;         </span>         <p>This is a custom alert!</p>     </div>      <script>         function showAlert() {             const alertBox =                  document.getElementById('customAlert');             alertBox.style.display = 'block';         }          function closeAlert() {             const alertBox =                  document.getElementById('customAlert');             alertBox.style.display = 'none';         }     </script>  </body>  </html> 
CSS
body {     font-family: Arial, sans-serif; }  .custom-alert {     display: none;     position: fixed;     top: 20%;     left: 50%;     transform: translate(-50%, -20%);     background-color: #4CAF50;     padding: 30px;     border-radius: 5px;     box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); }  .custom-alert p {     margin: 0;     color: #ffffff; }  .open-btn{     color: #fff;     background: green;     border: none;     padding: 10px;     border-radius: 8px;     cursor: pointer; }  .close-btn {     position: absolute;     top: 5px;     right: 5px;     cursor: pointer;     font-size: 20px;     color: #ffffff; } 

Output:

fosiGIF

Using visibility property to create custom alert

We can use the visibility property in the same way as we used the display property to hide and show the alert box on click to the button.

Example: The below code will explain the use of the visibility property to create custom alert.

HTML
<!DOCTYPE html> <html lang="en">  <head>     <meta charset="UTF-8">     <meta name="viewport" content= "width=device-width, initial-scale=1.0">     <title>Advanced Custom Alert Box</title>     <link rel="stylesheet" href="style.css"> </head>  <body style="text-align: center;">     <h2>         Creating a custom alert box     </h2>     <button class="open-btn" onclick="customAlert()">         Show Alert     </button>      <div id="custom-alert" class="custom-alert">         <div class="alert-content">             <span id="alert-message"></span>             <div class="button-container">                 <button id="confirm-btn"                      onclick="confirmAction()">                     Confirm                 </button>                 <button id="cancel-btn"                      onclick="cancelAction()">                     Cancel                 </button>             </div>         </div>     </div>      <script>         function customAlert() {             const message =                  "Are you sure you want to perform this action?";             document.getElementById("alert-message").                 innerText = message;             document.getElementById("custom-alert").                 style.visibility = "visible";             document.body.                 style.backgroundColor = "rgb(49 45 45 / 45%)";         }          function confirmAction() {             document.getElementById("custom-alert").                 style.visibility = "hidden";             document.body.                 style.backgroundColor = "#fff"         }          function cancelAction() {             document.getElementById("custom-alert").                 style.visibility = "hidden";             document.body.                 style.backgroundColor = "#fff"         }     </script> </body>  </html> 
CSS
.custom-alert {     visibility: hidden;     position: fixed;     top: 50%;     left: 50%;     transform: translate(-50%, -50%);     background-color: #e8efe8;     border: 1px solid #77bb77;     padding: 20px;     border-radius: 10px;     box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); }  .alert-content {     text-align: center; }  .open-btn {     color: #fff;     background: green;     border: none;     padding: 10px;     border-radius: 8px;     cursor: pointer; }  .button-container {     margin-top: 15px; }  #confirm-btn, #cancel-btn {     padding: 8px 20px;     background-color: #4CAF50;     border: none;     border-radius: 5px;     cursor: pointer;     font-size: 16px;     margin: 5px 10px;     transition: background-color 0.3s; }  #confirm-btn:hover, #cancel-btn:hover {     background-color: #45a049; }  #confirm-btn:focus, #cancel-btn:focus {     outline: none; }  #confirm-btn:active, #cancel-btn:active {     background-color: #3e8e41; } 

Output:

fosiGIF


Next Article
How to change the Background Color after Clicking the Button in JavaScript?
author
nikunj_sonigara
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to Center the JavaScript Alert Message Box ?
    To center the JavaScript alert message box, you can use CSS to define a custom alert box with fixed positioning and the transform property to adjust its position in the center of the viewport. ApproachFirst, create the layout of the web page using elements. styles alert box using CSS, positioning it
    2 min read
  • How to Center the JavaScript Alert Message Box ?
    An alert box is a dialogue box that pops up on the screen to provide information to the user. It contains a message and an OK button, which allow users to acknowledge the message and close the box. Alert boxes are commonly used in web development to display important messages, warnings, or notificat
    4 min read
  • How to Change the Color of HTML Element in JavaScript?
    Changing the text color of an HTML element in JavaScript improves the user experience. To change the font color of an HTML element using JavaScript, we use the DOM to access and modify its style properties, allowing for dynamic color changes. Syntax:element.style.color = "green";ApproachSelect the T
    2 min read
  • How to change the Background Color after Clicking the Button in JavaScript?
    Changing the background color after clicking a button in JavaScript involves attaching a click event listener to the button. When the button is clicked, it triggers a function that changes the background color of a specific element (like the page or a section of it). This creates a simple and intera
    3 min read
  • How to change the background color of the active nav-item?
    In this article, we will learn how we can change the background of the active nav item with the help of CSS and jQuery. Given an HTML document containing a list of items, the task is to change the background color of a particular list item when it is active or clicked. We can change the background c
    3 min read
  • How to show JavaScript alert box in the middle of the screen?
    To create an alert box that occurs in the middle of the browser window we use CSS to style the alert box and JavaScript to control its appearance. Techniques include adjusting the alert box's position using CSS properties as well as JavaScript event handling to trigger the alert box's display to enh
    2 min read
  • How to Change Button Label in Alert Box using JavaScript ?
    In JavaScript, the alert method is used to display an alert box with a message. By default, the alert box contains an "OK" button. We cannot directly update the default alert box, However, we can customize the button label by creating a custom alert box using HTML, CSS, and JavaScript. ApproachCreat
    2 min read
  • How to Create an Alert in JavaScript ?
    The alert() method in JavaScript displays an alert box with a message and an OK button. It's used when you want information to come through to the user, providing immediate notifications or prompts for user interaction during program execution. Note: Alert boxes interrupt user interaction, shifting
    1 min read
  • How to Edit a JavaScript Alert Box Title ?
    We can't directly modify the title of the alert box because the title is controlled by the browser and cannot be changed by JavaScript. However, we can create a custom alert box. Table of Content Using a Custom Alert Box FunctionUsing SweetAlert LibraryUsing a Custom Alert Box FunctionIn this approa
    2 min read
  • How to use the alert() method in JavaScript ?
    In this article, we will learn how to use the alert() method in JavaScript. The alert() method is used to show an alert box on the browser window with some message or warning. We can use it as a message or as a warning for the user. Approach: To show an alert on the browser window, we make a button.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences