css - body 的 background 位置
问题描述
http://runjs.cn/code/p2tvxwsj
代码如下:为什么 body 的 background 位置不在 border之内
<!DOCTYPE html><html><head>
<style> body{ background-image: url(’http://www.w3cschool.cn/statics/images/course/img_flwr.gif’); background-size:100% 100%; background-repeat:no-repeat; width:300px; height:60px;border:solid;} </style>
</head><body></body></html>
问题解答
回答1:因为在所有html标签中,body的background最特别,与其它的标签背景计算方法都不一样;可能W3C当时做浏览器和html语言时就是故意把body设计的复杂一些,起到统领全局的作用;我的建议是,把这些特殊的地方背诵记住避免以后出问题时找不到bug源头;总结一下body的background有两个奇怪的地方:1、你可以试着给body一个红色背景色,会发现全屏宽、全屏高都铺满了红色;2、给body设置 margin:50px auto; 居中后,发现背景色和背景图片都是从最左边到最右边宽度百分百,背景色的高度也是百分百;在你的代码基础上做了修改,你看下:http://runjs.cn/code/jwwcoan0
回答2:以前回答过的答案:body特殊性
回答3:果然, 之前也没有碰到过这样的问题!
试了下!
如果html的background-image值为none(默认值为:none), background-color值为transparent(默认值为: transparent), 那么浏览器就会向body借用这两个属性值来对html设置background-image或background-color属性.
<style type='text/css'>html{ background-color: transparent;}body{ width: calc(100% - 100px); height: 300px; background-color: red; margin: 50px;} </style>
例如上面的代码, 若不做改动效果如下:
若改动html中background-color属性值为有效值, 如: #fafafa.将会得到如下效果:
希望对你有所帮助!
相关文章:
1. angular.js - AngularJS v1.5.8 在IE9中出现灰屏,不启动的问题。2. angular.js - 在用会话存储传递参数时,使用angularjs冲突3. jquery - angularjs的数据为什么渲染不到页面上?数据获取到了,我的代码哪里出了错了?4. angular.js - 请教angularjs里html和js通信的问题5. angular.js - 用angular2-cli打包项目之后,跳转路由刷新会报404的错误6. angular.js - angular2动态显示隐藏组件问题7. node.js - 怎么在初始化的时候使得一个子组件不加载?8. angular.js - 我想实现打印angular $scope数据,代码如何实现呢?求教(如下图)9. Python中的内列表问题,真的很困惑10. python - Ubuntu16.04LTS登录后只显示屏保