简体   繁体   English

如何使图像的大小始终大于(或大于)浏览器窗口并固定到某个点

[英]How to make an image always the size (or bigger) than the browser window, and stuck to a certain point

I would like to know how to make an image rescale accordingly with the size of the browser window, similar to how these guys have their background video on the main page. 我想知道如何根据浏览器窗口的大小调整图像的缩放比例,类似于这些家伙在主页上显示背景视频的方式。 I have something similar working for a video, but applying the same code on the image just won't work. 我对视频有类似的工作方式,但是在图像上应用相同的代码将无法正常工作。

Having both the width and height of the image on 100% is not acceptable, since that will put the image out of proportions. 将图像的widthheight都设置在100%是不可接受的,因为这样会使图像不成比例。

All help greatly appreciated! 所有帮助,不胜感激!

Please note that it is downright impossible for me to add a jsfiddle here, because in that case I would have to put my entire project in jsfiddle. 请注意 ,在这里添加jsfiddle是绝对不可能的,因为在这种情况下,我将不得不将整个项目放在jsfiddle中。

No JavaScript required. 无需JavaScript。

You can do this using the various background CSS properties, namely background-size:cover; 您可以使用各种background CSS属性来完成此操作,即background-size:cover; and background-position:center center; background-position:center center; on an element with 100% dimensions, which will fill the browser if these are also defined for html and body and the element is a direct child. 尺寸为100%的元素上,如果还为htmlbody定义了这些元素,并且该元素是直接子元素,则会填充浏览器。

Demo Fiddle 演示小提琴

html, body, #img{
  height:100%;
  width:100%;
  margin:0;
  padding:0;
}
#img{
  background-image:url(myImage.png);
  background-size:cover;
  background-position:center center;
}

if it's a background image (like in the example link), you can use: 如果它是背景图像(如示例链接中所示),则可以使用:

background-size: cover;

Else if the img is in the HTML, you can give it a width of 100% indeed, but set the height to auto so it scales normally. 否则,如果img在HTML中,则可以为它提供100%的宽度,但是将高度设置为auto可以正常缩放。

width: 100%;
height: auto

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM