body标签疑问

来源:8-4 编程练习

小白0719

2022-11-26 08:48:22

老师,给body标签设置width属性后,为什么字体显示宽度收到限制,而背景颜色宽度不受限制呢?


<style type="text/css">
        body{
           
            width: 900px;
            background-color: yellow;
            background-image: url(img/5ea016c108c1f40603610240.jpg);
            background-position: center center;
            background-repeat: no-repeat;
        }
        p{
            background-color: red;
        }
        p{
         font-size: 40px;
      }
      .p1{
         font-family: 隶书;
      }
    </style>


https://img.mukewang.com/climg/638162300934c08d19130901.jpg


写回答

1回答

好帮手慕小脸

2022-11-26

同学你好,因为当html不设置背景时,body的背景将作为整个浏览器的背景色,而不是body标签自己的背景色。当设置html背景色后,body的背景色就变成了body自己的背景色,此时html的背景色将被浏览器获取成为浏览器的背景色。

所以,要让body的背景色渲染范围只在设置的body范围之内,我们需要对html进行背景样式的设置,比如

https://img.mukewang.com/climg/6381780c094bd4c709110243.jpg

祝学习愉快~


1

0 学习 · 9886 问题

查看课程