首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >object-fit未按预期执行

object-fit未按预期执行
EN

Stack Overflow用户
提问于 2018-10-04 10:46:36
回答 1查看 39关注 0票数 1

所以我尝试使用object-fit,但似乎忽略了css (似乎是在自动模式下)。

第二个图像是我希望它看起来是什么样子(或者至少具有的功能)。

HTML

代码语言:javascript
复制
<section
    style="background-image: url('https://via.placeholder.com/400x1000/d3d3d3/000000/?text=No%20Image&')"
    class="image"
>
    <img src="https://via.placeholder.com/400x1000/d3d3d3/000000/?text=No%20Image&">
</section>

<br><br><br>

<section
    style="background-image: url('https://via.placeholder.com/400x1000/d3d3d3/000000/?text=No%20Image&')"
    class="image contain"
>
    <img
        src="https://via.placeholder.com/400x1000/d3d3d3/000000/?text=No%20Image&"
        style="visibility: hidden;"
    >
</section>

SCSS

代码语言:javascript
复制
.image {
    background-position: 50% 50%;
    background-repeat: no-repeat;
    max-height: 400px;
    max-width: 100%;
    overflow: hidden;
    position: relative;
    width: 200px;
    height: 200px;
    &.auto {
        background-size: auto;
    }
    &.contain {
        background-size: contain;
    }
    &.cover {
        background-size: cover;
    }

    img {
        // max-height: 100%;
        // max-width: 100%;
        width: 100%;
        object-fit: contain;
        object-position: 50% 50%;
    }
}

这就是小提琴

https://jsfiddle.net/whb7qLku/2/

EN

回答 1

Stack Overflow用户

发布于 2018-10-04 14:09:28

由于图像处于纵向模式,您需要限制图像的高度才能使其工作。

设置img { max-height: 100%; max-width: 100%;}img { height: 100%; width: 100%;}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52638245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档