html - 中心裁剪图像

标签 html css image

我需要在垂直框(例如 600 X 400)内居中裁剪横向图像我有以下 html 代码和用于居中裁剪的 css 代码。但是输出图像不是中心裁剪的,它也被拉伸(stretch)了。

<div id="posts">
    <div class="image">
        <a href="#"> 
           <img src="Vasco45_barbosa.jpg">
        </a>
    </div>
</div>

 #posts .image {
    overflow: hidden;
    height: 600px;
    width: 400px;
    margin: 0 auto;
}

#posts .image img{
    background-position: center center;
    background-size: cover;
    display: block; 
    min-width: 100%;  
    min-height: 100%;  
    -ms-interpolation-mode: bicubic; 
}

Original Image
Image after applying the above css code

最佳答案

将 img css 改成这个

#posts .image img{
    object-fit: cover;
    object-position: 50% 50%; 
    width: 100%;
    height: 100%;
}

工作原理:https://jsfiddle.net/6ns549hy/

关于html - 中心裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40205364/

相关文章:

Php - 将 html 标签打印为文本

html - 响应式 - css 视差类型 - 图像不在容器内

html - CSS 高度不修改图像

javascript - 如何在被 html 表格的空单元格覆盖的 div 上接收点击事件?

android - 在 multipart retrofit2 中上传图像数组

python - 如果使用python在捕获区域中存在某些颜色,如何返回 bool 值

python - 如何使用Python在图像上生成随机正弦条纹?

php - 出现 "You have an error in your SQL syntax"错误 - PHP、MySQL

html - 如何通过中间在同一行上设置两个 block ?

css - 我可以使用转换作为转换属性的值吗?