Hello developers, today in this blog, you'll learn to create a Popup Notification using HTML, CSS & JavaScript.
A Popup Notification box, sometimes also called a message box. A popup notification is a message that appears on the user's browser or the desktop. These notifications are designed to grab your audience's attention and engage them in some way.
In this blog (Popup Notification) on the webpage, there are four buttons at the center of the webpage. When you click on the button, the notification will pop up from the top of the webpage. Those four buttons were Info, Warning, Error, and Success buttons. These buttons will pop up with their respective information, which gives the notification to the users. There is a cross button to close the notification. During the popup, the notification will hide the buttons. While moving the mouse over the buttons, the buttons will fade out and this is made by using the CSS hover effect.
The source code of this Popup Notification is given below, if you want the source code of this program, you can copy it. You can use this Popup Notification with your creativity and take this project to the next level.
Popup Notification [Source Code]
To make this website, you would like to make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html extension.
@import url("https://fonts.googleapis.com/css?family=Open+Sans|Roboto:400,700&display=swap");
*{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
body{
background: #2d333f;
}
.wrapper{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
.btn_grp{
display: flex;
}
.btn_grp .btn{
width: 225px;
height: 50px;
line-height: 50px;
text-align: center;
background: #fff;
margin: 0 10px;
border-radius: 3px;
cursor: pointer;
text-transform: uppercase;
letter-spacing: 2px;
font-size: 14px;
}
.btn_grp .btn.btn_info,
.alert_item.alert_info{
/*background: #cde8f5;*/
background-color: #4480ae;
color: #fff;
}
.btn_grp .btn.btn_warning,
.alert_item.alert_warning{
/*background: #f8f3d6;*/
background-color: #967132;
color: #fff;
}
.btn_grp .btn.btn_error,
.alert_item.alert_error{
/*background: #ecc8c5;*/
background-color: #b32f2d;
color: #fff;
}
.btn_grp .btn.btn_success,
.alert_item.alert_success{
/*background: #def2d6;*/
background-color: #5a7052;
color: #fff;
}
.btn_grp .btn.btn_info:hover{
/*background: #a5c7d8;*/
background: #cde8f5;
color: #000;
}
.btn_grp .btn.btn_warning:hover{
/*background: #dcd4a2;*/
background: #f8f3d6;
color: #000;
}
.btn_grp .btn.btn_error:hover{
/*background: #c79995;*/
background: #ecc8c5;
color: #000;
}
.btn_grp .btn.btn_success:hover{
/*background: #adc5a5;*/
background: #def2d6;
color: #000;
}
.alert_wrapper{
position: relative;
width: 100%;
height: 100%;
z-index: 999;
visibility: hidden;
}
.alert_backdrop{
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #2d333f;
opacity: 0.9;
z-index: 2;
}
.alert_wrapper .alert_item{
z-index: 3;
position: fixed;
top: -100%;
left: 50%;
transform: translate(-50%,-50%);
display: flex;
align-items: center;
padding: 25px 50px;
border-radius: 3px;
transition: all 0.2s ease;
}
.alert_wrapper .alert_item .data{
margin: 0 50px;
}
.alert_wrapper .alert_item .data span{
font-weight: 900;
letter-spacing: 1px;
}
.alert_wrapper .alert_item .data .sub{
padding-top: 5px;
font-size: 14px;
font-weight: 100;
}
.alert_wrapper .alert_item .icon{
font-size: 32px;
}
.alert_wrapper .alert_item .close{
cursor: pointer;
}
.alert_item.alert_info .close:hover{
color: #a5c7d8;
}
.alert_item.alert_warning .close:hover{
color: #dcd4a2;
}
.alert_item.alert_error .close:hover{
color: #c79995;
}
.alert_item.alert_success .close:hover{
color: #adc5a5;
}
.alert_wrapper.active{
visibility: visible;
}
.alert_wrapper.active .alert_item{
top: 50%;
}
.credit{
text-align: center;
color: #fff;
margin-top: 30px;
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
.credit a{
text-decoration: none;
color: #f8f3d6;
font-weight: bold;
}
var alert_items = document.querySelectorAll(".alert_item");
var btns = document.querySelectorAll(".btn");
var alert_wrapper = document.querySelector(".alert_wrapper");
var close_btns = document.querySelectorAll(".close");
btns.forEach(function(btn, btn_index){
btn.addEventListener("click", function(){
alert_wrapper.classList.add("active");
alert_items.forEach(function(alert_item, alert_index){
if(btn_index == alert_index){
alert_item.style.top = "50%";
}
else{
alert_item.style.top = "-100%";
}
})
})
})
close_btns.forEach(function(close, close_index){
close.addEventListener("click", function(){
alert_wrapper.classList.remove("active");
alert_items.forEach(function(alert_item, alert_index){
alert_item.style.top = "-100%";
})
})
})
Thank you for reading our blog. If you face any problem creating this Popup Notification using HTML, CSS & JavaScript, then contact us or comment us. We'll try to provide a solution to your problem as soon as possible.
Post a Comment
Thank you
Learning robo team