【问题标题】:How to configure a modal window to fit the actual size of the contained image?如何配置模态窗口以适应包含图像的实际大小?
【发布时间】:2019-03-19 05:43:03
【问题描述】:

我正在使用 Bootstrap 4.2,显示一个弹出窗口(模态),其宽度和高度均为实际大小(100%),只要浏览器窗口的大小允许,如果没有,那就是,如果图像大于窗口大小,则会自动缩小。我已尝试使用以下代码,但图像在模态的宽度上被放大(+ 100%),尽管我将其配置为 ma​​x-width: 100%

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Imagen 100%</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
</head>
<body>
    <style>
        .img-100{
            max-width: 100%;
            max-height: 100%;
       }
    </style>
    <a href="#modalArticulo" role="button" class="btn btn-primary" data-toggle="modal">Launch modal</a>
    <div id="modalArticulo" class="modal fade " tabindex="-1" role="dialog">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <img src="../images/Imagen360x360.jpg" class="img-100">
           <!-- <img src="../images/Imagen1200x1200.jpg" class="img-100"> -->
            </div>
        </div>
    </div>
</body>
</html>

图片是动态加载的,所以默认不知道图片的大小。

【问题讨论】:

    标签: html css bootstrap-4


    【解决方案1】:

    将边距设置为自动将防止图像拉伸以填充模态 div 的宽度和高度。您不需要设置图像的最大宽度或最大高度。

    .modal-image {
        margin: auto
    }
    

    进一步添加此类以使模式适合图片并在屏幕上居中。没有它,模态框将在图像周围显示边框。

    .modal-content {
         width: unset;
         margin: auto;
    }
    

    【讨论】:

    • 这效果不好,没有响应。尝试比窗口更大的图像,例如 1200x1200。 启动模态
    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多