How To Create a Modal Popup With CSS and JavaScript

How To Create a Modal Popup With CSS and JavaScript

In this tutorial we will show you how to create a simple modal with CSS and JavaScript in simple way. We can use modal popups in our website for newsletter signups, displaying notifications/alerts, get a quote and handling register and login forms.

HTML Code For Model Box

Here is html code for popup.
<!-- The Popup Modal -->
<div id="mkpopup" class="mkpopup">
	<!-- Modal content -->
	<div class="mkpopup-content">
		<span class="mkpopupclose" onclick="closmkpopup();">&times;</span>
		<h3>Get in touch</h3>
		<form method="post" name="popupcontact" onsubmit="popupvalidation();" action="#">
			<div class="col-md-12">
				<lable>Name <span class="required">*</span></lable>
				<input type="text" name="username">
			</div>
			<div class="col-md-12">
				<lable>Email <span class="required">*</span></lable>
				<input type="text" name="uemail">
			</div>
			<div class="col-md-12">
				<lable>Phone No. <span class="required">*</span></lable>
				<input type="text" name="uphoneno">
			</div>
			<div class="col-md-12">
				<lable>Message <span class="required">*</span></lable>
				<textarea name="umessage" placeholder="" style="height: 130px;"></textarea>
			</div>
			<div class="col-md-12">
				<input type="submit" name="popupcontact" value="Get in touch now">
			</div>
		</form>
	<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
<input type="button" value="Open Popup" onclick="openmkpopup();">

Css Code

<style>
/*Popup CSS*/
.mkpopup {
	display: none; /* Hidden by default */
	position: fixed; /* Stay in place */
	z-index: 99999999; /* Sit on top */
	padding-top: 100px; /* Location of the box */
	left: 0;
	top: 0;
	width: 100%; /* Full width */
	height: 100%; /* Full height */
	overflow: auto; /* Enable scroll if needed */
	background-color: rgb(0,0,0); /* Fallback color */
	background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.mkpopup-content {
	background-color: #fefefe;
	margin: auto;
	padding: 20px;
	border: 1px solid #888;
	width: 100%;
	max-width:700px;
}
/* The Close Button */
.mkpopupclose {
	color: #aaaaaa;
	float: right;
	font-size: 28px;
	font-weight: bold;
}
.mkpopupclose:hover, .mkpopupclose:focus {
	color: #000;
	text-decoration: none;
	cursor: pointer;
}
input[type="text"], textarea{width:100%; border:1px solid #cccccc; margin-bottom:10px; padding:5px;}
</style>

JavaScript Code

<script>
function openmkpopup(){
	document.getElementById("mkpopup").style.display = "block";
}
function closmkpopup(){
	document.getElementById("mkpopup").style.display = "none";
}
</script>

Full code javascript css model popup box

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>How To Create a Modal Popup With CSS and JavaScript</title>
		<style>
			/*Popup CSS*/
			.mkpopup {
				display: none; /* Hidden by default */
				position: fixed; /* Stay in place */
				z-index: 99999999; /* Sit on top */
				padding-top: 100px; /* Location of the box */
				left: 0;
				top: 0;
				width: 100%; /* Full width */
				height: 100%; /* Full height */
				overflow: auto; /* Enable scroll if needed */
				background-color: rgb(0,0,0); /* Fallback color */
				background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
			}
			/* Modal Content */
			.mkpopup-content {
				background-color: #fefefe;
				margin: auto;
				padding: 20px;
				border: 1px solid #888;
				width: 100%;
				max-width:700px;
			}
			/* The Close Button */
			.mkpopupclose {
				color: #aaaaaa;
				float: right;
				font-size: 28px;
				font-weight: bold;
			}
			.mkpopupclose:hover, .mkpopupclose:focus {
				color: #000;
				text-decoration: none;
				cursor: pointer;
			}
			input[type="text"], textarea{width:100%; border:1px solid #cccccc; margin-bottom:10px; padding:5px;}
		</style>
		<script>
			function openmkpopup(){
				document.getElementById("mkpopup").style.display = "block";
			}
			function closmkpopup(){
				document.getElementById("mkpopup").style.display = "none";
			}
		</script>
	</head>
<body>
<!-- The Popup Modal -->
<div id="mkpopup" class="mkpopup">
	<!-- Modal content -->
	<div class="mkpopup-content">
		<span class="mkpopupclose" onclick="closmkpopup();">&times;</span>
		<h3>Get in touch</h3>
		<form method="post" name="popupcontact" onsubmit="popupvalidation();" action="#">
			<div class="col-md-12">
				<lable>Name <span class="required">*</span></lable>
				<input type="text" name="username">
			</div>
			<div class="col-md-12">
				<lable>Email <span class="required">*</span></lable>
				<input type="text" name="uemail">
			</div>
			<div class="col-md-12">
				<lable>Phone No. <span class="required">*</span></lable>
				<input type="text" name="uphoneno">
			</div>
			<div class="col-md-12">
				<lable>Message <span class="required">*</span></lable>
				<textarea name="umessage" placeholder="" style="height: 130px;"></textarea>
			</div>
			<div class="col-md-12">
				<input type="submit" name="popupcontact" value="Get in touch now">
			</div>
		</form>
	<div class="clearfix"></div>
</div>
<div class="clearfix"></div>
</div>
<input type="button" value="Open Popup" onclick="openmkpopup();">
</body>
</html>

Tags

How To Create a Modal Popup With CSS and JavaScript, How to create a modal popup using JavaScript and CSS, How To Create a Modal Popup Box with CSS and JavaScript, How to create a modal popup using JavaScript, JavaScript Modal Popup Window, jquery modal popup, modal popup using jquery, jquery modal popup example, how to create popup in html with css, javascript popup window onclick example, css modal popup, javascript popup form, bootstrap 4 modal
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.