CSS正文元素宽度不起作用 [英] CSS body element width not working
问题描述
HTML:
<body>
<p>Some content</p>
</body>
CSS:
body {
margin: 0 auto;
width: 470px;
background-color: cyan;
}
https://jsfiddle.net/tsLu98tL/
为什么这不能一直居中,为什么颜色会扩展到整个页面而不是470像素?
Why does this not center all the way and why does the color expand to the whole page and not 470px?
推荐答案
主体实际上是处于该宽度,并且是居中.这只是一个CSS怪癖,使背景占据了整个页面,而不是body元素实际占据的空间.
The body actually is taking that width, and it is centering. It's just a CSS quirk that makes the background occupy the whole page rather than the space actually occupied by the body element.
解决此问题的一种方法是在 html
标记上包含一个 background
属性.
A way to fix this is to include a background
property on the html
tag.
这里是示例.
但是,正如其他人提到的那样,这可能不是您想要执行的操作.最好在正文和样式内添加 div
.
However, as mentioned by others, this probably isn't something you want to do. It's better to add a div
within the body and style that.
这篇关于CSS正文元素宽度不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!