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

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

我觉得这样更好

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

vs.

site.css

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


当前回答

你想要两个世界。

您需要多个CSS文件,因为浪费您的理智是一件可怕的事情。

同时,最好有一个单一的大文件。

解决方案是使用某种机制将多个文件合并为一个文件。

一个例子是

<link rel="stylesheet" type="text/css" href="allcss.php?files=positions.css,buttons.css,copy.css" />

然后,allcss.php脚本处理连接文件并传递它们。

理想情况下,脚本会检查所有文件的mod日期,如果其中任何一个文件发生变化,则创建一个新的组合,然后返回该组合,然后检查if - modified HTTP头,以便不发送多余的CSS。

这让你两全其美。也适用于JS。

其他回答

你可以只使用一个css文件来提高性能,然后像这样注释掉部分:

/******** Header ************/
//some css here

/******* End Header *********/


/******** Footer ************/
//some css here

/******* End Footer *********/

etc

我使用Jammit来处理我的css文件,并使用许多不同的文件来提高可读性。 在部署到生产环境之前,Jammit完成了所有合并和压缩文件的繁琐工作。 这样,我有许多文件在开发中,但只有一个文件在生产中。

捆绑的样式表可以节省页面加载性能,但是样式越多,浏览器在页面上呈现动画的速度就越慢。这是由于大量未使用的样式可能不在您所在的页面上,但浏览器仍然需要计算。

参见:https://benfrain.com/css-performance-revisited-selectors-bloat-expensive-styles/

捆绑样式表的优点: -页面加载性能

捆绑样式表的缺点: -较慢的行为,这可能会导致滚动,互动,动画,

结论: 为了解决这两个问题,对于生产来说,理想的解决方案是将所有css捆绑到一个文件中保存在http请求中,但使用javascript从该文件中提取您所在页面的css并使用它更新头部。

为了了解每个页面需要哪些共享组件,并降低复杂性,最好声明这个特定页面使用的所有组件,例如:

<style href="global.css" rel="stylesheet"/>
<body data-shared-css-components="x,y,z">

以下是最好的方法:

创建一个包含所有共享代码的通用CSS文件 将所有特定页面的CSS代码插入到同一页面中,在标签上或对每个页面使用属性style=""

通过这种方式,您只有一个包含所有共享代码的CSS和一个HTML页面。 顺便说一下(我知道这不是正确的主题),你也可以在base64编码你的图像(但你也可以用你的js和CSS文件)。通过这种方式,您可以将更多的HTTP请求减少到1。

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.