img图片在设定的宽高中不变形而居中显示(css)

一、 
<style>
        .container {
            width: 36.4583vw;
            height: 20.8333vw;
            height: 55.5556vh;
            background: #cdcdcd;
            margin: 0 auto;
        }

        .imgbox {
            width: 13.0208vw;
            height: 30.5556vh;
            overflow: hidden;
        }

        img {
            height: 100%;
            width: auto;
            left: 50%;
            position: relative;
            transform: translateX(-50%);
            -webkit-transform: translateX(-50%);
            -ms-transform: translateX(-50%);
            -moz-transform: translateX(-50%);
        }

        /* 或者 */
        img {
            height: auto;
            width: 100%;
            top: 50%;
            position: relative;
            transform: translateY(-50%);
            -webkit-transform: translateY(-50%);
            -ms-transform: translateY(-50%);
            -moz-transform: translateY(-50%);
        }
    </style>
  <div class="container">
        <div class="imgbox">
            <img src="./img/bg-injection1.png" alt="">
        </div>
    </div>
 
二、
 <style>
        .container {
            width: 36.4583vw;
            height: 20.8333vw;
            height: 55.5556vh;
            background: #cdcdcd;
            margin: 0 auto;
        }
        .imgbox {
            width: 13.0208vw;
            height: 30.5556vh;
            overflow: hidden;
        }
        img {
            width: inherit;
            height: inherit;
            object-fit: cover;
            object-position: center;
        }
    </style>
 <div class="container">
        <div class="imgbox">
            <img src="./img/bg-injection1.png" alt="">
        </div>
    </div>
posted @   蓝色精灵jah  阅读(515)  评论(0编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
点击右上角即可分享
微信分享提示