我有一个网站,有许多页面和不同的背景图片,我从CSS显示它们,像这样:

body.page-8 {
    background: url("../img/pic.jpg") no-repeat scroll center top #000;
    background-size: cover;
}

但是,我想在一个页面上使用<img>元素显示不同的(全屏)图片,并且我希望它们具有与上面的background-image相同的属性:cover;属性(图像不能显示从CSS,他们必须显示从HTML文档)。

通常我使用:

div.mydiv img {
    width: 100%;
}

Or:

div.mydiv img {
    width: auto;
}

使画面饱满,反应灵敏。然而,当屏幕变得太窄时,图片会收缩太多(宽度:100%),并在底部屏幕显示身体的背景色。另一种方法width: auto;只使图像完全大小,不响应屏幕大小。

是否有一种方法可以像background-size: cover那样显示图像?


当前回答

我找到了一个简单的解决方案来模拟覆盖和包含,这是纯CSS,适用于具有动态尺寸的容器,也没有对图像比例进行任何限制。

注意,如果你在16之前不需要支持IE或Edge,那么你最好使用object-fit。

background-size:封面

.img-container { position: relative; overflow: hidden; } .background-image { position: absolute; min-width: 1000%; min-height: 1000%; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%) scale(0.1); z-index: -1; } <div class="img-container"> <img class="background-image" src="https://picsum.photos/1024/768/?random"> <p style="padding: 20px; color: white; text-shadow: 0 0 10px black"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> </div>

在这里使用1000%是为了防止图像的自然大小大于它正在显示的大小。例如,如果图像是500x500,但容器只有200x200。使用这个解决方案,图像将被调整到2000x2000(由于min-width/min-height),然后缩小到200x200(由于变换:scale(0.1))。

x10因子可以用x100或x1000来代替,但是在20x20的div上渲染2000x2000的图像通常不理想。:)

background-size:包含

Following the same principle, you can also use it to emulate background-size: contain: .img-container { position: relative; overflow: hidden; z-index: 0; } .background-image { position: absolute; max-width: 10%; max-height: 10%; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%) scale(10); z-index: -1; } <div style="background-color: black"> <div class="img-container"> <img class="background-image" src="https://picsum.photos/1024/768/?random"> <p style="padding: 20px; color: white; text-shadow: 0 0 10px black"> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p> </div> </div>

其他回答

解决方案#1 -对象匹配属性(缺乏IE支持)

只需设置对象适合:覆盖;在img上。

身体{ 保证金:0; } img { 显示:块; 宽度:100大众; 身高:100 vh; object-fit:封面;/*或object-fit:包含;* / } <img src="https://loremflickr.com/1500/1000" alt="A random image from Flickr" />

关于object-fit: cover:

替换的内容大小保持其纵横比 填充元素的整个内容框。如果物体的纵横比 不匹配它的长宽比的盒子,那么对象会是什么 剪得合身。

对于object-fit:包含:

被替换的内容被缩放以保持其纵横比,同时适应元素的内容框。整个对象被用来填充方框,同时保留其长宽比,因此如果对象的长宽比与方框的长宽比不匹配,则该对象将被“字母盒化”。

另外,请参阅这个Codepen演示,它比较了应用于图像的object-fit: cover和应用于背景图像的background-size: cover


解决方案#2 -用css的背景图像替换img

身体{ 保证金:0; } img { 位置:固定; 宽度:0; 高度:0; 填充:50vh 50vw; 背景:url(“https://loremflickr.com/1500/1000”)无重复; background-size:封面; } <img src="https://via.placeholder.com/1500x1000" alt="A random image from Flickr" />

你能做的是使用'style'属性将背景图像添加到元素中,这样你仍然会在HTML中调用图像,但你仍然能够使用background-size: cover css行为:

HTML:

    <div class="image-div" style="background-image:url(yourimage.jpg)">
    </div>

CSS:

    .image-div{
    background-size: cover;
    }

这就是我如何将background-size: cover行为添加到我需要动态加载到HTML中的元素。然后你可以使用很棒的css类,如background-position: center。繁荣

假设您可以安排一个希望填充的容器元素,这似乎是可行的,但感觉有点笨拙。本质上,我只是在更大的区域上使用min/max-width/height,然后将该区域缩小到原始尺寸。

.container { width: 800px; height: 300px; border: 1px solid black; overflow:hidden; position:relative; } .container.contain img { position: absolute; left:-10000%; right: -10000%; top: -10000%; bottom: -10000%; margin: auto auto; max-width: 10%; max-height: 10%; -webkit-transform:scale(10); transform: scale(10); } .container.cover img { position: absolute; left:-10000%; right: -10000%; top: -10000%; bottom: -10000%; margin: auto auto; min-width: 1000%; min-height: 1000%; -webkit-transform:scale(0.1); transform: scale(0.1); } <h1>contain</h1> <div class="container contain"> <img src="https://www.google.de/logos/doodles/2014/european-parliament-election-2014-day-4-5483168891142144-hp.jpg" /> <!-- 366x200 --> </div> <h1>cover</h1> <div class="container cover"> <img src="https://www.google.de/logos/doodles/2014/european-parliament-election-2014-day-4-5483168891142144-hp.jpg" /> <!-- 366x200 --> </div>

尝试同时设置min-height和min-width,使用display:block:

img {
    display:block;
    min-height:100%;
    min-width:100%;
}

(小提琴)

如果你的图像的包含元素是position:relative或position:absolute,图像将覆盖容器。然而,它不会集中。

如果你知道它是水平溢出(设置margin-left:-50%)还是垂直溢出(设置margin-top:-50%),你可以很容易地将图像居中。可以使用CSS媒体查询(和一些数学)来解决这个问题。

不,你不能像background-size:cover那样得到它,但是。

这种方法非常接近:它使用JavaScript来确定图像是横向的还是纵向的,并相应地应用样式。

JS

 $('.myImages img').load(function(){
        var height = $(this).height();
        var width = $(this).width();
        console.log('widthandheight:',width,height);
        if(width>height){
            $(this).addClass('wide-img');
        }else{
            $(this).addClass('tall-img');
        }
    });

CSS

.tall-img{
    margin-top:-50%;
    width:100%;
}
.wide-img{
    margin-left:-50%;
    height:100%;
}

http://jsfiddle.net/b3PbT/