简体   繁体   English

每个网页都有单独的CSS

[英]Separate css for every web page

I am developing a website in asp.net 4.0. 我正在开发一个asp.net 4.0的网站。 While designing I have a question 设计时我有一个问题

  • Is it good to have separate css for every web page? 每个网页都有单独的CSS吗?

OR 要么

  • Have all styles in one css? 在一个CSS中有所有样式吗?

Please tell me what is the best solution? 请告诉我什么是最好的解决方案?

It's best to keep all rules in one CSS file, since this file will be cached for all subsequent requests. 最好将所有规则保存在一个CSS文件中,因为此文件将为所有后续请求缓存。

Unless you have a ton of CSS (ie. more than a few hundred kBs), this should not impact CSS engine performance, but it will significantly speed up loading of pages since requests won't have to be repeated for each page loaded, which will be your single greatest load time gain apart from your HTML page being generated. 除非你有大量的CSS(即超过几百KB),否则这不会影响CSS引擎性能,但它会显着加快页面的加载速度,因为不必为每个加载的页面重复请求,除了生成HTML页面之外,它将是您最大的加载时间。

EDIT : You should think about implementing compile-time (or on-demand) concatenation; 编辑 :您应该考虑实现编译时(或按需)连接; that way you can have multiple CSS files that will be served as a single file to your clients. 这样,您就可以拥有多个CSS文件,这些文件将作为单个文件提供给您的客户端。

It depends on the size of your code base. 这取决于代码库的大小。

Typically global rules should all be in one file, unless you are dealing with a massive code base where organizing them into separate files makes sense (but still included globally on every request). 通常,全局规则都应该在一个文件中,除非您正在处理大量的代码库,将它们组织成单独的文件是有意义的(但仍然包含在每个请求的全局中)。

Usually page specific CSS should be put in its own file and only included on pages that need it. 通常,页面特定的CSS应该放在其自己的文件中,并且仅包含在需要它的页面中。

Generally speaking with large code bases, you would have a minifier and build process that concatenates all your global css into one file and includes that in production environment. 一般来说,使用大型代码库,您将拥有一个缩小器和构建过程,该过程将所有全局css连接到一个文件中,并将其包含在生产环境中。 This can be difficult to debug, so in development its better to keep them as separate files. 这可能很难调试,因此在开发中最好将它们保留为单独的文件。

Aside from the caching and minimizing benefits outlined in other answers, consider that your CSS is essentially assigning a theme to your website or web application. 除了在其他答案中概述的缓存和最大程度地减少收益外,请考虑CSS实际上是为您的网站或Web应用程序分配了主题 If you think about how a theme works in your favorite media player or web browser, when you apply that theme, it generally affects the entire application, not just one part of it. 如果考虑主题在您喜欢的媒体播放器或Web浏览器中的工作方式,则在应用该主题时,它通常会影响整个应用程序,而不仅仅是整个应用程序的一部分。

Therefore, if you think of your CSS as a theme, it then becomes easier for your designers to then change the look and feel of your site without ever having to touch a single line of HTML. 因此,如果您将CSS视为主题,那么设计人员就可以更轻松地更改网站的外观,而无需触及单行HTML。 Since a good design practice is to keep your HTML, JavaScript, and CSS completely separate, it makes it easier to make changes to one without having to worry about changes to the others. 由于良好的设计习惯是将HTML,JavaScript和CSS完全分开,因此可以轻松更改其中一个,而不必担心更改其他内容。 This reduces chances for bugs, since things that don't change remain stable. 因为不会改变的东西保持稳定,所以这减少了错误的机会。

What's more, your designers only have to make changes to 1 file in order to create a new theme, which makes their job easier. 而且,您的设计师只需更改1个文件即可创建一个新主题,这使他们的工作更加轻松。 It makes your job easier because you don't have to go through and fix a bunch of broken HTML and JavaScript because CSS experts had to make changes to code that might not be their area of expertise. 它使您的工作更轻松,因为您不必经历并修复一堆破碎的HTML和JavaScript,因为CSS专家必须对可能不是他们专业领域的代码进行更改。

A good example of this practice is Stack Exchange. 这种做法的一个很好的例子是Stack Exchange。 Stack Overflow, Server Fault , Super User , and Stack Apps all run on the same Q&A engine, but each one has a slightly different theme. 堆栈溢出, 服务器故障超级用户堆栈应用程序都在同一个Q&A引擎上运行,但每个都有一个略有不同的主题。 To create a completely new Q&A site, the frontend changes pretty much involve only CSS changes. 要创建一个全新的问答网站,前端变化几乎只涉及CSS更改。 This is how Stack Exchange can get away with having only 1 designer, Jin , who manages the designs of every single Q&A site on the network. 这就是Stack Exchange如何让只有一名设计师Jin来摆脱困境的人, Jin管理网络上每个Q&A站点的设计。 What makes his job easier is the standardization and simplicity, and following the "don't repeat yourself" rule of thumb. 让他的工作更轻松的是标准化和简洁性,并遵循“不要重复自己”的经验法则。

From looking at the browser source, you can see there is only 1 CSS file: 通过查看浏览器源,您可以看到只有1个CSS文件:

    <link rel="stylesheet" type="text/css" 
        href="http://cdn.sstatic.net/stackoverflow/all.css?v=52aab16f8140">

no, it is not good to keep seperate css for every web page. 不,为每个网页保持单独的css是不好的。 a professional never do this. 专业人士永远不会这样做。 but u can use two or three separate css for different browsers.. 但是你可以为不同的浏览器使用两个或三个单独的css ..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM