首页 > 解决方案 > 如何使 body 元素扩展到 html 元素的大小?

问题描述

我希望正文随着页面变大而扩展。我尝试将 body 的高度设置为 100%,将 html 元素的高度设置为 100%,但它没有按预期工作:

蓝色代表正文,红色代表 html 元素

我希望正文覆盖整个 html 元素。这就是我在 css 中的代码:

html {
font-family: Palatino, 'Palatino Linotype', serif;
background-color: red;
height: 100%;
margin: 0;
padding: 0; 
}
body {
background-color: blue;
height: 100%;
margin: 0;
padding: 0;
}

我希望有一个人可以帮助我!

标签: htmlcss

解决方案


不要将高度更改为 100%,而是尝试将其更改为 height: auto; 这会自动将高度调整为其中的内容。

如果高度设置为数值(如像素、(r)em、百分比),那么如果内容不适合指定的高度,它将溢出。

希望能帮助到你


推荐阅读