A modal is very widely used in web development as a versatile UI component for displaying content or messages without redirecting users to another page. It appears as a pop-up window that overlays the main content, making it perfect for alerts, forms, image galleries, and more, all while maintaining an interactive and seamless user experience.
Let’s start by creating an HTML file, I named mine index.html
, and put some basic structure to begin with.
We’ll have a <h1>
tag instructing the user to click the button to open the modal, and the button that, when clicked, will open the modal.
Now we’ll add the HTML
for the modal. It will be represented by a div
with an id=modal
and it will contain an header div
for the modal title and close button and the content of the modal.
We will also add some CSS classes and IDs for styling and DOM manipulation with JavaScript.
Let’s add some styles now to finish our basic setup. Create a file called styles.css
and type the following code.
Now that we have our HTML structure, we can start adding some styling and interactivity.
If you try the code that we have so far, the modal is being shown under the <button id="btnModal">Open modal</button>
, as it should, because we are missing the styles to hide it.
Open the styles.css
file and add the following styles.
Here, we style our modal, by hiding it by default and positioning it in the middle of the screen, in front of all other content, and with a box-shadow
to make it more noticeable; we also style the modal components, like the close button and the header.
Now we just need to make the modal show when the button is clicked.
Let’s go ahead and create a JavaScript file called script.js
and write the following code.
Using plain JavaScript, we get our buttons (the button to open the modal, and the button to close it), and then attach onclick
event listeners that call a function when the user clicks on them.
Each function does very similar things, they get the modal and change its CSS display
property; block
to show the modal and none
to hide it.
Everything is pretty much done, but we can still improve it by adding a background overlay that stays in front of the page’s content and behind the modal. This will highlight our modal and make it more noticeable.
Start by opening the index.html
file.
Our changes are just to add an empty <div>
that represents the overlay. Now we need to style it in order for it to actually be an overlay.
Open the styles.css
file and add the following styles.
By adding a fixed position at top: 0
and left: 0
with width
and height
properties with 100%
value, we make the sure that the overlay occupies the whole page, just like we wanted.
Now, we just need to make the overlay show when the modal opens.
Open the script.js
file and add the following code.
We use the same logic, that we used to open the modal, to show the overlay: making the display
property have a block
value when the modal opens and changing it to none
when the modal closes.
After these changes, if you try this code, you’ll see a greyish background when you click the button to open the modal.
And that’s it, we have a functioning modal built with plain HTML, CSS, and JavaScript.
We got to the end of our post; let’s recap everything we’ve done: