先设置html及body的CSS的高,分别为height:100%\x0d\x0a如下:\x0d\x0ahtml,body{height:100%margin:0px}\x0d\x0a或\x0d\x0ahtml{height:100%}\x0d\x0abody{height:100%margin:0px}\x0d\x0a后面的高为100%才有效:\x0d\x0a
\x0d\x0a或\x0d\x0a
回答于 2022-12-11
抢首赞
已踩
0
查看全部1个回答
— 为你推荐更多精彩内容 —
正在加载
加载失败 点击重新加载
微信
微博
QQ
QQ空间
答案纠错
举报
取消
赞赏答主
5
10
50
100
200
已赞赏0财富值
合计:0 财富值
登录后赞赏
选择举报类型
侵犯版权
色情低俗
涉嫌违法犯罪
时政信息不实
垃圾广告
低质灌水
工作人员会在48小时内处理,处理结果请关注系统通知,感谢您对百度知道的支持。
确定
void function(a,b,c,d,e,f){function g(b){a.attachEvent?a.attachEvent("onload",b,!1):a.addEventListener&&a.addEventListener("load",b)}function h(a,c,d){d=d||15var e=new Datee.setTime((new Date).getTime()+1e3*d),b.cookie=a+"="+escape(c)+"path=/expires="+e.toGMTString()}function i(a){var c=b.cookie.match(new RegExp("(^| )"+a+"=([^]*)(|$)"))return null!=c?unescape(c[2]):null}function j(){var a=i("PMS_JT")if(a){h("PMS_JT","",-1)try{a=a.match(/{["']s["']:(\d+),["']r["']:["']([\s\S]+)["']}/),a=a&&a[1]&&a[2]?{s:parseInt(a[1]),r:a[2]}:{}}catch(c){a={}}a.r&&b.referrer.replace(/#.*/,"")!=a.r||alog("speed.set","wt",a.s)}}if(a.alogObjectConfig){var k=a.alogObjectConfig.sample,l=a.alogObjectConfig.randd="https:"===a.location.protocol?"https://fex.bdstatic.com"+d:"http://fex.bdstatic.com"+d,k&&l&&l>k||(g(function(){alog("speed.set","lt",+new Date),e=b.createElement(c),e.async=!0,e.src=d+"?v="+~(new Date/864e5)+~(new Date/864e5),f=b.getElementsByTagName(c)[0],f.parentNode.insertBefore(e,f)}),j())}}(window,document,"script","/hunter/alog/dp.mobile.min.js")
window.tt = 1676983402
当我们给块级元素设置响应式高度的时候,例如给div设置height=50%,往往没能看到效果。
原因是百分比的大小是相对其父级元素宽高的大小,如最外层元素设置的百分比是对应屏幕而言的。
需要了解的是对于宽度来说,其父级元素无须确定宽度就能设置百分比,例如我们可以利用这个特性给未知宽度的块级元素设置水平居中效果:
父元素css: position: relative/absoluteleft: 50%
子元素css: position: relativeleft: -50%
但高度则不同,若某元素的父元素没有确定高度,则无法有效使用height=XX%的样式,我们可以这样解决(假设最外层的div需要设置百分比高度样式):
html, body {
height: 100%
}
.outDiv {
height: 50%
}
不过这里有个需要注意的,若div里的内容超出了div的高度,在IE7+的浏览器是无法将div撑起来的(IE6则可以),如果要顾及这一点,可以使用min-height解决(当然也要考虑IE6不支持min-height的问题):
html, body {
height: 100%
}
.outDiv {
min-height: 50%
}
* html .outDiv {
height: 50%
}
最后说通俗点,如果你想把高度设为百分比,那么父级必须设置高度
/* 首先,高度是内容撑出来的,宽度没问题 */
/* .bgp 就自适应浏览器的长度和高度 */
.bgp
{
position:fixed
top:0
right:0
bottom:0
left:0
}