我刚刚完成了一个中型网站,我注意到我的 CSS 组织的一件事是我在整个网站中有很多硬编码的颜色值。这显然不利于可维护性。一般来说,当我设计一个网站时,我会为主题选择 3-5 个主要颜色。我最终在主 css 的开头为段落、链接等设置了一些默认值,但某些组件会更改颜色(例如图例标签)并要求我使用我想要的颜色重新设置样式。你如何避免这种情况?我正在考虑为每种颜色创建单独的规则,并在需要重新设计样式时使用这些规则。
IE
.color1 {
color: #3d444d;
}
Run Code Online (Sandbox Code Playgroud)
这正是你应该做的。
你的 CSS 越集中,将来进行更改就越容易。说实话,您将来会想要改变颜色。
你几乎不应该将任何 css 硬编码到你的 html 中,它应该全部在 css 中。
另外,我开始更频繁地做的事情是将你的 css 类相互分层,以便更轻松地更改颜色一次......代表无处不在。
示例(随机颜色)CSS:
.main_text {color:#444444;}
.secondary_text{color:#765123;}
.main_color {background:#343434;}
.secondary_color {background:#765sda;}
Run Code Online (Sandbox Code Playgroud)
然后是一些标记,注意我如何将颜色层与其他类一起使用,这样我就可以更改一个 css 类:
<body class='main_text'>
<div class='main_color secondary_text'>
<span class='secondary color main_text'>bla bla bla</span>
</div>
<div class='main_color secondary_text>
You get the idea...
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
记住...内联 css = 不好(大多数时候)