我一直严重依赖CSS的一个网站,我正在工作。现在,所有的CSS样式都是在每个标签的基础上应用的,所以现在我试图将它移动到更多的外部样式,以帮助任何未来的变化。

但现在的问题是,我已经注意到我得到了一个“CSS爆炸”。我很难决定如何在CSS文件中最好地组织和抽象数据。

我在网站中使用了大量的div标签,从一个基于表格的网站。我得到了很多这样的CSS选择器:

div.title {
  background-color: blue;
  color: white;
  text-align: center;
}

div.footer {
  /* Styles Here */
}

div.body {
  /* Styles Here */
}

/* And many more */

这还不算太糟,但由于我是初学者,我想知道是否可以就如何最好地组织CSS文件的各个部分提出建议。我不想在我的网站上的每个元素都有一个单独的CSS属性,我总是希望CSS文件是相当直观和易于阅读的。

我的最终目标是使它易于使用CSS文件,并证明他们的力量,以提高web开发的速度。这样,将来可能在这个网站上工作的其他人也将开始使用良好的编码实践,而不必像我一样学习。


当前回答

你应该看看边界元法。

理论

BEM试图提供一组用于组织和命名css选择器的指令,以使选择器更具可重用性和模块化,并避免选择器之间的冲突,这种冲突通常会导致意大利面条式的代码和特定的问题。

如果正确使用,它实际上有一些非常积极的影响。

当样式被添加到元素中时,它们的作用是您所期望的 样式不会泄露,并且只影响它们所添加的内容 样式与文档结构完全解耦 不同的风格不需要被强迫凌驾于彼此之上

BEM works well with SASS to bring an almost object oriented style to CSS. You can build modular files, that handle the display of a single UI element and contain variables, such as colours and 'methods' such as how internal elements are handled. While a hardcore OO programmer might balk at this idea, in fact, the applied concepts bring in a lot of the nicer parts of OO structures, such as modularity, loose coupling and tight cohesion and context free re-usability. You can even build in a way that looks like an encapsulated object by using Sass and the & operator.

更多来自Smashing杂志的深入文章可以在这里找到;还有一个来自CCS Wizardry的Harry Roberts(任何参与css的人都应该读一下他的书)。

在实践中

我已经用过它几次了,还用过SMACSS和OOCSS,这意味着我也可以对它们进行比较。我也在一些大混乱中工作过,通常是我自己没有经验的创作。

当我在现实世界中使用BEM时,我用一些额外的原则来增强这种技术。我利用实用工具类-一个很好的例子是包装类:

.page-section {
    width: 100%;
}
@media screen and (min-width: 1200px) {
    margin: 0 auto;
    width: 1200px;
}

我也有些依赖于级联和特异性。在这里,BEM模块将是.primary-box,而.header将是特定重写的上下文

.header {
  .primary-box {
    color: #000;
  }
}

(我尽我最大的努力使所有的东西都是通用的和上下文无关的,这意味着一个好的项目几乎所有的东西都在可重用的模块中)

我想说的最后一点是,无论你的项目看起来多么小,多么简单,你都应该从一开始就这么做,原因有二:

项目越来越复杂,所以打下良好的基础是很重要的,包括CSS 即使是一个看起来很简单的项目,因为它是建立在WordPress上的,JavaScript很少,但在CSS中仍然非常复杂——好吧,你不需要做任何服务器端编码,所以这部分很简单,但是宣传册前端有20个模块和每个模块的3个变体:你有一些非常复杂的CSS !

Web组件

在2015年,我们开始关注Web组件。我对这些还不太了解,但它们希望将所有前端功能放在自包含的模块中,有效地尝试将各种原则从BEM应用到前端作为一个整体,并将分散但完全耦合的元素(如DOM片段、Js (MVC)和CSS)组合在一起,这些元素都构建相同的UI小部件。

通过这样做,他们b将解决css中存在的一些原始问题,我们试图用BEM等东西来解决这些问题,同时使其他一些前端架构更加合理。

这里有一些进一步的阅读材料,还有一个框架聚合物,非常值得一看

最后

我也认为这是一个优秀的、现代的css最佳实践指南——专门为防止大型css项目变得混乱而设计。我尽量遵循其中的大部分。

其他回答

我的回答是高水平的,针对你刚才提到的高水平的关切。也许你可以通过一些低级的组织技巧和调整来使它更漂亮,但这些都不能解决方法上的缺陷。有几个因素会影响CSS的爆炸。显然是网站的整体复杂性,但还有像命名语义、CSS性能、CSS文件组织和可测试性/可接受性等问题。

