简体   繁体   中英

How to make modal box as responsive?

I have implemented this CSS-based modal box. It works perfect, and it is quite easy to use.However, it is not responsive. how can we make its responsive? pls help.

<!DOCTYPE html>
    <html>
    <head>
    <style> 
    /* The Modal (background) */
    .modal4 {
        display: none; 
        position: fixed; 
        z-index: 1; 
        padding-top: 100px; 
        left: 0;
        top: 0;
        width: 100%; 
        height: 100%; 
        overflow: auto; 
        background-color: rgb(0,0,0); 
        background-color: rgba(0,0,0,0.4); 
    }

    /* Modal content1 */
    .modal4-content2 {
        position: relative;
        background-color: #fefefe;
        margin: auto;
        padding: 0;
        border: 1px solid #888;
        width: 80%;
        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
    }

    /* Add Animation */
    @-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}
    }

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

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

    .modal4-header2 {
        padding: 2px 16px;
        background-color: #5cb85c;
        color: white;
    }

    .modal4-body {padding: 2px 16px;}

    .modal4-footer2 {
        padding: 2px 16px;
        background-color: #5cb85c;
        color: white;
    }
    </style>
    </head>
    <body>

    <h2>Animated Modal with header1 and footer1</h2>

    <!-- Trigger/Open The Modal -->
    <button id="myBtn4">Open Modal</button>

    <!-- The Modal -->
    <div id="myModal4" class="modal4">

      <!-- Modal content1 -->
      <div class="modal4-content2">
        <div class="modal4-header2">
          <span class="close">&times;</span>
          <h2>Modal header1</h2>
        </div>
        <div class="modal4-body">
          <p>Some text in the Modal Body</p>
          <p>Some other text...</p>
        </div>
        <div class="modal4-footer2">
          <h3>Modal footer1</h3>
        </div>
      </div>

    </div>

    <script>
    // Get the modal
    var modal4 = document.getElementById('myModal4');

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

    // 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() {
        modal4.style.display = "block";
    }

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

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

    </body>
    </html>

您的代码是响应式的...但是如果您仍然没有响应式,那么请在下面的meta标记中添加以下内容:

<meta name="viewport" content="width=device-width, initial-scale=1">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM