Updated: Pure HTML5/CSS3 Responsive Modal Window Version 2

September 18, 2013 Freebies, Tutorials

I updated the popular HTML5/CSS3 Responsive Modal Window to version 2. The update adds an overlay over the background to focus on the modal and to prevent the user from interacting with the site behind the modal and a new animation when closing the modal.

You can get all of the new code on GitHub, or see the updated code below.

Markup


<div id="modal">
	<div class="modal-content">
		<div class="header">
			<h2>Modal Heading</h2>
		</div>
		<div class="copy">
			<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
		</div>
		<div class="cf footer">
			<a href="#" class="btn">Close</a>
		</div>
	</div>
	<div class="overlay"></div>
</div>

CSS

 
/* Normal styles for the modal */
#modal {
	left:50%;
	margin:-250px 0 0 -40%;
	opacity: 0;
	position:absolute;
	top:-50%;
	visibility: hidden;
	width:80%;
	box-shadow:0 3px 7px rgba(0,0,0,.25);
	box-sizing:border-box;
	transition: all 0.4s ease-in-out;
	-moz-transition: all 0.4s ease-in-out;
	-webkit-transition: all 0.4s ease-in-out;
}
	/* Make the modal appear when targeted */
	#modal:target {
		opacity: 1;
		top:50%;
		visibility: visible;
	}
#modal .header,#modal .footer {
	border-bottom: 1px solid #e7e7e7;
	border-radius: 5px 5px 0 0;
}
	#modal .footer {
		border:none;
		border-top: 1px solid #e7e7e7;
		border-radius: 0 0 5px 5px;
	}
#modal h2 {
	margin:0;
}
#modal .btn {
	float:right;
}
#modal .copy,#modal .header, #modal .footer {
	padding:15px;
}
.modal-content {
	background: #f7f7f7;
	position: relative;
	z-index: 20;
	border-radius:5px;
}
#modal .copy {
	background: #fff;
}

#modal .overlay {
	background-color: #000;
	background: rgba(0,0,0,.5);
	height: 100%;
	left: 0;
	position: fixed;
	top: 0;
	width: 100%;
	z-index: 10;
}

Comments

  • Peter Burgess

    Great Script but I can get it to work on a tablet it just sends you to the top of the page

  • Rami

    How can I implement this on a button click so I can get the data from server and populate and show the model window?

    • cameronbaney

      You would need to add some JavaScript to do this. In the link below, I used jQuery to do this. This is a basic example, but it should be enough to get what you need. In this example, the HTML file that is being loaded would need some of the modal content, which is also in the link below.

      https://gist.github.com/cameronbaney/11218524

  • Christian Flach

    Great piece of work! I love it!

    I’m trying to use it for our band website, but I do have an issue: I’m using the modal for a newsletter form, where the input form is implemented in html/php. Now, when the server issues back the answer after subscribing/unsubscribing, the modal is glued to the top of the page, instead of keeping it’s top-margin – do you have any suggestions for me?

    Regards,
    Christian

  • Samyak Jain

    Hi, Thanks for this great work. Can we open modal inside another modal. Regard, Samyak

  • Marco Ilardi

    good job

  • José Luis Berbesi

    I´m using this in the portfolio section of my website. When I click on any of these link for the first time, I see the correct content, but when I click on other links it shows the same content loaded for the first time, It doesnt update the content. I want it to be updated everytime its clicked., I have Googled a lot but didn’t find any solutions. I hope anyone can help me. Thanks in advance.