河畔的风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

object-fit: fill || cover || contain || none || scale-down

 

fill: 中文释义“填充”。默认值。替换内容拉伸填满整个contentbox,不保证保持原有的比例。

contain: 中文释义“包含”。保持原有尺寸比例。保证替换内容尺寸一定可以在容器里面放得下。因此,此参数可能会在容器内留下空白。
cover: 中文释义“覆盖”。保持原有尺寸比例。保证替换内容尺寸一定大于容器尺寸,宽度和高度至少有一个和容器一致。因此,此参数可能会让替换内容(如图片)部分区域不可见(上面讲解的例子就是如此)。
none: 中文释义“无”。保持原有尺寸比例。同时保持替换内容原始尺寸大小。
scale-down: 中文释义“降低”。就好像依次设置了none或contain, 最终呈现的是尺寸比较小的那个。

 

 

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        img {
            height: 200px;
            width: 200px;

        }

        .img-fill {
            object-fit: fill;
        }

        .img-cover {
            object-fit: cover;
        }

        .img-conatin {
            object-fit: contain;
        }

        .img-none {
            object-fit: none;
        }

        .img-sacle-down {
            object-fit: scale-down;
        }

        .box {
            display: flex;

        }

        .box>div {
            padding: 20px;
        }
    </style>
</head>

<body>
    <div class="box">
        <div>
            <div>原图</div>
            <img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2442489277,908091175&fm=26&gp=0.jpg"
                alt="">
        </div>
        <div>
            <div>object-fit: fill</div>
            <img class="img-fill"
                src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2442489277,908091175&fm=26&gp=0.jpg"
                alt="">
        </div>
        <div>
            <div>object-fit: cover</div>
            <img class="img-cover"
                src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2442489277,908091175&fm=26&gp=0.jpg"
                alt="">
        </div>
        <div>
            <div>object-fit: contain</div>
            <img class="img-conatin"
                src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2442489277,908091175&fm=26&gp=0.jpg"
                alt="">
        </div>
        <div>
            <div>object-fit: none</div>
            <img class="img-none"
                src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2442489277,908091175&fm=26&gp=0.jpg"
                alt="">
        </div>
        <div>
            <div>object-fit: scale-down</div>
            <img class="img-sacle-down"
                src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2442489277,908091175&fm=26&gp=0.jpg"
                alt="">
        </div>
    </div>
</body>

</html>
posted on 2020-10-14 16:09  河畔的风  阅读(85)  评论(0编辑  收藏  举报