Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to create a delete confirmation modal with CSS and JavaScript?
A delete confirmation modal is a popup window that asks users to confirm before performing a destructive action like deleting data. This helps prevent accidental deletions and provides a better user experience.
Syntax
.modal {
display: none;
position: fixed;
z-index: 1;
background-color: rgba(0, 0, 0, 0.4);
}
Example
The following example creates a complete delete confirmation modal with CSS styling and JavaScript functionality −
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: Arial, Helvetica, sans-serif;
padding: 20px;
}
.modal {
text-align: center;
display: none;
position: fixed;
z-index: 1;
padding-top: 100px;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.4);
}
.modalContent {
font-size: 20px;
font-weight: bold;
background-color: #fefefe;
margin: auto;
padding: 20px;
border: 1px solid #888;
border-radius: 8px;
width: 400px;
max-width: 80%;
}
.close {
color: rgb(255, 65, 65);
float: right;
font-size: 40px;
font-weight: bold;
}
.close:hover, .close:focus {
color: #ff1010;
cursor: pointer;
}
.modalContent button {
border: none;
border-radius: 4px;
font-size: 18px;
font-weight: bold;
padding: 10px 20px;
margin: 10px 5px;
cursor: pointer;
}
.del {
background-color: rgb(255, 65, 65);
color: white;
}
.del:hover {
background-color: rgb(255, 7, 7);
}
.cancel {
background-color: #ccc;
}
.cancel:hover {
background-color: rgb(167, 167, 167);
}
.openModal {
background-color: #007bff;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>Delete Confirmation Modal Example</h1>
<button class="openModal">Delete Account</button>
<p>Click on the above button to open the confirmation modal</p>
<div class="modal">
<div class="modalContent">
<span class="close">×</span>
<p>Are you sure you want to delete your account?</p>
<p style="color: #666; font-size: 16px;">This action cannot be undone.</p>
<button class="del" onclick="deleteAccount()">Delete Account</button>
<button class="cancel" onclick="hideModal()">Cancel</button>
</div>
</div>
<script>
var modal = document.querySelector(".modal");
var btn = document.querySelector(".openModal");
var span = document.querySelector(".close");
btn.addEventListener("click", () => {
modal.style.display = "block";
});
span.addEventListener("click", () => {
hideModal();
});
function hideModal() {
modal.style.display = "none";
}
function deleteAccount() {
alert("Account deleted successfully!");
hideModal();
}
window.onclick = function(event) {
if (event.target == modal) {
hideModal();
}
};
</script>
</body>
</html>
Output
The above code will produce the following output −
A page with "Delete Account" button appears. When clicked, a modal overlay opens with a confirmation message "Are you sure you want to delete your account?" along with "Delete Account" and "Cancel" buttons. The modal can be closed by clicking the X button, Cancel button, or clicking outside the modal area.
Key Features
The modal includes several important features:
- Overlay background − Semi-transparent background that covers the entire page
- Close options − Users can close via X button, Cancel button, or clicking outside
- Confirmation buttons − Clear Delete and Cancel options with distinct styling
- Warning message − Clear indication that the action cannot be undone
Conclusion
Delete confirmation modals are essential for preventing accidental data loss. This implementation provides a user-friendly interface with multiple ways to close the modal and clear visual feedback for the destructive action.
Advertisements
