开始新项目时的 CSS 最佳实践

发布于 2024-11-29 11:11:04 字数 166 浏览 0 评论 0原文

我想知道在开始大型项目的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坐在坟头思考人生 2024-12-06 11:11:04

CSS 框架

对于大型项目,您可能需要在“常规”CSS 之上添加额外的功能,例如嵌套、继承和混合。其中任何一个都应该完成工作:

性能优化

此外,您还需要进行自动性能优化(源文件的串联、缩小和压缩),因此请查看:

或任何适合您的开发平台的内容。

命名

许多大型站点在类名上使用某种前缀来将样式类与脚本类分开。例如:

<div class="navigation dynHomepageNav">(...)</div>

dyn* 类在脚本中用作选择器,而 navigation 类用于样式设置。这样做的好处是,您可以让编码人员在不触及设计的情况下重构脚本,而设计人员可以更改模板而不必担心破坏功能。

然而,借助现代 Javascript 框架和 HTML5,您可以做得更好;对 ID 和类使用语义命名,使用这些 ID 和类应用样式,并对所有脚本挂钩使用 data-* 属性。示例:

<section class="navigation" data-hook="homepageNav">(...)</div>

您将使用类标识符设置样式:

.navigation {
  border: 1px dotted #9c9;
  padding: 12px;
}

并使用数据挂钩的脚本(使用 James Padolsey 的 jQuery 数据选择器属性):

$('section:data(hook="homepageNav")').fadeIn();

它可能不像旧的 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.:

<div class="navigation dynHomepageNav">(...)</div>

Where the dyn* class is used as a selector in scripts, while the navigation 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:

<section class="navigation" data-hook="homepageNav">(...)</div>

Which you will style using the class identifier:

.navigation {
  border: 1px dotted #9c9;
  padding: 12px;
}

And script using the data hook (using James Padolsey's data selector attribute for jQuery):

$('section:data(hook="homepageNav")').fadeIn();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文