简体   繁体   English

设置背景图像以根据视口大小进行覆盖

[英]Set background image to cover according to viewport size

With respect to the mentioned subject, I would like to know how to add code that will set the background image size to cover the window completely without the sides getting cut off. 关于所提到的主题,我想知道如何添加代码,该代码将设置背景图像的大小以完全覆盖窗口而不会截断侧面。 I have tried using height and width auto,cover and 100% but none are working. 我试过使用高度和宽度自动,覆盖和100%,但没有一个工作。

Any help is appreciated. 任何帮助表示赞赏。 Thank you! 谢谢!

Use the CSS background-size property. 使用CSS background-size属性。

If you want your background image to fill the area to the minimum width/height use: 如果希望背景图像将区域填充到最小宽度/高度,请使用:

background-size: cover;

If you want your background image to fill the area as large as possible but still show all your image use: 如果您想让背景图片尽可能大地填充区域,但仍显示所有图片,请使用:

background-size: contain;

html{ width:100%; height:100%; 
      background:#fff url(image.jpg) center center no-repeat;
      background-attachment: fixed;
      background-size:100% auto;
}

Actually, I found it! 其实我找到了! I used a div to wrap the whole content inside the body with the ID 'wrapper' and then used the following CSS code: 我使用div将整个内容包装在ID为“ wrapper”的正文中,然后使用以下CSS代码:

#wrapper
{
background-image: url('Media/bg.jpg');
position:absolute;
top:0;
bottom:0;
right:0;
left:0;
}

Thanks for all the help! 感谢您的所有帮助! :) :)

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

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