Menu
Is free
registration
home  /  Navigators/ Modal window with css form. How to create a modal window in HTML5 and CSS3

Modal window with css form. How to create a modal window in HTML5 and CSS3

In this lesson, I will not reveal a secret for seasoned layout designers and css gurus, but this article will be useful for beginners. this is where you can learn how to create pop-ups on top of your entire site.

Most often, such windows appear after performing certain actions on the site, for example, a user clicks on the "Request a call back" link and an order form pops up in front of him.

It is very convenient to use PopUp windows in conjunction with ajax, but this is a topic for another lesson.

More and more web resources are starting to appear on the web that use PopUp pop-ups. Everyone knows as an example. social networks... All unnecessary data from screenshots has been removed.

In contact with
Facebook

Twitter

I think there are enough reasons to start studying the question: how to make a PopUp pop-up on your site.

Problem statement (TOR)

You need to create a pop-up window with a blackout screen on top of the entire site.

Solution

Method 1
html
Sample Text
Text in Popup
css
* (font-family: Areal;) .b-container (width: 200px; height: 150px; background-color: #ccc; margin: 0px auto; padding: 10px; font-size: 30px; color: #fff;) .b-popup (width: 100%; height: 2000px; background-color: rgba (0,0,0,0.5); overflow: hidden; position: fixed; top: 0px;) .b-popup .b-popup -content (margin: 40px auto 0px auto; width: 100px; height: 40px; padding: 10px; background-color: # c5c5c5; border-radius: 5px; box-shadow: 0px 0px 10px # 000;)
Result:

Very often it is suggested to use:

Position: absolute;
Yes, the result is the same, but due to the fact that we have set the height of the "shading" block, scroll bars appear. That is why this method is not suitable.

Method 2
This method does not differ dramatically from Method 1, but I find it more convenient.
Html (unchanged)
Sample Text
Text in Popup
Css
* (font-family: Areal;) .b-container (width: 200px; height: 150px; background-color: #ccc; margin: 0px auto; padding: 10px; font-size: 30px; color: #fff;) .b-popup (width: 100%; min-height: 100%; background-color: rgba (0,0,0,0.5); overflow: hidden; position: fixed; top: 0px;) .b-popup. b-popup-content (margin: 40px auto 0px auto; width: 100px; height: 40px; padding: 10px; background-color: # c5c5c5; border-radius: 5px; box-shadow: 0px 0px 10px # 000;)
The result is similar
Thanks to the property: min-height: 100%; our block "blackout" has acquired a width of 100% and minimum height in 100% of the screen.

The only drawback this method is that Internet Explorer supports this property only since version 8.0.

Adding magic to Jquery

Now let's add links to hide / show our popup.

To do this, you need to connect the JQuery library and a small script:


Also the Html needs to be updated:

Sample Text Show popup
Text in Popup Hide popup

Result
Now, when the page loads, the PopUp will disappear.

You can see the result here.

Hello everyone, I'll tell you about one very convenient thing called modal window, we will figure out how to make a modal window? Many do not understand what it is, I will try to explain it in a language you understand, in other words - this is a pop-up window when you click on a button or text.

Inside this window, there can be any information (video, picture, code, etc.) I have now started to use the modal window more, as there is some material that needs to be admitted so that it is not displayed on the page, but let's say there was a picture, when you click on it, a window pops up and information.

I recently did this on one site, inserted a picture on the main page, not even a picture but a screenshot from a YouTube video, and when I clicked, a window pops up where it shows the video. An example of such a window can be seen on my page with the game, the principle is the same, when you click on the picture, a window with the game pops up. I did this because the expansion of the game is wider than my content column, so I found such a way out of the situation.

Consider a modal window in css, so as not to fool our heads, we will use styles, that is, it writes styles in the main file and the code in our window, we will consider everything in detail, what and how.

Modal window in css, how to make a modal window

The first thing we need is styles, copy the entire code with styles, which is below and paste it into your main style file, that is, in style.css

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ... Window (position: fixed; font-family: Arial, Helvetica, sans-serif; top: 0; right: 0; bottom: 0; left: 0; background: rgba (0, 0, 0, 0.7); z- index : 99999; - webkit-transition: opacity 400ms ease-in; - moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; display: none; pointer-events: none;). Window: target (display: block; pointer-events: auto;). Window> div (width: 460px; position: relative; margin: 10% auto; padding: 30px 10px 10px; border-radius: 10px; background: #fff; box-shadow: 0px 0px 20px 2px;). close (background: # cc3300; color: #FFFFFF; line- height: 25px; position: absolute; right: - 12px; text- align: center; top: - 10px; width: 24px; text- decoration: none; font- weight: bold; - webkit- border- radius: 12px; - moz- border- radius: 12px; border- radius: 12px; - moz- box- shadow: 1px 1px 3px # 000; - webkit- box- shadow: 1px 1px 3px # 000; box-shadow: 1px 1px 3px # 000;). close: hover (background: # 990000;)

Window (position: fixed; font-family: Arial, Helvetica, sans-serif; top: 0; right: 0; bottom: 0; left: 0; background: rgba (0,0,0,0.7); z-index : 99999; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; display: none; pointer-events: none;). Window: target (display : block; pointer-events: auto;). Window> div (width: 460px; position: relative; margin: 10% auto; padding: 30px 10px 10px; border-radius: 10px; background: #fff; box-shadow: 0px 0px 20px 2px;) .close (background: # cc3300; color: #FFFFFF; line-height: 25px; position: absolute; right: -12px; text-align: center; top: -10px; width: 24px; text -decoration: none; font-weight: bold; -webkit-border-radius: 12px; -moz-border-radius: 12px; border-radius: 12px; -moz-box-shadow: 1px 1px 3px # 000; -webkit -box-shadow: 1px 1px 3px # 000; box-shadow: 1px 1px 3px # 000;) .close: hover (background: # 990000;)

Save, you can play with the styles, and make something more beautiful, change the background, make another frame, and so on. Go to the page where you want this window to pop up, and paste the following code:

1 2 3 4 5 6 7 < a href= "#ModalOpen" title= "" >Button < div id= "ModalOpen" class = "Window" > < div> < a href= "#close" title= "Close" class = "close" >XThe contents of the window will be here

Button

X The contents of the window will be here

Now let's analyze a little what is in it, you need to change it. Where is it spelled out "Button" there you can insert, like a picture and text, whatever you want, that is, when you click on it, a window will open. Where it is written "The contents of the window will be here" insert what you would like to see in the pop-up window, that's basically all.

NEWS!

Well, on the follow-up news, as always, I have become frequent with the news already)) I made myself a small page with affiliate program, in the form, I will powder everyone to go to it and see, maybe someone will give some more interesting advice, for example, to add something, well, you know what I'm talking about. There are still some thoughts on her, to add a video with me, maybe an instruction, well, something like that, In general, I'm waiting for a comment from you!

Once again, I turn to the topic of creating modal (pop-up) windows. Recently, I am more and more interested in various techniques for executing pop-ups, without using javascript, jQuery plugins etc. Of greater interest is the possibility of applying clean styles and the inexhaustible potential of new CSS3 features.

Based on the developments of some respected bourgeoisie, they are cunning guys in this regard, good results are obtained in terms of creating modal windows using CSS3. The task, first of all, is to achieve more or less stable cross-browser compatibility of the final result, and of course, with the least loss, to reduce the total amount of code, both in HTML and in CSS, in order to make life easier for long-suffering website builders.
As a result, I have in this regard today, we will see, and at the same time, and learn how to make pop-up modal windows using the "magic" of CSS3.

To begin with, all those who are interested in this topic can take a look at an example of how modal windows work in various versions and download the sources:

You should not take this lesson as a guide to action, since at this stage it was possible to achieve confident support only in modern browsers ( IE 9+, Firefox, Safari, Opera, Chrome). With an eye on the fact that the ancient versions of the IE browser are still quite popular among users, I recommend considering this article as some kind of experiment, a demonstration of the capabilities of CSS3.

Ok, now let's go directly to the layout itself. html code and styling our modal window using css3)))

Step 1. HTML

First, let's create some basic HTML markup. As you can see, the basic construction is quite simple if we consider the html markup of modals and buttons (links) to activate them. Each modal window is nothing more than a standard container

, with a certain content inside and a "Close" button, generated exclusively by means of css.

Open window 1 Open window 2 Video in window 3 Photo in window 4

In the above code example, for clarity, I wrote brief explanations in the containers of modal windows. It remains for you by analogy, in div container pop-up window, place any of your content, be it simple text, pictures or videos from any third-party resource, YouTube, Vimeo, etc. Links to call modal windows, you can make text, or you can design them in the form of wonderful, gradient buttons, as in the example.

Step 2. CSS

The next step, this is the most interesting, it is important to prepare all the necessary styles for our modal window, arrange appearance and add functionality. I have omitted the basic page styles so as not to confuse, and have added some comments with comments for clarity:

/ * Basic blackout and modal layer styles * / .overlay (top: 0; right: 0; bottom: 0; left: 0; z-index: 1; visibility: hidden; / * blackout background * / background-color: rgba (0, 0, 0, 0.7); opacity: 0; position: fixed; / * fixed positioning * / cursor: default; / * cursor type * / -webkit-transition: opacity .5s; -moz-transition: opacity .5s; -ms-transition: opacity .5s; -o-transition: opacity .5s; transition: opacity .5s;) .overlay: target (visibility: visible; opacity: 1;) .is-image (top: 0 ; right: 0; bottom: 0; left: 0; display: block; margin: auto; width: 100%; height: auto; / * rounding the corners of inline images * / -webkit-border-radius: 4px; -moz- border-radius: 4px; -ms-border-radius: 4px; border-radius: 4px;) / * embedded m-media elements, frames * / embed, iframe (top: 0; right: 0; bottom: 0; left : 0; display: block; margin: auto; min-width: 320px; max-width: 600px; width: 100%;) .popup h1 (/ * heading 1 * / color: # 008000; text-align: left; text-shadow: 0 1p x 3px rgba (0,0,0, .3); font: 24px "Trebuchet MS", Helvetica, sans-serif; font-weight: bold; ) .popup h2 (/ * heading 2 * / color: # 008000; text-align: left; text-shadow: 0 1px 3px rgba (0,0,0, .3); font: 22px "Trebuchet MS", Helvetica , sans-serif;) / *** Form modal window styles *** / .popup (top: 0; right: 0; left: 0; font-size: 14px; z-index: 10; display: block; visibility : hidden; margin: 0 auto; width: 90%; min-width: 320px; max-width: 600px; / * fixed positioning, the window is stable when scrolling * / position: fixed; padding: 15px; border: 1px solid # 383838 ; / * rounding corners * / -webkit-border-radius: 4px; -moz-border-radius: 4px; -ms-border-radius: 4px; border-radius: 4px; background-color: #FFFFFF; / * outer box-shadow * / -webkit-box-shadow: 0 0 6px rgba (0, 0, 0, 0.8); -moz-box-shadow: 0 0 6px rgba (0, 0, 0, 0.8); -ms-box -shadow: 0 0 6px rgba (0, 0, 0, 0.8); -o-box-shadow: 0 0 6px rgba (0, 0, 0, 0.8); box-shadow: 0px 0px 6px rgba (0, 0 , 0, 0.8); / * full transparency of the window, appearing on click * / opacity: 0; / * transition effect (appearing) * / -webkit-transiti on: all ease .5s; -moz-transition: all ease .5s; -ms-transition: all ease .5s; -o-transition: all ease .5s; transition: all ease .5s; ) / * enable the window to appear and darken the background * / .overlay: target + .popup (top: 20%; / * position of the window from the top of the page when it appears * / visibility: visible; opacity: 1; / * remove transparency * /) / * form the close button * / .close (position: absolute; top: -10px; right: -10px; padding: 0; width: 20px; height: 20px; border: 2px solid #ccc; -webkit-border-radius: 15px ; -moz-border-radius: 15px; -ms-border-radius: 15px; -o-border-radius: 15px; border-radius: 15px; background-color: rgba (61, 61, 61, 0. eight); -webkit-box-shadow: 0px 0px 10px # 000; -moz-box-shadow: 0px 0px 10px # 000; box-shadow: 0px 0px 10px # 000; text-align: center; text-decoration: none; font-weight: bold; line-height: 20px; / * set values ​​and transition effect on hover * / -webkit-transition: all ease .8s; -moz-transition: all ease .8s; -ms-transition: all ease .8s; -o-transition: all ease .8s; transition: all ease .8s; ) .close: before (color: rgba (255, 255, 255, 0.9); content: "X"; text-shadow: 0 -1px rgba (0, 0, 0, 0.9); font-size: 12px;) .close: hover (background-color: rgba (252, 20, 0, 0.8); / * twist the button on hover * / -webkit-transform: rotate (360deg); -moz-transform: rotate (360deg); -ms -transform: rotate (360deg); -o-transform: rotate (360deg); transform: rotate (360deg);) / * optional when adding attachments * / .popup p, .popup div (margin-bottom: 10px;)

As you can see, dear friends, everything is pretty simple and no unnecessary mess. If you do everything correctly, you will get in your arsenal, a wonderful modal pop-up window in which you can place any content, be it text content, photos, various registration forms and feedback, or video from any third-party source. Experiment with the parameters, modify the window as your heart desires, and, if possible, share your best practices, as well as problems that suddenly arise, in the comments.

Modals are a commonly used tool in the webmaster's arsenal. It is very well suited for solving a large number of tasks, such as displaying registration forms, ad units, messages, and so on.

But despite the important place in information support project, modals are usually implemented in JavaScript, which can create problems when expanding functionality or providing backward compatibility.

HTML5 and CSS3 allow you to create modals with extraordinary ease.

HTML markup

The first step towards creating a modal is simple and effective markup:

Open modal window

Inside a div element the content of the modal is placed. You also need to arrange a link to close the window. For example, let's post a simple heading and a few paragraphs. The complete markup for our example would look like this:

Open modal window

X

Modal window

An example of a simple modal window that can be created using CSS3.

It can be used in a wide range, from displaying messages to a registration form.

Styles

Create a class modalDialog and we begin to shape the appearance:

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; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; display: none; pointer-events: none;)

Our window will have a fixed position, that is, it will move down if you scroll the page while the window is open. Also, our black background will expand to fill the entire screen.

The background will have a slight transparency, and will also be positioned on top of all other elements by using the property z-index.

Finally, we set up transitions for displaying our modal window and hide it in an inactive state.

Maybe you don't know the property pointer-events, but it allows you to control how the elements react to the mouse click. We set the value to its value for the class modalDialog, since the link should not respond to mouse clicks when the pseudo class is active “: Target”.

Now we add the pseudo class : target and styles for the modal window.

ModalDialog: target (display: block; 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); )

Pseudo class target changes the display mode of the element, so our modal will be displayed when the link is clicked. We also change the value of the property pointer-events.

We define the width of the modal and the position on the page. Also define a gradient for the background and rounded corners.

Close the window

Now we need to implement the functionality of closing the window. We form the appearance of the button:

Close (background: # 606061; color: #FFFFFF; line-height: 25px; position: absolute; right: -12px; text-align: center; top: -10px; width: 24px; text-decoration: none; font- weight: bold; -webkit-border-radius: 12px; -moz-border-radius: 12px; border-radius: 12px; -moz-box-shadow: 1px 1px 3px # 000; -webkit-box-shadow: 1px 1px 3px # 000; box-shadow: 1px 1px 3px # 000;) .close: hover (background: # 00d9ff;)

For our button, we set the background and text position. Then we position the button, make it round using the rounding property of the frame, and form a subtle shadow. When you hover the mouse cursor over the button, we will change the background color.

Quite often, you can find modals on sites, and they are all used for different purposes. Indeed, it is quite a powerful tool that allows you to make the site interface more interactive and user-friendly. For example, modal windows can be used for various forms, such as an authorization form, a feedback form, ordering a product, and you never know.

In this post, we will look at an example of how to make a modal simple window with using jQuery and CSS. The peculiarity of this example is that it is not required here, well, with the exception of the jQuery library itself.

Place the modal code on the page:

Open modal window

As you can see from the markup, the block of the modal window itself is a div with an id = attribute modal_form which contains a span element with id = modal_close... This element will serve as a button to close the modal window, in addition, below the block there is a div with the id = attribute overlay, which serves at the same time to darken the background. The modal window will open by reference, with the class modal.

CSS for modal window

#modal_form (width: 300px; height: 300px; border-radius: 5px; border: 3px # 000 solid; background: #fff; position: fixed; top: 45%; left: 50%; margin-top: -150px; margin-left: -150px; display: none; opacity: 0; z-index: 5; padding: 20px 10px;) #modal_form #modal_close (width: 21px; height: 21px; position: absolute; top: 10px; right: 10px; cursor: pointer; display: block;) #overlay (z-index: 3; position: fixed; background-color: # 000; opacity: 0.8; -moz-opacity: 0.8; filter: alpha (opacity = 80) ; width: 100%; height: 100%; top: 0; left: 0; cursor: pointer; display: none;)

For modal_form we set a fixed width and height and then centered the position to the center of the screen. For a modal underlay ( overlay) we set the size to the width of the screen, fill with transparency, and also hide it by default. A special moment with z-index, the modal should have the largest of all the elements on the page, and the cover should have the largest of all but the modal itself.

Now to the most basic, this is the javascript code. For the modal window, two main events will be used, this is its opening - clicking on an element with the class modal, in our case this is a link, and closing the modal window is a click on the cover ( overlay), or click on the close button, in our case it is a span element with id = modal_close.

$ (document) .ready (function () ($ (". modal"). click (function (event) (event.preventDefault (); $ ("# overlay"). fadeIn (400, // animate showing the cover function () (// then show the mod. window $ ("# modal_form") .css ("display", "block") .animate ((opacity: 1, top: "50%"), 200);)); )); // close the modal window $ ("# modal_close, #overlay"). click (function () ($ ("# modal_form") .animate ((opacity: 0, top: "45%"), 200, // decrease opacity function () (// after animation $ (this) .css ("display", "none"); // hide the window $ ("# overlay"). fadeOut (400); // hide the tray) );));));

With animate we change the vertical position top, as well as transparency opacity, and with this we get an interesting effect. A similar effect is used both when the window is opened and when it is closed. The difference is that the order of applying properties for blocks is changed, thereby visualizing the opening and closing of the window.