The simplest modal window in HTML and CSS. Creating a Modal with HTML5 and CSS3

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

Which will go to the disable function or just 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, like statistics or an informer.

But the fact is, if you have a dark style of the resource, then in the style you can quickly change the gamma, or rather, fit it to the original design. Here's one of the standard methods for putting pure CSS on a modal that will fire when you click on a button below a link with an HTML anchor. The button itself goes more for visibility, where in the styles, removing one class and the name will remain, which can be put both in the navigation or in the control panel, where the main functionality or site navigation is located.

This is when checking that everything works fine:

Let's start the installation:

Window with button



ZorNet.Ru - webmaster's portal×


This is where the content for the site will be located.


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;
}


It's also worth knowing that CSS styling and pseudo-classing is one of those not fully exploited features of CSS with many interesting potential applications.

It fires when the page's URL matches its element's ID, or put another way, it's when the user jumps to a specific element on the page.

Hello everyone! In this short tutorial, we'll create a simple yet powerful pure CSS modal window. And at the same time we will repeat (and for whom we will open) such a useful thing as flexbox. At the same time, we will create not just a modal window that opens on click, but which is positioned exactly in the center of the screen. Once upon a time, this could only be done using javascript, but time goes by and now this can be done using literally 4 lines of code.

Open modal window

All this modal window consists of two layers, as it were - the first layer, which has the class ModalWindow, darkens all the space around the modal window and will align the content of the window to the center of the screen. The second layer is the class Modal_Body- contains the content of the modal window itself.

Now let's create the CSS for this markup:

Modal ( position: fixed; display: none; top: 0; right: 0; bottom: 0; left: 0; z-index: 0; background: rgba(0,0,0,0.7); pointer-events: none ; ) .Modal:target ( display: flex; pointer-events: auto; ) .Modal_Body ( position: relative; z-index: 2; display: block; margin: auto; padding: 15px; background: #FFF; ) . ModalFull ( position: absolute; display: block; z-index: 0; width: 100%; height: 100%; )

Now let's look at the modal window and understand how it works.

As we can see, when you click on “Open modal window”, the entire window is shaded, and a white modal window appears exactly in the center. Let's stop there for now and devote ourselves to theory.

Since we agreed not to use javascript and can't track clicks on elements with it, we can easily do this with a css pseudo-class and an anchor link with a hash (to point to an element on a given page) and id c with a value that must be equal to the pointer in the reference. Look at our example: href links and id the main container of the modal window have the same meaning − ModalWindow. This is important because the browser needs to understand which elements will interact with each other.

In our case, the general container of the modal window is hidden and, accordingly, all the content of the modal window is hidden. But when the link is clicked, the element gets a pseudo class :target and appears accordingly. Look in css code - property display changes from none on the flex. Note that it is flex, because with it we can align Modal_Body right in the center of the screen. All other styles we registered for him immediately.

By the way, if you don’t quite understand how it was so flattened over the entire surface of the screen, I’ll tell you - it’s all about the following 4 lines:

top: 0; right: 0; bottom: 0; left: 0;

We indicated that it should somehow be in the zero pixel on the right, left, top and bottom at the same time. Instead, you can use, for example, the following construct:

Width: 100%; height: 100vh;

Here we specify the width equal to 100% of the screen, but the height is better set with viewport height- the height of the browser window. I'll stick with my version.

Another important nuance is the value of the z-index property of Modal and Modal_Body. They must be mandatory and Modal_Body it must be greater in value by at least one unit, otherwise the content of the modal window will not be available - links and buttons will be impossible to click. And if there is scrolling content, then this will not work either, since one element will overlap the other.

Let's continue to create our masterpiece. When clicked, a modal window appears, but we can't close it just as easily. Let's add a button to close it:

In fact, she cancels :target for our modal window, and then simply takes the initial position - it is hidden out of sight. But there is some subtlety with this link - when you click it, the browser will try to find such an element, but will fail, and scroll the page to the very beginning. This behavior is one of the minor disadvantages of this approach to making modal windows, but it can be dealt with.

For this attribute href at the link we change from «#» on the "#ModalWindowClose", and for the link-button that opened the window, add the attribute id with the same value. You can also use the attribute name, but in HTML5 anchor definition is better and requires attribute id.

Open modal window

Now when you click the browser will roll back to the button. For the sake of truth, I want to say that this anchor will be located at the top edge of the screen. But, if this button to open is in the header or footer, then this problem is leveled. And if the header has a fixed position, it will be generally great - for example, for ordering a callback or pre-order / consultation, it will work fine even when the window is closed.

Here is an example of what we got:

You can modify the container a little more Modal_Body- these are size restrictions (so that it is not distributed in height and width). And one more small nuance - I recommend placing the code with the modal window, if possible, before the closing tag

.

This is how you can quickly make a modal window. This code, which we wrote, can be used in the basic version, adding the necessary styles as needed.

I hope my lesson was useful for you. Good day to you and see you again on the Web Islands!

A dialog box is a great way to display a short message or information. The modal window is a very popular web design element these days. If you add some special content to the page, then it is better to put it in a modal window:

Previously, it was created using JavaScript, which on this moment not considered best practice. But HTML5 and CSS3 make it easy to create modal windows. In this tutorial, we will be using CSS3 transitions, opacity, pointer-event and background color.

This modal window will be designed in pure CSS. It will be responsive and should work fine on smartphones and tablets. And also on large screens with high resolution.

Creating CSS Modal Window

First of all, we need to create a simple HTML code:

open modal!

We have a simple link that offers to open a modal window and hashtag modal-one . We'll be styling all of this right here with classes, so the id is used as a hook to create the modal.

Keep in mind that we'll only be using CSS and not a jQuery modal, so we'll use the pseudo selector “ :before target”.

Next, you need to save all the contents of the modal window. Inside the div, we will place a hyperlink. It closes the container that we display with using CSS. You can then place a heading with a few paragraphs of text below it. Our HTML markup will now look like this:

Set styles

Now we have a hyperlink wrapped in a div . You can start styling the container to make it more practical. First, let's create a modal CSS class for the modal window. We use the :before pseudo-element for it:

Modal:before ( content: ""; display: none; background: rgba(0, 0, 0, 0.6); position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 10; ) .modal:target:before ( display: block; )

We create a modal window, giving it a fixed position. It will move down as the user scrolls the page.

Additionally, we set the top, right, bottom, and left edges to zero so that the dimmed background covers the entire screen. Now we need to set the background, border, radius for .modal-dialog , as well as a fixed position. Our CSS code will look like this:

Modal-dialog ( background: #fefefe; border: #333 solid 1px; border-radius: 5px; margin-left: -200px; position: fixed; left: 50%; top: -100%; z-index: 11; width: 360px; -webkit-transform: translate(0, -500%); -ms-transform: translate(0, -500%); transform: translate(0, -500%); -webkit-transition: -webkit -transform 0.3s ease-out; -moz-transition: -moz-transform 0.3s ease-out; -o-transition: -o-transform 0.3s ease-out; transition: transform 0.3s ease-out; )

Modal:target .modal-dialog ( -webkit-transform: translate(0, 0); -ms-transform: translate(0, 0); transform: translate(0, 0); top: 20%; )

Here are a few more styles to use to make the modal look nice:

body ( color: #333; font-family: "Helvetica", arial; height: 80em; ) .wrap ( padding: 40px; text-align: center; ) hr ( clear: both; margin-top: 40px; margin- bottom: 40px; border: 0; border-top: 1px solid #aaa; ) h1 ( font-size: 30px; margin-bottom: 40px; ) p ( margin-bottom: 20px; ) .modal-body ( padding: 20px ; ) .modal-header, .modal-footer ( padding: 10px 20px; ) .modal-header ( border-bottom: #eee solid 1px; ) .modal-header h2 ( font-size: 20px; ) .modal-footer ( border-top: #eee solid 1px; text-align: right; )

Now that we've styled the HTML modal and made it functional, all we need to do is create the bottom right button. CSS code :

Btn ( background: #428bca; border: #357ebd solid 1px; border-radius: 3px; color: #fff; display: inline-block; font-size: 14px; padding: 8px 15px; text-decoration: none; text- align: center; min-width: 60px; position: relative; transition: color .1s ease; /* top: 40em;*/ ) .btn:hover ( background: #357ebd; ) .btn.btn-big ( font- size: 18px; padding: 15px 20px; min-width: 100px; ) .btn-close ( color: #aaa; font-size: 30px; text-decoration: none; position: absolute; right: 5px; top: 0; ) .btn-close:hover ( color: #919191; ) /*add to stop scrolling up*/ #close ( display: none; )

What are the advantages of the created modal window?

The main advantage of our modal window is that it is created using only HTML5 and CSS3. Why is it so important? Javascript Modal Window is something that even a beginner can create. There are many ready-made solutions that can be downloaded on the Internet. So why do we want to do just HTML5 and CSS3 without JavaScript?

Well, for example, so that users without JavaScript support can use a modal window. Statistics show that more than 2% of users worldwide do not use JavaScript.

We've effectively used a few lines of CSS code to create the animation. If you apply any JavaScript library animations, you'll be stunned by how much less code we've used. This provides another benefit - cleaner code.

We've implemented a div that contains all of the animation, and it's just a few lines of code. Which makes the code cleaner and this solution allows you to change or move that div as you like without having to worry about code changes in JavaScript.

And finally, HTML5 and CSS3 are the future. We all try to use them in our projects. Thanks to them, you get cleaner code, no need to worry about JavaScript libraries. HTML5 and CSS3 are here to stay, so there's no reason not to use them.

Conclusion

Now you can create a simple modal with HTML5 and CSS3 that can be used for a login or registration form, ad units, and more. In addition, you learned why you should use HTML5 and CSS3 instead of JavaScript, and saw several examples of how websites use modals.

Translation of the article " How To Make a CSS Modal Window without Javascript» was prepared by a friendly project team.

In this tutorial, we will learn how to create a modal window using HTML5 and CSS3. We won't be using JS in this tutorial, just full CSS3. Modal windows can be used for a form feedback, for photos and videos and there are many other uses for it.

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 on which the window will open, and make the frame of our window. To do this, we write the following code:

Step 2. Content of the modal window

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

and put in code instead of ellipsis. You also need to insert a link that will close our window and give it class="close". This is how your code should look like:

REDSTAR PROJECT

REDSTAR- a project dedicated to the issues that you have been interested in for so long. This project contains free lessons and articles on various topics. The topics are very diverse, ranging from simple Windows installation and ending with website development.

Step 3 Styles

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

Step 4. Functionality and viewing

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

div ; background: #fff; background: -moz-linear-gradient(#fff, #b8ecfb); background: -webkit-linear-gradient(#fff, #b8ecfb); background: -o-linear-gradient(#fff, #b8ecfb); )

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

Now we need to add styles to our class. close.

Step 5. Making the close button

In this step we will improve appearance our button that 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; 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: #860015;)

Well, now our button looks like it was intended. Yours should look like this:

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

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

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

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

HTML5 and CSS3 make it incredibly easy to create modals.

HTML markup

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

Open modal window

Inside a div element the content of the modal window is placed. You also need to organize a link to close the window. For example, let's place 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 that can be created using CSS3.

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

Styles

Create a class modalDialog and start shaping the look:

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 when open window. Also, our black background will expand to fill the 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 transitions to display 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 a mouse click when the pseudo class is active “:target”.

Now 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 display mode of the element, so our modal window 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. We 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 position of the text. Then we position the button, make it round using the rounded border property, and form a light shadow. When you hover the mouse cursor over the button, we will change the background color.

Liked the article? Share with friends: