html - 基本 HTML 文档中的 CSS 颜色更改

标签 html css

考虑以下代码:

<html>

<head>
  <style>
    body {
      background-color: #CCC;
    }

    .con {
      background-color: #FFF;
      width: 80%;
      margin: 0 auto;
    }
  </style>
</head>

<body>
  <div class="con">
    [SITE CONTENT]
  </div>
</body>

</html>

我想要实现的是让内容背景为白色,占据屏幕宽度的 80% 并且居中。

主体背景(10% 右侧和 10% 左侧)将为灰色。 (80% 的中心将被白色背景的内容 div 占据)。

它工作正常。但大多数情况下,当页面加载时(主要是网络速度较慢时),最初内容也会显示为灰色背景,然后变为白色。

避免这种过渡效果的最佳方法是什么?

我现在正在考虑以下选项:

a. Change color of body at the end using js

b. Initially in , background color of body also be white and then at the end of document, override css and make it gray.

但我确信有更好的解决方案。请帮忙。

最佳答案

你可以在不使用 jQuery 的情况下尝试这样的事情:

display: none; 中设置您的 html。然后当您的页面加载时更改为 display: block; 以显示 html 内容。

setTimeout(function() {
  var html = document.querySelector("html");
  html.setAttribute("style", "display: block");
}, 1000);
html {
  display: none;
}
body {
  background-color: #CCC;
}
.con {
  background-color: #FFF;
  width: 80%;
  margin: 0 auto;
}
<body>
  <div class="con">[SITE CONTENT]</div>
</body>

另一种方法是使用 jQuery 库:

$(function() // When the DOM is ready.
{
    $("html").css({
      "display": "block"
    });
});
html {
  display: none;
}
body {
  background-color: #CCC;
}
.con {
  background-color: #FFF;
  width: 80%;
  margin: 0 auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<body>
  <div class="con">[SITE CONTENT]</div>
</body>

希望对你有帮助

关于html - 基本 HTML 文档中的 CSS 颜色更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32925126/

相关文章:

html - 基本 CSS/布局问题 - 动态列?

animation - css 动画持久结束状态

python - 从 html 表制作数据框时,可以在 pandas read_html 中保留换行符吗?

javascript - 位置随机形状

python - 如何使用用户输入作为参数运行 .py 脚本

html - 在链接语句中指定类型属性

javascript - 可调整大小的 jquery 与 map 不工作

css - 将 typekit 与字体事件一起使用时避免 FOUT

javascript - 从 JavaScript 中删除 HTML li 和文本

html - 使用 “<select>” 时 Div 行为不正确;使用 “&lt;input&gt;” 时工作正常