简体   繁体   English

我可以将我的外部CSS文件放在html页的末尾以防止渲染阻塞吗?

[英]Can I put my external css files at the end of the html page to prevent render blocking?

I have done google page speed test. 我已经完成了Google页面速​​度测试。 In the report it suggests me to remove render blocking css resources in above the fold content. 在报告中,它建议我删除折叠内容上方的渲染阻止CSS资源。 I came to know that I have 14 external CSS files which causes delay in loading my page. 我知道我有14个外部CSS文件,这会导致页面加载延迟。 So can I put my CSS files at the end of the HTML tag or is there any other way to do it? 那么我可以将CSS文件放在HTML标记的末尾,还是有其他方法可以呢?

Above the fold content means, what you presently seeing on the screen without scrolling down.the styles for the above the fold content is necessary for the initial load, other styles can be download later.so what you can do, have two style sheets one contain the styles for the initial page load and the other style sheet will have the rest of the styles.the second style sheet you can include through javascript 折叠内容上方表示您当前在屏幕上看到的内容而无需向下滚动。折叠内容上方的样式对于初始加载而言是必需的,其他样式可以在以后下载。因此您可以做的是拥有两个样式表包含用于初始页面加载的样式,其他样式表将具有其余样式。您可以通过javascript包含的第二个样式表

How to optimize you css files for better performance 如何优化CSS文件以获得更好的性能

  1. instead of having so many external files ,combine those in to a single file.this will reduce the number of request to the server 而不是拥有太多外部文件,而是将这些文件合并到一个文件中。这将减少对服务器的请求数量

  2. Minify your css files (This will remove the unwanted space) 缩小您的CSS文件(这将删除不需要的空间)

  3. Remove unused css. 删除未使用的CSS。 you might have used a front end framework like twitter bootstrap and foundation.there are lot of addons to identify the list of unused css. 您可能使用了诸如twitter bootstrap和foundation之类的前端框架。有很多插件可以识别未使用的CSS列表。

Doing what you suggested will load a site which is not styled and then after the css has been loaded the site will get styling which will give a jerky / bad UX. 按照您的建议进行操作将加载未样式化的网站,然后在加载CSS之后,该网站将获得样式化,这将给用户带来生涩/不良的用户体验。 Instead you can inline (or add inside head tag) the first fold content, ie the CSS that is required to style the first part of the website you see without scrolling. 取而代之的是,您可以内联(或在head标签内添加)第一折内容,即,无需滚动即可为您看到的网站第一部分设置样式的CSS。 Then in production mode merge all your css files into a single file with a dns-prefetch if your source is a trusted source. 然后在生产模式下,如果您的源是受信任的源,则将所有css文件合并为具有dns-prefetch的单个文件。 Also if your are using a react app bundled via webpack you can try implementing code splitting to load bundles pertaining to the route. 另外,如果您正在使用通过Webpack捆绑的react应用,则可以尝试实施code splitting以加载与该路线有关的捆绑。 Split your vendor css/js files and your app files so that browser cache can be leveraged. 分割供应商的css / js文件和应用程序文件,以便可以利用浏览器缓存。

Udacity has this free course here which can give you a better idea of how to optimise your page speed time. Udacity 在这里有此免费课程,可以使您更好地了解如何优化页面速度。

You can do that and it will load faster, but while it is being loaded, it will be unstyled. 您可以执行此操作,它将加载得更快,但是在加载时,将不设置样式。 If this is ok for you, then go ahead and do it. 如果您认为可以,请继续进行。 However, if you ask me you can categorize your CSS styles/files to urgent and not so urgent. 但是,如果您问我,您可以将CSS样式/文件分类为紧急而不是紧急。 If your page loads for like 5 seconds and meanwhile everything is unstyled, then you might think that the result is not very good in terms of UX. 如果您的页面加载了大约5秒钟,同时所有内容都未设置样式,那么您可能会认为就UX而言,结果不是很好。 So, pick up the styles you absolutely want to load at the start and put the rest at the end. 因此,请从一开始就选择绝对要加载的样式,然后将剩下的样式放在最后。

Putting it at the end of the file would cause the browser to first display all your html wihtout any styling. 将其放在文件末尾会导致浏览器首先显示所有HTML样式。 Which I personally find unsatisfactory. 我个人觉得不满意。

Try to rather merge your css files together to use only one big minified file so the browser only needs to load one file for css. 尝试将CS​​S文件合并在一起,以仅使用一个较大的缩小文件,因此浏览器仅需要为CSS加载一个文件。

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

相关问题 如何在 HTML CSS 中使我的滚动始终位于页面的末尾。 我希望我的卷轴总是在页面的末尾 - How do I make my scroll always in the end of the page in HTML CSS. I want my scroll to always be at the end of the page 我需要在我的html页面中插入一个外部Javascript文件,其中jquery与我的css文件有关 - I need to insert an external Javascript file into my html page where jquery is involved with my css file 如何将外部CSS / JS文件提供给NodeJS应用显示的html文件? - How can I serve external css/js files to html files displayed by a NodeJS app? 如何防止引导程序模式中加载的html影响我的页面html? - How can I prevent the html loaded within my bootstrap modal from affecting my page html? 是否有可以放入 HTML 页面的指令来防止或限制 JavaScript? - Is there a directive that you can put in an HTML page to prevent or limit JavaScript? 当我需要从其他资源向页面添加HTML代码时,如何防止CSS泄漏 - How to prevent CSS leaking when I need to add HTML codes from other resource to my page 单击按钮时如何防止我的页面呈现 - How to prevent my Page render when I click on the button 如何将Highchart放到html页面 - How can I put the Highchart to the html page 如何加载外部html文件? - How can I load external html files? 我应该在哪里放置JavaScript-页面或外部文件? - Where should I put my JavaScript - page or external file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM