Building A Modal In Vanilla JavaScript

1. HTML Structure

First, let’s lay the foundation of our modal masterpiece with some HTML:

  • Button to Open the Modal: This is your gateway to the modal! Imagine it as the magical key that unlocks the hidden treasure—your modal window. The button is labeled “Open Modal” and will be used to trigger the appearance of our modal.
  • The Modal: This is the treasure chest itself! Initially, it’s hidden away from view. Inside this chest, we have a special area that will pop up on your screen when you click the magic button.
    • Modal Content: Here’s where the real magic happens. Inside this space, we’ll place the content you want to display. Think of it as the jewels and scrolls in your treasure chest.
    • Close Button: The enchanted “X” icon that lets you close the modal and return to your regular view. It’s like the spell that seals the chest when you’re done admiring its contents.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JavaScript Modal Example</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>

<!-- Button to Open the Modal -->
<button id="openModalBtn">Open Modal</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal Content -->
  <div class="modal-content">
    <span class="close-btn">×</span>
    <h2>Modal Header</h2>
    <p>This is a simple modal window.</p>
  </div>

</div>

<script src="script.js"></script>
</body>
</html>

2. CSS Styles

Next, let’s add some style and pizzazz to our modal with CSS. Save this in a file named styles.css.

  • .modal: This is the background enchantment that dims everything else on the page when your modal is open. It’s like the magical fog that appears around your treasure chest to make sure all attention is on it.
  • .modal-content: This is the luxurious interior of your modal. We give it a cozy white background, some padding, and center it on the screen. It’s where all your precious content gets displayed in style.
  • .close-btn: This little icon is more than just a simple “X.” It’s designed to be eye-catching with its size and color changes on hover, making sure users know it’s there to help them close the modal whenever they wish.
CSS
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgba(0, 0, 0, 0.4); /* Black background with opacity */
}

/* Modal Content */
.modal-content {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* Could be more or less, depending on screen size */
}

/* The Close Button */
.close-btn {
    color: #aaa;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close-btn:hover,
.close-btn:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

3. JavaScript Functionality

Finally, let’s add some interactive magic with JavaScript. Save this in a file named script.js.

  • Opening the Modal: When you click the button, JavaScript performs a spell to reveal the modal. It changes the modal’s display property from hidden to visible, making your treasure chest appear on the screen.
  • Closing the Modal: The close button also has a magical function. When clicked, it performs a spell to hide the modal again, putting the treasure chest back into its hidden state.
  • Clicking Outside the Modal: If you click outside the modal, JavaScript detects this and also performs a spell to close the modal. It’s like an extra layer of enchantment ensuring the treasure chest closes if you accidentally click outside it.
JavaScript
// Get the modal
const modal = document.getElementById('myModal');

// Get the button that opens the modal
const btn = document.getElementById('openModalBtn');

// Get the <span> element that closes the modal
const span = document.getElementsByClassName('close-btn')[0];

// When the user clicks the button, open the modal
btn.onclick = function() {
    modal.style.display = 'block';
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = 'none';
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = 'none';
    }
}

That’s it! By following these steps, you’ll have a fully functional and stylish modal that enhances your website’s interactivity and user experience!

Address

1523 Kansas Ave
White Oak, PA 15131

Phone

(724) 664-5380