Skip to main content

Creating a modal window with HTML5 & CSS3

Modal boxes are a frequently utilized tool in the web developer’s arsenal. Used for, amongst many things, login/register forms; advertisements; or just notifications to the user. However despite the fact that modal windows frequently contain mission critical information, they are routinely created with JavaScript, which does not sit well with the best-practices of progressive enhancement or graceful degration. This doesn’t need to be a problem, because HTML5 & CSS3 allow us to create modal windows with ease. 

The HTML

The first step to creating our modal box is this short but sweet markup: 
<a href="#openModal">Open Modal</a>

<div id="openModal" class="modalDialog">
 
</div>
As you can see, we just have a simple link that says “Open Modal” and links to our openModal div that is placed right below it. We are doing all of our styling here with classes, so we use the ID just as a hook for opening our modal box, and we will style everything using the modalDialog class. 
Next, we add a div tag that will hold all of our content in the modal box. Inside of this div we are going to have a link to close the box which we will style with our CSS. We will then put a simple heading with a few paragraphs of text beneath it. Your HTML markup should now look like this:
<a href="#openModal">Open Modal</a>

<div id="openModal" class="modalDialog">
 <div>
  <a href="#close" title="Close" class="close">X</a>
  <h2>Modal Box</h2>
  <p>This is a sample modal box that can be created using the powers of CSS3.</p>
  <p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
 </div>
</div>

Starting styling

Right now we just have a link with a div showing beneath it. We will begin styling our box and making it actually functional. Let’s first create our modalDialog classes and start moving forward. 
.modalDialog {
 position: fixed;
 font-family: Arial, Helvetica, sans-serif;
 top: 0;
 right: 0;
 bottom: 0;
 left: 0;
 background: rgba(0,0,0,0.8);
 z-index: 99999;
 opacity:0;
 -webkit-transition: opacity 400ms ease-in;
 -moz-transition: opacity 400ms ease-in;
 transition: opacity 400ms ease-in;
 pointer-events: none;
}
The code here is pretty simple. We style our dialog box by giving it a fixed position, meaning it will move down the page, when open, if you scroll. We also set our top, right, bottom, and left edges to 0 so that our dark background will span across the entire monitor.
Since we are going to want the background around the modal box to go dark when it’s open, we set the background to black, and change the opacity slightly. We also make sure that our modal box is sitting on top of everything by setting our z-index property.
Lastly, we set a nice transition for our modal box to show up on the screen, and hide the box when it’s not clicked on by setting the display to none. 
You may not be completely familiar with what the pointer-events property, but it allows you to control when you do and don’t want elements to be clickable. We set it for our modalDialog class because we don’t want the link to be clickable until the “:target” pseudo class is fired.

Functionality and looks

Now let’s add our :target pseudo class as well as the styling for our modal box.
.modalDialog:target {
 opacity:1;
 pointer-events: auto;
}

.modalDialog > div {
 width: 400px;
 position: relative;
 margin: 10% auto;
 padding: 5px 20px 13px 20px;
 border-radius: 10px;
 background: #fff;
 background: -moz-linear-gradient(#fff, #999);
 background: -webkit-linear-gradient(#fff, #999);
 background: -o-linear-gradient(#fff, #999);
}
With our target pseudo class, we set our display to block, so that when the link is clicked our modal box will display. We also use our pointer-events property so that when the link is hovered over it’s active. 
We then style our div tag by setting the width, position, and using our margins to bump the modal box down from the top, and centering it on our page. We then add a little style by creating some padding, setting a nice border radius, and using a gradient of white to dark gray for our background.

Comments

Popular posts from this blog

Basic naming idea in codeigniter

File Naming Class files must be named in a Ucfirst-like manner, while any other file name (configurations, views, generic scripts, etc.) should be in all lowercase. INCORRECT: somelibrary.php someLibrary.php SOMELIBRARY.php Some_Library.php Application_config.php Application_Config.php applicationConfig.php CORRECT : Somelibrary.php Some_library.php applicationconfig.php application_config.php Furthermore, class file names should match the name of the class itself. For example, if you have a class named  Myclass , then its filename must be Myclass.php. Class and Method Naming Class names should always start with an uppercase letter. Multiple words should be separated with an underscore, and not CamelCased. INCORRECT : class superclass class SuperClass CORRECT : class Super_class class Super_class {           public function __construct ()        ...

Codeigniter php frameworks: codeigniter URLs part 1

codeIgniter Codeigniter php frameworks will teach you about codeigniter URLs. If you not understand about installing codeigniter, please follow the previous post . The codeigniter URLs feature is designed to be search engine and human friendly. For the example is example.com/ news / article / my_article . Firstly is URI Segment, the segment in the URL is following with the Model-View-Controller(MVC). And for the structure  is example.com/ class / function / ID . Class is the first segment represents the controller class that should be invoke. Function is the second segment represents the class function, or method, that should be called. And ID is the third, and any additional segment, represents  the ID and any variables that will be passed to the controller. For the second learn about codeigniter URLs is How to removing the index.php. what is index.php? index.php is the main file to default appear to your URL. The actually codeigniter URL is example.com/ index....

How to setup CodeIgniter in your localhost with WAMP server

I've been developing a couple of website using codeIgniter framework. I only learned about this framework early this year 2011. Basically, I've tried installing codeIgniter from localhost and on the host server itself. How to make codeigniter work is very simple and easy. Below are the steps to kick start your first website using codeigniter. 1. Installing WAMP First and foremost, before you can have your website running on your localhost you should get a copy of WAMP(Windows Apache MySQL and PHP). It is where you can manage your server settings and Apache/MySQL services. So start downloading Wampserver now! As you can see there are 2 buttons to download between 32bits and 64bits that would be basing on your system type. To check what OS type is running click Windows logo on your keyboard + Pause/Break and a window will appear. After download, double click the file and follow the instructions given. Everything will be installed automatically  with the ...