Maison  >  Questions et réponses  >  le corps du texte

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>

巴扎黑巴扎黑2741 Il y a quelques jours811

répondre à tous(3)je répondrai

  • PHP中文网

    PHP中文网2017-04-17 14:56:09

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

    répondre
    0
  • 阿神

    阿神2017-04-17 14:56:09

    以前回答过的答案:
    body特殊性

    répondre
    0
  • PHP中文网

    PHP中文网2017-04-17 14:56:09

    果然, 之前也没有碰到过这样的问题!

    试了下!

    如果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.将会得到如下效果:

    希望对你有所帮助!

    répondre
    0
  • Annulerrépondre