Html 在正文中设置 div 背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7947363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:19:01  来源:igfitidea点击:

set div background color in body

htmlcss

提问by NomenNescio

Im trying to set the background color of a div tag that resides in a body tag to the entire screen but I have not found a satisfactory solution to this problem:

我试图将位于 body 标签中的 div 标签的背景颜色设置为整个屏幕,但我还没有找到满意的解决方案:

...
<body>
  <div>
  </div>
</body>
...

How can I set the background color of the whole page from the div tag that resides in body?

如何从位于 body 中的 div 标签设置整个页面的背景颜色?

doing

正在做

div {
  background-color: green;
}

doesn't turn the background green.

不会将背景变成绿色。

回答by no.

Why can't you just set the body's background color?

为什么你不能只设置身体的背景颜色?

Either way you'll have to do something like this:

无论哪种方式,您都必须执行以下操作:

CSS:

CSS:

html, body { padding: 0; margin: 0; height: 100%; }

#fake_body { width: 100%; height: 100%; background: #000; }

HTML:

HTML:

<html>
    <body>
        <div id="fake_body"></div>
    </body>
</html>

Unless I'm missing something, that should work. jsFiddle

除非我遗漏了什么,否则应该有效。js小提琴

回答by Andrew Humphreys

Just make sure the div covers the entire page I suppose.

只要确保 div 覆盖了我想的整个页面。

#divID {
background-color: #fff;
top: 0;bottom: 0;right: 0;left: 0;
position: absolute;
}

Note:

笔记:

Woah, 2011.. I guess I am late to the party.. by far. Anyways, for the sake of google.

哇,2011.. 我想我参加聚会迟到了..到目前为止。无论如何,为了谷歌。