繁体   English   中英

body 元素的 CSS 背景颜色不起作用

[英]CSS background-color for body element not working

我无法让身体改变它的背景颜色。 所有其他 CSS 属性都完美地适用于其他元素。

 document.getElementById('color').onclick = changeColor; var currentColor = "red"; function changeColor() { if (currentColor == "red") { document.body.style.color = "green"; currentColor = "green"; } else { document.body.style.color = "red"; currentColor = "red"; } return currentColor; }
 body { font-family: Arial; font-size: 32px; color: red; background-color: #E7E7E7; } h1 { font-family: Georgia, Arial; font-size: 38px; } p { font-family: Arial; } button { font-family: Arial; } ul { list-style-type: none; margin: 0; padding: 0; text-align: center; }
 <h1> This is a header.</h1> <button id="color">Change the color</button> <p> Paragraph testing two.</p> <br><br><br><br><br><br><br> <ul> <li><a href="https://www.google.com">Google</a></li> <li><a href="https://www.cnn.com">CNN</a></li> </ul>

首先,这可以应用于 !important 指令,例如

body {
 background-color: #E7E7E7 !important;
}

如果您需要更多帮助,请提供有关项目或页面的更多详细信息。 它也可以继承一些其他 CSS 指令或类,或者它可能包含样式指令。

如果您共享页面代码,解决方案可以更快。

检查这个。 您的预览代码我无法获得您的完整代码。 现在检查这个https://codepen.io/bravotanmoy/pen/bRpEbL

HTML

<button id="color" onclick="changeColor()">Change the color</button>

<div id="white"></div>
<div id="not-white"></div>

CSS

 body {
  background-color: yellow;  
}

div {
  border: 1px solid #000;
  float: left;
  height: 50px;
  width: 50px;
}

#white {
  background: #fff;
  margin-right: 20px;
}

#not-white {
  background:#e7e7e7;
}

JavaScript

//document.getElementById('color').onclick = changeColor();
var currentColor = "red";

function changeColor() {
        if(currentColor == "red"){
        document.body.style.backgroundColor  = "green";
        currentColor = "green";
        }
        else{
        document.body.style.backgroundColor  = "red";
        currentColor = "red";
        }
        return currentColor;
}

尝试使用这个简单的 css,在 css 中创建新的唯一 ID,例如:david

.david {
  background-color: #19529C;
  font-family: Helvetica, sans-serif;
}

然后将您网站上的 html 代码匹配到该类,通常像一个部分

像这样:

<section id="unique id" class="david">

在此处添加您的内容

</section>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM