html - 为什么 body 元素的背景样式会影响整个屏幕?

标签 html css background

当您为 body 元素的背景设置样式时,为什么样式会影响整个屏幕而不仅仅是 body 元素本身?假设我创建了以下规则:

body {
  width: 700px;
  height:200px;
  border: 5px dotted red;
  background-color: blue;
}

我发现边框显示为 700 像素宽,如我所料,但背景色占据了整个浏览器视口(viewport)。为什么?

最佳答案

引自 http://www.w3.org/TR/CSS21/colors.html

The background of the root element becomes the background of the canvas and covers the entire canvas, anchored (for 'background-position') at the same point as it would be if it was painted only for the root element itself. The root element does not paint this background again.

body 元素是根元素,因此,根据 CSS 规则的要求,它失去了背景样式,背景样式应用于包含 Canvas (浏览器中的网页区域), 因此整个屏幕都是蓝色的。其他属性保留在元素中(例如 border)。

关于html - 为什么 body 元素的背景样式会影响整个屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29553519/

相关文章:

javascript - 如何遍历表中输入的所有值(html/javascript)

javascript - 获取所有样式属性颜色

html - Joomla 3.x : Limit a module's size

css - 导航高度和填充

javascript - 如何使 iframe 响应

javascript - 参数后缺少 ) 但仅适用于长字符串

javascript - 用 JS 面向对象编程改变 CSS

html - 用于打印的 3 个文本 div 的最佳布局

CSS - 带有一些透明像素的图像周围的背景颜色

java - Android如何处理 "called from wrong thread"异常