Skip to content
Advertisement

Modal Opening Unintentionally On Load

Hi there I am having problems with a modal I have implemented on my site where the modal is open upon loading the site, I wish to have it only open when the correct button is pressed. Additionally it is moving down all other divs below it even though I have the position as absolute and its parent div as relative.

Please see code below

    <header class="col-1" id="myBtn" style="margin-top: 10px; border: .25rem solid #E86E5E; font-size: .9rem; z-index: 0;">Contact</header>
  
    <!-- The Modal -->
    <div id="contact" class="modal">

      <!-- Modal content -->
      <div class="contact-content">
        <span class="close">&times;</span>
        <p class="modal-title">Get in touch.</p>
        <form action="/action_page.php" style="margin-left: 20px;">
          <label for="fname">Full Name</label><br>
          <input type="text" id="fname" name="fname" value=""><br>
          <label for="email">Email</label><br>
          <input type="text" id="email" name="email" value=""><br>
          <label for="email">Company</label><br>
          <input type="text" id="company" name="company" value=""><br>
          <label for="company">Company</label><br>
          <input type="text" id="subject" name="subject" value=""><br><br>
          <input type="submit" value="Submit">
        </form> 
      </div>

    </div>

    <script>
      // Get the modal
      var modal = document.getElementById("contact");

      // Get the button that opens the modal
      var btn = document.getElementById("myBtn");

      // Get the <span> element that closes the modal
      var span = document.getElementsByClassName("close")[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";
        }
      }
    </script>

.contact {
  display: none; /* Hidden by default */
  position: absolute; /* Stay in place */
  z-index: 100; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

.contact-content {
  background-color: #222222;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
  color: #FFFFFF;
  font-weight: bold;
}

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

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

.modal-title {
  font-size: 5vw;
}

Any advice you can offer would be greatly appreciated.

Advertisement

Answer

Your CSS is hiding a non-existent element by class while the element you’re wanting to style is using an id.

You need to change the selector in CSS from .contact to #contact, or you could select .modal since the element has a class titled “modal”.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement