Animated Modal Window Always stay centered vertically with CSS












0















I followed this w3schools tutorial on animated Modal Windows. Now I have a Modal Window that slides in from the top and contains an Image. When I scale the window to be wider than it is tall, the image scales along with it and the bottom of the Modal Window falls off the bottom of the screen. Is there a way to, first, animate the window so it always stops at the center of the screen no matter the viewport height; second, lock the bottom of the Modal at a certain height from the bottom of the viewport?



Here's my HTML (JavaScript fills .container with an image):



<div id="modal" class="modal">
<div class="modal-content">

<div id="modal-header" class="modal-header">
<span class="close">&times;</span>
<h2>Modal Header </h2>
<h3> sub </h3>
</div>

<div class="modal-body">
<div class="container">

</div>
</div>
</div>




and here's my CSS:



/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 15vh; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
max-height: 100%;
overflow: hidden;
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.65); /* Black w/ opacity */
}

/* Modal Content */
.modal-content {
position: relative;
background-color: #262626;
margin: 0 auto;
border: 1px solid #888;
width: 95%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}

.modal-header {
font-size: 10px;
text-overflow: scale;
padding: 2px 16px;
background-color: black;
color: white;
}

@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}

@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}

.modal-body {
padding: 1px 6px;
}

.container img {
width: 100%;
height: auto;
}









share|improve this question



























    0















    I followed this w3schools tutorial on animated Modal Windows. Now I have a Modal Window that slides in from the top and contains an Image. When I scale the window to be wider than it is tall, the image scales along with it and the bottom of the Modal Window falls off the bottom of the screen. Is there a way to, first, animate the window so it always stops at the center of the screen no matter the viewport height; second, lock the bottom of the Modal at a certain height from the bottom of the viewport?



    Here's my HTML (JavaScript fills .container with an image):



    <div id="modal" class="modal">
    <div class="modal-content">

    <div id="modal-header" class="modal-header">
    <span class="close">&times;</span>
    <h2>Modal Header </h2>
    <h3> sub </h3>
    </div>

    <div class="modal-body">
    <div class="container">

    </div>
    </div>
    </div>




    and here's my CSS:



    /* The Modal (background) */
    .modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 15vh; /* Location of the box */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    max-height: 100%;
    overflow: hidden;
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.65); /* Black w/ opacity */
    }

    /* Modal Content */
    .modal-content {
    position: relative;
    background-color: #262626;
    margin: 0 auto;
    border: 1px solid #888;
    width: 95%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
    }

    .modal-header {
    font-size: 10px;
    text-overflow: scale;
    padding: 2px 16px;
    background-color: black;
    color: white;
    }

    @-webkit-keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
    }

    @keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
    }

    .modal-body {
    padding: 1px 6px;
    }

    .container img {
    width: 100%;
    height: auto;
    }









    share|improve this question

























      0












      0








      0








      I followed this w3schools tutorial on animated Modal Windows. Now I have a Modal Window that slides in from the top and contains an Image. When I scale the window to be wider than it is tall, the image scales along with it and the bottom of the Modal Window falls off the bottom of the screen. Is there a way to, first, animate the window so it always stops at the center of the screen no matter the viewport height; second, lock the bottom of the Modal at a certain height from the bottom of the viewport?



      Here's my HTML (JavaScript fills .container with an image):



      <div id="modal" class="modal">
      <div class="modal-content">

      <div id="modal-header" class="modal-header">
      <span class="close">&times;</span>
      <h2>Modal Header </h2>
      <h3> sub </h3>
      </div>

      <div class="modal-body">
      <div class="container">

      </div>
      </div>
      </div>




      and here's my CSS:



      /* The Modal (background) */
      .modal {
      display: none; /* Hidden by default */
      position: fixed; /* Stay in place */
      z-index: 1; /* Sit on top */
      padding-top: 15vh; /* Location of the box */
      left: 0;
      top: 0;
      width: 100%; /* Full width */
      height: 100%; /* Full height */
      max-height: 100%;
      overflow: hidden;
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0,0.65); /* Black w/ opacity */
      }

      /* Modal Content */
      .modal-content {
      position: relative;
      background-color: #262626;
      margin: 0 auto;
      border: 1px solid #888;
      width: 95%;
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
      -webkit-animation-name: animatetop;
      -webkit-animation-duration: 0.4s;
      animation-name: animatetop;
      animation-duration: 0.4s
      }

      .modal-header {
      font-size: 10px;
      text-overflow: scale;
      padding: 2px 16px;
      background-color: black;
      color: white;
      }

      @-webkit-keyframes animatetop {
      from {top:-300px; opacity:0}
      to {top:0; opacity:1}
      }

      @keyframes animatetop {
      from {top:-300px; opacity:0}
      to {top:0; opacity:1}
      }

      .modal-body {
      padding: 1px 6px;
      }

      .container img {
      width: 100%;
      height: auto;
      }









      share|improve this question














      I followed this w3schools tutorial on animated Modal Windows. Now I have a Modal Window that slides in from the top and contains an Image. When I scale the window to be wider than it is tall, the image scales along with it and the bottom of the Modal Window falls off the bottom of the screen. Is there a way to, first, animate the window so it always stops at the center of the screen no matter the viewport height; second, lock the bottom of the Modal at a certain height from the bottom of the viewport?



      Here's my HTML (JavaScript fills .container with an image):



      <div id="modal" class="modal">
      <div class="modal-content">

      <div id="modal-header" class="modal-header">
      <span class="close">&times;</span>
      <h2>Modal Header </h2>
      <h3> sub </h3>
      </div>

      <div class="modal-body">
      <div class="container">

      </div>
      </div>
      </div>




      and here's my CSS:



      /* The Modal (background) */
      .modal {
      display: none; /* Hidden by default */
      position: fixed; /* Stay in place */
      z-index: 1; /* Sit on top */
      padding-top: 15vh; /* Location of the box */
      left: 0;
      top: 0;
      width: 100%; /* Full width */
      height: 100%; /* Full height */
      max-height: 100%;
      overflow: hidden;
      background-color: rgb(0,0,0); /* Fallback color */
      background-color: rgba(0,0,0,0.65); /* Black w/ opacity */
      }

      /* Modal Content */
      .modal-content {
      position: relative;
      background-color: #262626;
      margin: 0 auto;
      border: 1px solid #888;
      width: 95%;
      box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
      -webkit-animation-name: animatetop;
      -webkit-animation-duration: 0.4s;
      animation-name: animatetop;
      animation-duration: 0.4s
      }

      .modal-header {
      font-size: 10px;
      text-overflow: scale;
      padding: 2px 16px;
      background-color: black;
      color: white;
      }

      @-webkit-keyframes animatetop {
      from {top:-300px; opacity:0}
      to {top:0; opacity:1}
      }

      @keyframes animatetop {
      from {top:-300px; opacity:0}
      to {top:0; opacity:1}
      }

      .modal-body {
      padding: 1px 6px;
      }

      .container img {
      width: 100%;
      height: auto;
      }






      html css






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 0:34









      Yllier123Yllier123

      297




      297
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291484%2fanimated-modal-window-always-stay-centered-vertically-with-css%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291484%2fanimated-modal-window-always-stay-centered-vertically-with-css%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud