I have successfully made a modal yesterday, separately, because I did not want to ruin the rest of my code. How do I add it to the existing one now? Here are the coding files. T
Is this what you are looking for?
If so, then it should be relatively easy. First you need to combine the modal HTML with the rest of your code. It'd probably make sense to add it to the end of the main page, after the . You don't have to add the two tags, and tag though, unless they are different from what is in the main page already.
To make the modal nice looking, you need to place it in a container, which fills the entire page, and has a sort of faded background, like this:
Notice how the background of the modal is greyed out somewhat. This highlights the modal nicely.
To do this, we wrap the div myModal in another div, which I'm going to call modalContainer. We'll apply some styles to it to get that desired background, so we also need to add a CSS class (I'm going to give this class the very clever name of modal). So you have this sort of structure:
... everything before the modal on the main page ...
... modal stuff goes here
... scripts and whatnot after the modal ...
And the CSS for this:
.modal {
position: fixed; /* position it so that fills the screen, but doesn't move with the page (when you scroll, it doesn't move, but the page moves in the background) */
top: 0; /* position this element at the top... */
left: 0; /* ...left corner of the page... */
width: 100%; /* ...and set both the width */
height: 100%; /* ...and height to 100%, so that the element fills the entire screen */
z-index: 99999; /* set the z-index to a high enough number so that this element is positioned on top of all other elements */
background: rgb(0, 0, 0, 0.7); /* set the background to black with some transparency, so you can see through it */
/* The following simply centers the modal within this container */
display: flex;
justify-content: center;
align-items: center;
}
So, modalContainer spans the complete height and width of the screen and is the background for the modal, while myModal houses the actual modal.
Now, to function properly, this requires some changes to modal.js, namely in opening the modal container, instead of just the modal:
//Get the modal
var modal = document.getElementById("myModal");
var modal_container = document.getElementById("modalContainer")
modal_container.style.display = "none";
window.onclick = function (event) {
console.log(event.target)
if(event.target.id == "myBtn") { // If the button is clicked, open the modal container
modal_container.style.display = "flex"
}
else if (modal !== event.target && !modal.contains(event.target)) { // If the click is outside the modal, hide the modal container
modal_container.style.display = "none";
}
}
Now it should work as desired.
Full HTML code:
Bem-vindo à MMT UNIVERSITY
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
deserunt mollit anim id est laborum.
Run and edit this code online
EDIT: (for the problem mentioned in the comments, which has been deleted by SO)
There are a couple of problems with what you have.
Fixing that results in:
...but the modal is stuck at the bottom left:
modal was a dumb idea on my part, because jQuery seems to already have reserved that name and is overriding it with its own styles. I should've seen that. Anyways, renaming it to modal-container and updating the HTML accordingly fixes this problem, but now the modal spans the entire page:container class isn't included for some reason on your style.css. I pasted it over, because it is a required class to structure the modal correctly:overlay-panel. Removing this results in:And all should be good now.
Repl.it link: https://repl.it/@marsnebulasoup/UncommonIntentMolecule-1