在命名语义方面,您似乎走在了正确的道路上,但还可以更进一步。重复出现在站点上而没有进行结构修改的HTML部分(称为“模块”)可以被视为选择器根,从那里您可以相对于根确定内部布局的范围。这是面向对象的CSS的基本原则,您可以在Yahoo工程师的演讲中阅读/观看更多关于它的内容。

需要注意的是,这种干净的方法可能与性能问题相反,它倾向于基于id或标记名的短选择器。找到平衡取决于你,但除非你有一个庞大的网站,这应该只是一个指南在你的脑后提醒你保持你的选择器简短。这里有更多关于性能的信息。

最后,您将为整个网站使用一个CSS文件,还是多个文件(单个基本文件用于每个页面或-section文件)?单个文件对于性能来说更好,但是对于多个团队成员来说可能更难理解/维护,并且可能更难测试。对于测试,我建议您使用一个CSS测试页面,其中包含所有受支持的CSS模块,以测试冲突和意外级联。

或者,您也可以采用多文件方法,将CSS规则作用于一个页面或一个部分。这需要浏览器下载多个文件,这是一个性能问题。您可以使用服务器端编程动态地指定和聚合(并缩小)CSS文件到单个文件中。但是,由于这些文件是分开的,对它们的测试也是分开的,因此可能会在页面/部分之间引入不一致的外观。因此测试变得更加困难。

由你来分析客户的具体需求,并相应地平衡这些问题。

你应该看看边界元法。

理论

BEM试图提供一组用于组织和命名css选择器的指令,以使选择器更具可重用性和模块化,并避免选择器之间的冲突,这种冲突通常会导致意大利面条式的代码和特定的问题。

如果正确使用,它实际上有一些非常积极的影响。

当样式被添加到元素中时,它们的作用是您所期望的 样式不会泄露,并且只影响它们所添加的内容 样式与文档结构完全解耦 不同的风格不需要被强迫凌驾于彼此之上

BEM works well with SASS to bring an almost object oriented style to CSS. You can build modular files, that handle the display of a single UI element and contain variables, such as colours and 'methods' such as how internal elements are handled. While a hardcore OO programmer might balk at this idea, in fact, the applied concepts bring in a lot of the nicer parts of OO structures, such as modularity, loose coupling and tight cohesion and context free re-usability. You can even build in a way that looks like an encapsulated object by using Sass and the & operator.

更多来自Smashing杂志的深入文章可以在这里找到;还有一个来自CCS Wizardry的Harry Roberts(任何参与css的人都应该读一下他的书)。

在实践中

我已经用过它几次了,还用过SMACSS和OOCSS,这意味着我也可以对它们进行比较。我也在一些大混乱中工作过,通常是我自己没有经验的创作。

当我在现实世界中使用BEM时,我用一些额外的原则来增强这种技术。我利用实用工具类-一个很好的例子是包装类:

.page-section {
    width: 100%;
}
@media screen and (min-width: 1200px) {
    margin: 0 auto;
    width: 1200px;
}

我也有些依赖于级联和特异性。在这里,BEM模块将是.primary-box,而.header将是特定重写的上下文

.header {
  .primary-box {
    color: #000;
  }
}

(我尽我最大的努力使所有的东西都是通用的和上下文无关的,这意味着一个好的项目几乎所有的东西都在可重用的模块中)

我想说的最后一点是,无论你的项目看起来多么小,多么简单,你都应该从一开始就这么做,原因有二:

项目越来越复杂,所以打下良好的基础是很重要的,包括CSS 即使是一个看起来很简单的项目,因为它是建立在WordPress上的,JavaScript很少,但在CSS中仍然非常复杂——好吧,你不需要做任何服务器端编码,所以这部分很简单,但是宣传册前端有20个模块和每个模块的3个变体:你有一些非常复杂的CSS !

Web组件

在2015年,我们开始关注Web组件。我对这些还不太了解,但它们希望将所有前端功能放在自包含的模块中,有效地尝试将各种原则从BEM应用到前端作为一个整体,并将分散但完全耦合的元素(如DOM片段、Js (MVC)和CSS)组合在一起,这些元素都构建相同的UI小部件。

通过这样做,他们b将解决css中存在的一些原始问题,我们试图用BEM等东西来解决这些问题,同时使其他一些前端架构更加合理。

