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

Your email address will not be published. Required fields are marked *

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

JavaScript timer countdown days hours minutes seconds

JavaScript validation on image field

html5 video tag Complete Guide

Getting The Screen Resolution Using JavaScript

How to create textbox in html dynamically using javascript

how to disable right click using JavaScript

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Free online tool to download Instagram videos, photos and save Instagram videos, photos to your device. It's free & easy to use.

Download Now

CURRENCY CONVERTER

Our real time currency converter calculator will convert your money based on current values from around the world. It's free & easy to use.

Convert Now

ROBOST.TXT GEN.

Create robots txt file that help search engines to crawling and indexing your site with help of our free online Robots.txt Generator tool.

Create Now

HTML MINIFIER

Free online tool to minify or compress html code and reduce the size of html code in few clicks. It's free & easy to use.

Minify Now

CSS MINIFIER

Free online tool to minify or compress css code and reduce the size of css code in few clicks. It's free & easy to use.

Minify Now

JAVASCRIPT MINIFIER

Free online tool to minify or compress javascript code and reduce the size of javascript code in few clicks. It's free & easy to use.

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.