Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

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>


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

因为在所有html标签中,body的background最特别,与其它的标签背景计算方法都不一样;
可能W3C当时做浏览器和html语言时就是故意把body设计的复杂一些,起到统领全局的作用;我的建议是,把这些特殊的地方背诵记住避免以后出问题时找不到bug源头;
总结一下body的background有两个奇怪的地方:
1、你可以试着给body一个红色背景色,会发现全屏宽、全屏高都铺满了红色;
2、给body设置 margin:50px auto; 居中后,发现背景色和背景图片都是从最左边到最右边宽度百分百,背景色的高度也是百分百;
在你的代码基础上做了修改,你看下:
http://runjs.cn/code/jwwcoan0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...