简体   繁体   English

为什么拥有外部样式表比内部样式表更好? 除了重用

[英]Why is it better to have stylesheets external than internal? Apart from reuse

I've been writing some css for a page as an intern, and I put the css in the header of the page. 我一直在为页面写一些CSS作为实习生,然后将CSS放在页面的标题中。 Instead of putting it in the sites main css file. 而不是将其放在站点的主CSS文件中。 One huge file. 一个巨大的文件。 The page has a lot of unique styles compared to the rest of the site. 与网站的其余部分相比,该页面具有许多独特的样式。 I got an email from the external contractor where I work. 我收到了我工作所在的外部承包商的电子邮件。 Asking that the styles be moved into the main file. 要求将样式移入主文件。 Is there any particular reason that I should do this. 有什么特别的原因我应该这样做。 I don't mind moving it into an external css file other than the main file. 我不介意将其移动到除主文件之外的外部CSS文件中。 I can't really see why this is as he said "standard best practice". 我真不明白为什么这就是他所说的“标准最佳实践”。

As far as I know the only difference is that external can be re used and i'd like to stick to my guns. 据我所知,唯一的区别是外部可以重复使用,我想坚持自己的看法。 But at the same time i'm keen to go with what this guy thinks, because he's more experienced. 但与此同时,我也热衷于理解这个家伙的想法,因为他更有经验。

If anybody can explain to me why using external stylesheets is best practice. 如果有人可以向我解释为什么使用外部样式表是最佳实践。 I very rarely see internal stylesheets on sites so I guess there must be some logic to this. 我很少在网站上看到内部样式表,因此我想对此应该有一定的逻辑。

Thanks! 谢谢!

In some cases, your page is going to have dynamic content. 在某些情况下,您的页面将具有动态内容。 Because of that, it cannot be cached. 因此,它不能被缓存。 By adding the css to the header, you are increasing that amount of bandwidth that must be used. 通过将css添加到标头中,可以增加必须使用的带宽量。 Having a separate stylesheet can reduce bandwidth consumption through caching. 拥有单独的样式表可以通过缓存减少带宽消耗。

Also, stylesheets can be loaded in parallel, so external stylesheets can speed up page loads. 另外,样式表可以并行加载,因此外部样式表可以加快页面加载速度。

Many pages reuse the same CSS. 许多页面重复使用相同的CSS。 Therefore the browser can cache the external stylesheet and reuse it on every page, which will result in lower overhead and faster page loads. 因此,浏览器可以缓存外部样式表并在每个页面上重复使用,这将导致较低的开销和更快的页面加载速度。

If the global CSS is inline, it's being loaded for every page and that's a lot of overhead and unnecessary lifting for the connection and browser. 如果全局CSS是内联的,则将为每个页面加载它,这将导致大量的开销,并且不必要增加连接和浏览器的负担。

However, there are good arguments for inlining some of your CSS, what you could classify as "critical" CSS. 但是,有很好的理由可以内联某些 CSS,也可以将其归类为“关键” CSS。 External CSS stylesheets are render-blocking, so it won't apply your styles until all of the CSS has been downloaded. 外部CSS样式表具有渲染功能,因此在所有CSS均已下载之前,它不会应用您的样式。 For large, global external CSS files, you'll sometimes notice a delay before the browser applies the styles (also known as FOUT [Flash of Unstyled Text]). 对于大型的全局外部CSS文件,有时您会注意到浏览器应用样式(也称为FOUT [无样式文本的Flash])之前的延迟。 A proposed solution is to inline the critical CSS that's visible above the fold so those styles are applied quickly. 提议的解决方案是将关键CSS内联在折叠上方,以便快速应用这些样式。 Then allow the browser to apply the rest of the CSS when the page is ready. 然后,在页面准备好后,允许浏览器应用其余的CSS。

FOUT is much more noticeable on mobile connections and in this day-and-age where mobile traffic increases daily, it's important to consider these practices to improve the mobile experience. FOUT在移动连接上更加引人注目,在当今移动流量每天都在增加的时代,重要的是要考虑这些做法以改善移动体验。