这里有一些进一步的阅读材料,还有一个框架聚合物,非常值得一看

最后

我也认为这是一个优秀的、现代的css最佳实践指南——专门为防止大型css项目变得混乱而设计。我尽量遵循其中的大部分。

我所见过的对抗CSS膨胀的最好方法是使用面向对象的CSS原则。

甚至还有一个非常好的OOCSS框架。

一些意识形态与上面的答案相悖,但一旦你知道如何以面向对象的方式构建CSS,你就会发现它实际上有助于保持代码的精简和平均。

这里的关键是在你的网站中识别“对象”或构建块模式,并使用它们进行架构。

Facebook聘请了OOCSS的创建者Nicole Sullivan,以节省大量的前端代码(HTML / CSS)。是的,你实际上可以得到节省,不仅在你的CSS,但在你的HTML太,这是由它的声音,是非常有可能为你,因为你提到转换一个基于表格的布局到很多div的

另一个很好的方法在某些方面与OOCSS类似,就是从一开始就计划和编写可伸缩和模块化的CSS。Jonathan Snook写了一本关于SMACSS的书/电子书——CSS的可伸缩和模块化架构

让我给你介绍一些链接: 海量CSS的5个错误-(视频) 海量CSS的5个错误(幻灯片) CSS膨胀-(幻灯片)

很多时候,我看到有人把文件分成几个部分,在部分之间加上标题注释。

类似的

/* Headings and General Text */

.... stuff here for H1, etc..

/* Main page layout */

.... stuff here for layout page setup etc.

它工作得很好,可以让你很容易回头看看你正在做什么。

合理CSS的核心原则,摘自CSS重构:从仅追加到模块化CSS

Write in SASS. You'd be insane to forego the advantages of variables, mixins, and so on. Never use an HTML ID for styling; always use classes. HTML IDs, when used correctly, appear only once in the whole page, which is the complete opposite of re-usability — one of the most basic goods in sensible engineering. Moreover, it's really hard to override selectors containing IDs and often the only way to overpower one HTML ID is to create another ID, causing IDs to propagate in the codebase like the pests they are. Better to leave the HTML IDs for unchanging Javascript or integration test hooks. Name your CSS classes by their visual function rather than by their application-specific function. For example, say ".highlight-box" instead of ".bundle-product-discount-box". Coding in this way means that you can re-use your existing style-sheets when you role out side-businesses. For example, we started out selling law notes but recently moved into law tutors. Our old CSS classes had names like ".download_document_box", a class name that makes sense when talking about digital documents but would only confuse when applied to the new domain of private tutors. A better name that fits both existing services — and any future ones — would be ".pretty_callout_box". Avoid naming CSS classes after specific grid information. There was (and still is) a dreadful anti-pattern in CSS communities whereby designers and creators of CSS frameworks (cough Twitter Bootstrap) believe that "span-2" or "cols-8" are reasonable names for CSS classes. The point of CSS is to give you the possibility to modify your design without affecting the markup (much). Hardcoding grids sizes into the HTML thwarts this goal, so it is advised against in any project expected to last longer than a weekend. More on how we avoided grid classes later. Split your CSS across files. Ideally you would split everything into "components"/"widgets" and then compose pages from these atoms of design. Realistically though, you'll notice that some of your website pages have idiosyncrasies (e.g. a special layout, or a weird photo gallery that appears in just one article). In these cases you might create a file related to that specific page, only refactoring into a full-blown widget when it becomes clear that the element will be re-used elsewhere. This is a tradeoff, one that is motivated by practical budgetary concerns. Minimise nesting. Introduce new classes instead of nesting selectors. The fact that SASS removes the pain of repeating selectors when nesting doesn't mean that you have to nest five levels deep. Never over-qualify a selector (e.g. don't use "ul.nav" where ".nav" could do the same job.) And don't specify HTML elements alongside the custom class name (e.g."h2.highlight"). Instead just use the class name alone and drop the base selector (e.g. the previous example should be ".highlight"). Over-qualifying selectors doesn't add any value. Create styles for HTML elements (e.g. "h1") only when styling base components which should be consistent in the whole application. Avoid broad selectors like "header ul" because it's likely that you have to override them in some places anyway. As we keep saying, most of the time you want to use a specific, well-named class whenever you want a particular style. Embrace the basics of Block-Element-Modifier. You can read about it for example on here. We used it quite lightly, but still it helped us a lot in organising CSS styles.