Modal window in pure CSS. Aligning a modal window to the center

Modal windows are a frequently used tool in a webmaster's arsenal. It is very suitable for solving large quantity tasks such as displaying registration forms, advertising units, messages, and so on.

But despite important place V information support project, modal windows are usually implemented in JavaScript, which can create problems when expanding functionality or ensuring backward compatibility.

HTML5 and CSS3 make it incredibly easy to create modal windows.

HTML markup

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

Open modal window

Inside a div element The contents of the modal window are placed. You also need to provide a link to close the window. For example, let's place a simple title and several 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 wide range, starting from the display of messages and ending with the registration form.

Styles

Create a class modalDialog and begin to form 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; display: none;

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

The background will have a slight transparency and will also be placed on top of all other elements through the use of the property z-index.

Finally, we set the 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 elements will react to mouse clicks. 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 a 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 output mode of the element, so our modal window will be displayed when you click on the link. We also change the property value pointer-events.

We define the modal window's width and position on the page. We also define a gradient for the background and rounded corners.

Closing the window

Now we need to implement the window closing functionality. Forming 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; -moz-box-shadow: 1px 3px #000; 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 frame rounding property and form a light shadow. When you hover your mouse over the button, we will change the background color.

My first post.
Centering a block relative to another block is a relatively common problem; this is yet another solution to it. For me, it has become the most universal and covers all cases that I have ever encountered.

Formulation
Center the modal window horizontally and vertically.

Conditions

  • Modal dimensions can be indicated in any units of measurement. Or they may not be specified at all.
  • Responsiveness. When resizing the window, the modal adapts to the current size.
  • If the modal is laid out so that it has min-height/min-width, then when resizing the window to smaller sizes A scroll should appear.
  • IE 9+.
  • Positioning should be implemented in CSS, without using JS.
How was this done before?
If the dimensions of the modal window are specified, then everything is simple:


* ( box-sizing: border-box; ) .fixed-overlay ( position: fixed; overflow: auto; left: 0; top: 0; width: 100%; height: 100%; background-color: rgba(0, 0,0,0.5); width: 200px; height: 150px )
Works great, no complaints. But this method is not suitable for us, because we do not want to depend on the size of the modal window.

I saw the first method that satisfies all the listed requirements from Jabher. This is about using the transform property and its translate value instead of margin. Here's how it works:

