我有一个网站,有许多页面和不同的背景图片,我从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>

其他回答

对于IE6,你还需要包括第二行——width: 100%;

.mydiv img {
    max-width: 100%;
    width: 100%;
}
 

我不允许'添加一个评论'这样做,但是的,什么Eru Penkman做的是相当到位,让它像背景覆盖所有你需要做的就是改变

.tall-img { margin-top: -50%; 宽度:100%; } .wide-img { margin-left: -50%; 高度:100%; }

TO

.wide-img { margin-left: -42%; 高度:100%; } .tall-img { margin-top: -42%; 宽度:100%; }

使用CSS可以模拟对象匹配:[cover| include];使用transform和[max|min]-[width|height]。 它并不完美。这在一种情况下不起作用:如果图像比容器更宽或更短。

.img-ctr{ background: red;/*visible only in contain mode*/ border: 1px solid black; height: 300px; width: 600px; overflow: hidden; position: relative; display: block; } .img{ display: block; /*contain:*/ /*max-height: 100%; max-width: 100%;*/ /*--*/ /*cover (not work for images wider and shorter than the container):*/ min-height: 100%; width: 100%; /*--*/ position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); } <p>Large square: <span class="img-ctr"><img class="img" src="http://placehold.it/1000x1000"></span> </p> <p>Small square: <span class="img-ctr"><img class="img" src="http://placehold.it/100x100"></span> </p> <p>Large landscape: <span class="img-ctr"><img class="img" src="http://placehold.it/2000x1000"></span> </p> <p>Small landscape: <span class="img-ctr"><img class="img" src="http://placehold.it/200x100"></span> </p> <p>Large portrait: <span class="img-ctr"><img class="img" src="http://placehold.it/1000x2000"></span> </p> <p>Small portrait: <span class="img-ctr"><img class="img" src="http://placehold.it/100x200"></span> </p> <p>Ultra thin portrait: <span class="img-ctr"><img class="img" src="http://placehold.it/200x1000"></span> </p> <p>Ultra wide landscape (images wider and shorter than the container): <span class="img-ctr"><img class="img" src="http://placehold.it/1000x200"></span> </p>

background:url('/image/url/') right top scroll; 
background-size: auto 100%; 
min-height:100%;

遇到完全相同的症状。以上对我来说很管用。

实际上有一个非常简单的css解决方案,甚至可以在IE8上工作:

.container { position: relative; overflow: hidden; /* Width and height can be anything. */ width: 50vw; height: 50vh; } img { position: absolute; /* Position the image in the middle of its container. */ top: -9999px; right: -9999px; bottom: -9999px; left: -9999px; margin: auto; /* The following values determine the exact image behaviour. */ /* You can simulate background-size: cover/contain/etc. by changing between min/max/standard width/height values. These values simulate background-size: cover */ min-width: 100%; min-height: 100%; } <div class="container"> <img src="http://placehold.it/200x200" alt="" /> </div>