当您设计body元素的背景时,为什么样式会影响整个屏幕,而不仅仅是body元素本身?假设我创建了以下规则:
body {width: 700px; border: 1px dotted red; background-color: blue;}
我发现边框显示为700px宽,如我所料,但背景颜色占据整个浏览器视口.为什么?
解决方法
报价从
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规则的要求,它将丢失其背景样式,并将背景样式应用于包含画布(浏览器中的网页区域),因此整个屏幕是蓝色的.其他属性与元素(例如边框)保持一致.