Addy Osmani wrote a great article "Detecting Critical CSS For Above-the-Fold" that'll give you a better understanding of the practice and its benefits: http://addyosmani.com/blog/detecting-critical-above-the-fold-css-with-paul-kinlan-video/ 阿迪·奥斯曼尼(Addy Osmani)撰写了一篇很棒的文章“检测出关键的CSS超出常规”,这将使您更好地理解这种做法及其好处: http : //addyosmani.com/blog/detecting-critical-above-the-用保罗金兰视频折叠css /

In addition to Franz's answer, separation of structure, logic, and display code is a best practice. 除了Franz的答案,将结构,逻辑和显示代码分离是一种最佳实践。 It's the driving force behind MVC - it makes for much more maintainable code. 这是MVC背后的驱动力-它使代码更具可维护性。

To format a web page or the theme for a blog there are 3 types of stylesheets available for you to use: External Stylesheet: An external stylesheet is a separate file with all your styles in it. 要设置网页或博客主题的格式,可以使用三种样式表:外部样式表:外部样式表是一个单独的文件,其中包含所有样式。 You link to it in the head section of your web page coding. 您可以在网页编码的开头部分链接到它。

<link rel="stylesheet" type="text/css" href="styles/stylesht.css">

This makes it easy to find all your stylesheets if you have multiple ones. 如果您有多个样式表,则可以轻松找到所有样式表。 Also, you can put an instruction in your robot.txt file instructing the search engine bots to stay out of the folder and not add these to their database. 另外,您可以在robot.txt文件中放置一条指令,以指示搜索引擎漫游器不要进入该文件夹,并且不要将其添加到其数据库中。


Internal Stylesheet : An internal stylesheet is added to the head of the page. 内部样式表:内部样式表已添加到页面的顶部。

<style type="text/css">
Your style types
</style>

Styles inside the internal stylesheet are only applied to the page they are actually coded into. 内部样式表中的样式仅应用于实际编码到的页面。 You see this quite often. 您经常看到这种情况。 A template is made with an internal stylesheet and as you recylce the template the style coding is inserted into each page's head section. 模板由内部样式表构成,在您重新使用模板时,样式代码将插入到每个页面的头部。 Some Wordpress plugins will do this too instead of inserting a link to an external stylesheet just for the plugin. 某些Wordpress插件也可以这样做,而不是仅为该插件插入指向外部样式表的链接。


Inline Style : An inline style is applied to an HTML element. 内联样式:内联样式应用于HTML元素。

<p style="color: #636954;">My sentence.</p>

This inline style changes the colour of my text to a green: 这种内联样式将文本的颜色更改为绿色:

暂无
暂无

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

相关问题 即使使用相同的选择器,外部样式表仍将覆盖内部样式表 - External stylesheets are overriding internal, even with the same selector 为什么我的外部样式表没有链接到移动设备上? - Why is my external stylesheets not linking on mobile device? 外部SVG中的样式表 - 是从缓存加载还是两次加载? - Stylesheets in external SVG - are they loaded from cache or twice? 为什么<h5>和<h6>字体大小小于<p>在大多数用户代理默认样式表中? - Why do <h5> and <h6> have smaller font sizes than <p> in most user agent default stylesheets? SVG模板,内部,外部重用的最佳场所? - SVG templates, best place to put them for reuse, internal, external? 为每篇文章创建一个文件还是为每篇文章重复使用一个文件更好? - Is it better to have a file for every article or reuse a single file for each article? 打开所有外部链接,并在除域之外的新标签页中打开 - Open all external links open in a new tab apart from a domain 在d3中加载内部JSON数据而不是从外部资源/文件加载以获取朝阳 - Loading internal JSON data rather than from an external resource / file for a sunburst in d3 加载 <head></head> 来自外部页面的内容,特别是来自外部页面的样式表 - Load <head> </head> content from external page, specifically the stylesheets from the external page 在PHP文件中包含外部样式表 - Including external stylesheets in a PHP file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM