开始新项目时的 CSS 最佳实践
我想知道在开始大型项目的 CSS 工作时采取的最佳方法是什么?因为我在大多数大型项目(如 wordpress)中看到它们将共享相同属性的所有类聚集在一起,但是,您如何事先知道它们将被匹配,或者这是编程后的微工作?
不管怎样,只是想知道对类、id 等进行分组的最佳实践,以及这种方式的行业标准方法是什么。
I wanted to know what's the best approach to take on, when starting work on a CSS of a big project? Because I see on most big projects (like wordpress) that they bunch all the classes sharing same properties together, however, how can you know before hand that they'll be matched, or is that the post-programming micro-work?
Anyway, Just wanted to know the best practises for grouping classes, ids and such, and what's the industry standard approach on this manner.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CSS 框架
对于大型项目,您可能需要在“常规”CSS 之上添加额外的功能,例如嵌套、继承和混合。其中任何一个都应该完成工作:
性能优化
此外,您还需要进行自动性能优化(源文件的串联、缩小和压缩),因此请查看:
或任何适合您的开发平台的内容。
命名
许多大型站点在类名上使用某种前缀来将样式类与脚本类分开。例如:
dyn*
类在脚本中用作选择器,而navigation
类用于样式设置。这样做的好处是,您可以让编码人员在不触及设计的情况下重构脚本,而设计人员可以更改模板而不必担心破坏功能。然而,借助现代 Javascript 框架和 HTML5,您可以做得更好;对 ID 和类使用语义命名,使用这些 ID 和类应用样式,并对所有脚本挂钩使用
data-*
属性。示例:您将使用类标识符设置样式:
并使用数据挂钩的脚本(使用 James Padolsey 的 jQuery 数据选择器属性):
它可能不像旧的 use-semantic-classes-for-everything 方法那么简洁或看起来那么熟悉,但它会创建一个整齐的分离样式和行为属性,一旦您拥有 50.000 行 HTML 并且需要修改设计,您就会欣赏它们。
CSS Frameworks
For big projects, you'll likely want extra functionality on top of 'regular' css, like nesting, inheritance and mixins. Either one of these should get the job done:
Performance optimization
Also, you'll want to do automatic performance optimization (concatenation, minification, and compression of source files), so take a look at:
or whatever suits your development platform.
Naming
Many large sites use some kind of prefix on class names to separate style classes from script classes. E.g.:
Where the
dyn*
class is used as a selector in scripts, while thenavigation
class is used for styling. The benefit is you can have coders refactoring scripts without touching the design, and designers changing the templates without worrying about breaking functionality.However, with modern Javascript frameworks and HTML5 you can do better; use semantic naming for IDs and classes, apply style using those IDs and classes, and use
data-*
attributes for all script hooks instead. Example:Which you will style using the class identifier:
And script using the data hook (using James Padolsey's data selector attribute for jQuery):
It may not be as concise or look as familiar as the good old use-semantic-classes-for-everything method, but it will create a neat separation of style and behavioral properties, which you'll appreciate once you have 50.000 lines of HTML and you need to revamp the design.