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

Leave a Reply

Related Posts: You may like

Get Latitude Longitude from Address

cookie policy popup

How to make a sticky sidebar on scroll

How to get the value of HTML tag using JS

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.