Modal ( position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); ) .modal_container ( padding: 20px; background-color: #fff; color: #000; )
Magic! Now we are not dependent on the dimensions.modal_container. This is because translate is based on the size of the element to which the property is applied. Let me remind you that percentage values margin properties will be calculated relative to the size of the parent, which is clearly not suitable for us.

Now about the cons. Using the transform property, we will encounter subpixel rendering. Simply put, the content will begin to blur during resizing, the result will look bad, this is especially noticeable when rendering text and thin lines, such as one-pixel borders. I couldn't find solutions to this problem, if you have them, please share them in the comments.

Tadaam

Not long ago I found a method that is amazingly simple. Inline blocks are in a hurry to help. They can be easily centered horizontally by assigning text-align: center to the parent. After thinking a little, I remembered the wonderful vertical-align property. How does it work? If this property is set to middle, then elements with this property will be centered vertically relative to each other. This means that in addition to the .modal element, there must be someone else in the .fixed-overlay who will help our modal to stand in the center of the window. The height of this someone should be equal to the height of.fixed-overlay. A pseudo-element suggests itself for the role of this helper:


.fixed-overlay__modal ( text-align: center; white-space: nowrap; ) .fixed-overlay__modal::after ( display: inline-block; vertical-align: middle; width: 0; height: 100%; content: " "; ) .modal ( display: inline-block; vertical-align: middle; ) .modal_container ( margin: 50px; padding: 20px; min-width: 200px; text-align: left; white-space: normal; background- color: #fff; color: #000;

In this tutorial we will learn how to create a modal window using HTML5 and CSS3. In this tutorial we will not use JS, only full CSS3. Modal windows can be used for a form feedback, for photos and videos, and there are many more options for its use.

Let's start making our modal window.

Step 1: HTML Markup

First of all, we need to make HTML markup, i.e. make a link that will open a window, and make a frame for our window. To do this we write the following code:

Step 2. Contents of the modal window

Now let's add the contents of our window. Let's make a title and some text and put it all in a tag

and paste it into the code instead of an ellipsis. We also need to insert a link that will close our window and give it class="close". This is what your code should look like:

PROJECT REDSTAR

REDSTAR- a project dedicated to issues that have interested you for so long. On this project posted free lessons and articles on various topics. The topics are very diverse, ranging from simple Windows installations and ending with website development.

Step 3. Styles

Now let's start writing styles for our window. In this step we will make our window invisible. It will only appear when you click on the link. To do this, we write styles for our class modalDialog:

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; display: none;

Step 4. Functionality and viewing

In this step we will make sure that our window is already functioning. To do this, let's add a few more styles for our class modalDialog:

ModalDialog:target ( opacity:1; pointer-events: auto; display: block; ) .modalDialog > div ( width: 400px; position: relative; margin: 10% auto; padding: 5px 20px 13px 20px; border-radius: 10px ; background: #fff; background: -moz-linear-gradient(#fff, #b8ecfb); background: -webkit-linear-gradient(#fff, #b8ecfb); #b8ecfb);

At this step you can already view your window, it is already functioning. But, button close it doesn't look very nice.

Now we need to add styles for our class close.

Step 5. Making a close button

In this step we will improve the appearance of our button, which will close our window. To do this, we write styles for our class close:

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; -moz-box-shadow: 1px 3px #000; 3px #000; box-shadow: 1px 1px 3px #000; .close:hover (background: #860015;)

Well, now our button looks the way it was intended. It should look like this for you:

On this this lesson finished. Thank you for your attention! For your convenience, I have added a demo version and source files. If something is not clear, then download the source files.

The lesson was prepared for you by the site team REDSTAR.

A modal window that is simple in function and is entirely made in pure CSS where you can put it under different functions to call on the site. This is probably one of many that I have come across from a selection of modal windows, in terms of its settings, but also in terms of installation. But the main thing is its functionality, which will not be inferior to others. Also, by default it is made in a light shade, where in the right top corner There is a button installed in the form of a cross.

Which will be used to disable the function or simply to make the frame disappear, where even on this small element there is an effect of changing the color palette. Now the web master can put it on the site and place a description or operators in it, which can display different categories, such as statistics or an informer.

But the thing is, if you have a dark resource style, then you can quickly change the style in style, or rather, adapt it to the original design. Here is one of the standard methods how to make pure CSS on a modal window that will be launched when the button under the link with HTML binding is clicked. The button itself is more for visibility, where in the styles, by removing one class, the name will remain, which can be placed either in the navigation or in the control panel, where the main functionality or site navigation is located.

This is when checking that everything is working fine:

Let's start installation:

Window with a button



ZorNet.Ru - webmaster portal×


Here you will find content on the topic for the site.


CSS

Butksaton-satokavate (
display: inline-block;
text-decoration: none;
margin-right: 7px;
border-radius: 5px;
padding: 7px 9px;
background: #199a36;
color: #fbf7f7 !important;
}

Anelumen (
display: flex;
position: fixed;
left: 0;
top: -100%;
width: 100%;
height: 100%;
align-items: center;
justify-content: center;
opacity: 0;
-webkit-transition: top 0s .7s, opacity .7s 0s;
transition: top 0s .7s, opacity .7s 0s;
}

Anelumen:target (
top: 0;
opacity: 1;
-webkit-transition: none;
transition: none;
}

Anelumen figure (
width: 100%;
max-width: 530px;
position: relative;
padding: 1.8em;
opacity: 0;
background-color: white;
-webkit-transition: opacity .7s;
transition: opacity .7s;
}

Anelumen.lowingnuska figure (
background: #f9f5f5;
border-radius: 7px;
padding-top: 8px;
border: 3px solid #aaabad;
}

Anelumen.lowingnuska figure h2 (
margin-top: 0;
padding-bottom: 3px;
border-bottom: 1px solid #dcd7d7;
}

Anelumen:target figure (
opacity: 1;
}

Anelumen.lowingnuska.compatibg-ukastywise (
text-decoration: none;
position: absolute;
right: 8px;
top: 0px;
font-size: 41px;
}

Anelumen.nedismiseg (
left: 0;
top: 0;
width: 100%;
height: 100%;
position: fixed;
background-color: rgba(10, 10, 10, 0.87);
content: "";
cursor: default;
visibility: hidden;
-webkit-transition: all .7s;
transition: all .7s;
}

Anelumen:target .nedismiseg (
visibility: visible;
}


You also need to know that CSS styling and pseudo-class are one of those that are not fully used by CSS features with many interesting potential applications.

It starts when URL address page corresponds to the identifier of its element, or you can say it differently, this is when the user jumps to specific element On the page.