拥有一个包含几乎每个页面都要使用的样式元素的怪物.css文件有什么好处吗?

我在想,为了便于管理,我想把不同类型的CSS拉出到几个文件中,并包括我的主<link />是坏的吗?

我觉得这样更好

positions.css buttons.css tables.css copy.css

vs.

site.css

你见过用一种方法做和用另一种方法做有什么问题吗?


当前回答

I've created a systematic approach to CSS development. This way I can utilize a standard that never changes. First I started with the 960 grid system. Then I created single lines of css for basic layouts, margins, padding, fonts and sizes. I then string them together as needed. This allows me to keep a consistent layout across all of my projects and utilize the same css files over and over. Because they are not specific. Here's an example: ----div class="c12 bg0 m10 p5 white fl"/div--- This means that the container is 12 columns across, utilizes bg0 has margins of 10px padding of 5 the text is white and it floats left. I could easily change this by removing or adding a new - What I call a "light" style- Instead of creating a single class with all these attributes; I simply combine the single styles as I code the page. This allows me to create any combination of styles and does not limit my creativity or cause me to create a massive number of styles that are similar. Your style sheets become a lot more manageable, minimized and allow you to re-use it over and over. This method I have found to be fantastic for rapid design. I also no longer design first in PSD but in the browser which also saves time. In addition because I have also created a naming system for my backgrounds and page design attributes I simply change out my image file when creating a new project.(bg0 = body background according to my naming system) That means that if I previously had a white background with one project simply changing it to black simply means that on the next project bg0 will be a black background or another image..... I have not found anything wrong with this method yet and it seems to work very well.

其他回答

SASS和LESS使这一切都成为一个有争议的问题。开发人员可以建立有效的组件文件,并在编译时将它们全部组合起来。在SASS中,您可以在开发过程中关闭压缩模式以方便阅读,并在生产过程中切换回来。

http://sass-lang.com http://lesscss.org

最后,不管你使用什么技术,一个简化的CSS文件就是你想要的。更少的CSS,更少的HTTP请求,更少的服务器需求。

我更喜欢在开发过程中使用多个CSS文件。这样管理和调试就容易得多。但是,我建议你在部署时使用像YUI Compressor这样的CSS缩小工具,它可以将你的CSS文件合并成一个整体文件。

我通常有一些CSS文件:

一个用于重置和全局样式的“全局”CSS文件 “模块”特定的CSS文件用于逻辑分组的页面(可能是结帐向导或其他东西中的每个页面) 用于覆盖页面的“page”特定CSS文件(或者,将其放在单个页面的块中)

我真的不太关心CSS文件的多页请求。大多数人都有不错的带宽,我相信还有其他优化会比将所有样式组合到一个单独的CSS文件中产生更大的影响。在速度和可维护性之间进行权衡,我总是倾向于可维护性。YUI压缩机听起来很酷,但我可能要检查一下。

对于页面的加载时间来说,只有一个CSS文件更好,因为这意味着更少的HTTP请求。

拥有几个小的CSS文件意味着开发更容易(至少,我是这么认为的:应用程序的每个模块拥有一个CSS文件会让事情变得更容易)。

所以,这两种情况都有很好的理由……

一个可以让你得到两个想法的最好的解决方案是:

使用几个小的CSS文件进行开发 也就是更容易发展 要为您的应用程序建立一个构建过程,将这些文件“组合”成一个文件 顺便说一句,这个构建过程也可以缩小那个大文件 这显然意味着你的应用程序必须有一些配置,允许它从“多文件模式”切换到“单文件模式”。 而要在生产中使用,只有大文件 即更快的加载页面

也有一些软件可以在运行时组合CSS文件,而不是在构建时;但是在运行时这样做意味着消耗更多的CPU(并且显然需要一些缓存机制,以避免太频繁地重新生成大文件)

像Sass或LESS这样的CSS编译器是一个很好的方法。通过这种方式,你可以为网站交付一个最小化的CSS文件(这将比普通的单个CSS源文件小得多,速度也快得多),同时保持最好的开发环境,所有东西都整齐地分割成组件。

Sass和LESS具有变量、嵌套和其他方法的附加优势,使CSS更容易编写和维护。强烈推荐。我个人现在使用Sass (SCSS语法),但以前使用LESS。两者都很棒,好处相似。一旦你用编译器编写了CSS,你就不太可能不需要编译器。

http://lesscss.org

http://sass-lang.com

如果你不想在Ruby上浪费时间,这个Mac版的LESS编译器非常棒:

http://incident57.com/less/

或者你可以使用CodeKit(同样的人):

http://incident57.com/codekit/

WinLess是一个用于编译LESS的Windows GUI

http://winless.org/