拥有一个包含几乎每个页面都要使用的样式元素的怪物.css文件有什么好处吗?
我在想,为了便于管理,我想把不同类型的CSS拉出到几个文件中,并包括我的主<link />是坏的吗?
我觉得这样更好
positions.css buttons.css tables.css copy.css
vs.
site.css
你见过用一种方法做和用另一种方法做有什么问题吗?
拥有一个包含几乎每个页面都要使用的样式元素的怪物.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.
其他回答
你想要两个世界。
您需要多个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。
捆绑的样式表可以节省页面加载性能,但是样式越多,浏览器在页面上呈现动画的速度就越慢。这是由于大量未使用的样式可能不在您所在的页面上,但浏览器仍然需要计算。
参见: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">
像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/
我更喜欢多个CSS文件。这样就可以更容易地根据需要将“皮肤”换入和换出。单一文件的问题在于,它可能会失控,难以管理。如果你想要蓝色背景但又不想改变按钮怎么办?只要改变你的背景文件。等。
单个CSS文件的优点是传输效率高。每个HTTP请求意味着每个请求的文件都有一个HTTP头响应,这需要占用带宽。
我把我的CSS作为一个PHP文件,在HTTP头中带有“text/ CSS”mime类型。这样,我可以在服务器端拥有多个CSS文件,并在用户请求时使用PHP include将它们推入单个文件。每个现代浏览器都会接收包含CSS代码的.php文件,并将其作为. CSS文件处理。