简体   繁体   English

Div全高和动态

[英]Div full height and width dynamic

现在,我尝试制作一个带有背景图像的div,以填充页面加载时的整个视口,并在需要时将所有其他页面内容下推,以便您在加载页面时只看到带有图像的div-这是什么最好的方法来使这个工作?

Demo Fiddle 演示小提琴

This should do it: 应该这样做:

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

You need to set the height of the div to 100%, but this needs to be relative to 'something' which is why you also set height:100% for the html and body elements. 您需要将div的高度设置为100%,但这需要相对于“某物”,这就是为什么还要将htmlbody元素设置为height:100%的原因。 As long as the div with the image appears first in your HTML, it will work as anticipated. 只要带有图片的div首先出现在您的HTML中,它就会按预期工作。

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

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