{"id":1311,"date":"2017-03-20T20:14:32","date_gmt":"2017-03-20T14:44:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1311"},"modified":"2017-03-29T11:28:09","modified_gmt":"2017-03-29T05:58:09","slug":"recommendation-include-css-javascript-invalid","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/recommendation-include-css-javascript-invalid\/","title":{"rendered":"[Solved -7 Answers] CSS &#8211; Is the recommendation to include CSS before JavaScript invalid"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM:<\/label><\/p>\n<p>In countless places online we have seen the recommendation to include CSS prior to JavaScript.<\/p>\n<p>The reasoning is generally, of this form:<\/p>\n<p>use the following page to test<\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3C!DOCTYPE%20html%3E%20%0A%3Chtml%3E%20%0A%3Chead%3E%20%0A%3Ctitle%3Etest%3C%2Ftitle%3E%0A%20%3Cscript%20type%3D\u2019text%2Fjavascript\u2019%3E%20var%20startTime%20%3D%20new%20Date()%3B%20%3C%2Fscript%3E%20%0A%3Clink%20href%3D%22http%3A%2F%2F10.0.0.50%3A8081%2Ftest.css%3Fdelay%3D500%22%20type%3D%22text%2Fcss%22%20rel%3D%22stylesheet%22%3E%20%0A%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22http%3A%2F%2F10.0.0.50%3A8081%2Ftest2.js%3Fdelay%3D400%26amp%3Bjsdelay%3D1000%22%3E%3C%2Fscript%3E%20%3C%2Fhead%3E%0A%20%3Cbody%3E%20%0A%3Cp%3E%20Elapsed%20time%20is%3A%0A%20%3Cscript%20type%3D\u2019text%2Fjavascript\u2019%3E%20document.write(new%20Date()%20-%20startTime)%3B%20%3C%2Fscript%3E%20%3C%2Fp%3E%20%3C%2Fbody%3E%20%3C%2Fhtml%3E%20%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>When we include the CSS first, the page takes 1.5 seconds to render:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-1322 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture4-1.png\" alt=\"\" width=\"749\" height=\"614\" \/><\/p>\n<p>When I include the JavaScript first, the page takes 1.4 seconds to render:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-1323 size-full\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture5-1.png\" alt=\"\" width=\"747\" height=\"616\" \/><\/p>\n<p style=\"top: 154px;\">[ad type=\u201dbanner\u201d]\n<ul>\n<li>What appears to be happening is that the JavaScript interpreter refuses to start until all the CSS is downloaded. So, it seems that having JavaScript includes first is more efficient as the JavaScript thread gets more run time.<\/li>\n<li>Is the recommendation to place CSS includes prior to JavaScript includes not correct?<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<ul>\n<li>We have always put the CSS <link href=\u201d\u2026\u201d>s before the JS <script src=\u201d\u2026\u201d>s because it\u2019s high time we do some actual research!<\/li>\n<li>Made sure there was no HTTP caching so the browser would have to do a full download each time a page is loaded.<\/li>\n<li>To simulate reality, we included jQuery and the H5BP CSS (so there\u2019s a decent amount of script\/CSS to parse)<\/li>\n<li>Set up two pages \u2013 one with CSS before script, one with CSS after script.<\/li>\n<li>Recorded how long it took for the external script in the <head> to execute<\/li>\n<li>Recorded how long it took for the inline script in the <body> to execute, which is analogous to DOMReady.<\/li>\n<li>Delayed sending CSS and\/or script to the browser by 500ms.<\/li>\n<li>Ran the test 20 times in the 3 major browsers.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<p>There are two main reasons to put CSS before JavaScript.<\/p>\n<ul>\n<li>Old browsers (Internet Explorer 6-7, Firefox 2, etc.) would block all subsequent downloads when they started downloading a script.<\/li>\n<li>So if you have a.js followed by b.css they get downloaded sequentially: first a then b. If you have b.css followed by a.js they get downloaded in parallel so the page loads more quickly.<\/li>\n<li>Nothing is rendered until all stylesheets are downloaded \u2013 this is true in all browsers. Scripts are different \u2013 they block rendering of all DOM elements that are below the script tag in the page.<\/li>\n<li>If you put your scripts in the HEAD then it means the entire page is blocked from rendering until all stylesheets and all scripts are downloaded.<\/li>\n<li>While it makes sense to block all rendering for stylesheets (so you get the correct styling the first time and avoid the flash of unstyled content FOUC), it doesn\u2019t make sense to block rendering of the entire page for scripts.<\/li>\n<li>Often scripts don\u2019t affect any DOM elements or just a portion of DOM elements. It\u2019s best to load scripts as low in the page as possible, or even better load them asynchronously.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<p>During the loading of your website, there are two scenarios that you would see:<\/p>\n<p><strong>CASE 1:<\/strong> white screen > unstyled website > styled website > interaction > styled and interactive website<\/p>\n<p><strong>CASE 2:<\/strong> white screen > unstyled website > interaction > styled website > styled and interactive website<\/p>\n<p>\u201cWhen using scripts that rely on the value of CSS style properties, it\u2019s important to reference external stylesheets or embed style elements before referencing the scripts\u201d.<\/p>\n<p>When the files are loaded in the wrong order (first JS, then CSS), any Javascript code relying on properties set in CSS files (for example the width or height of a div) won\u2019t be loaded correctly. It seems that with the wrong loading order, the correct properties are \u2018sometimes\u2019 known to Javascript (perhaps this is caused by a race condition?). This effect seems bigger or smaller depending on the browser used.<\/p>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<ul>\n<li>There are several things to consider here, and the relevance of these recommendations almost always become rules when you venture into high-caliber web development.<\/li>\n<li>The purpose of the \u201cput stylesheets at the top and scripts at the bottom\u201d rule is that, in general, it\u2019s the best way to achieve optimal progressive rendering, which is critical to the user experience.<\/li>\n<li>All else aside: assuming your test is valid, and you really are producing results contrary to the popular rules.<\/li>\n<li>Every website (and everything it takes to make the whole thing appear on a user\u2019s screen) is different and the Internet is constantly evolving.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<ul>\n<li>We include CSS files before Javascript for a different reason.<\/li>\n<li>If Javascript needs to do dynamic sizing of some page element (for those corner cases where CSS is really a main in the back) then loading the CSS after the JS is russing can lead to race conditions, where the element<\/li>\n<li>is resized before CSS styles are applied and thus looks weird when the styles finally kick in.<br \/>\nIf load the CSS beforehand we can guarantee that things run in the intended order and that the final layout is what we want it to be.<\/li>\n<\/ul>\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<p>The css will download parallel but js cant. Consider for the same case,<\/p>\n<p>Instead of having single css, take 2 or 3 css files and try it out these ways,<\/p>\n<p>1) css..css..js<br \/>\n2) css..js..css<br \/>\n3) js..css..css<\/p>\n<p>css..css..js will give better result than all others<\/p>\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<p>We have to keep in mind that new browsers have worked on their Javascript engines, their parsers and so on, optimizing common code and markup problems in a way that problems experienced in ancient browsers such as <=IE8 are no longer relevant, not only with regards to markup but also to use of JavaScript variables, element selectors etc.<\/p>\n<p>We can see in the not so distant future a situation where technology has reached a point where performance is not really an issue anymore.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM: In countless places online we have seen the recommendation to include CSS prior to JavaScript. The reasoning is generally, of this form: use the following page to test [pastacode lang=\u201dmarkup\u201d manual=\u201d%3C!DOCTYPE%20html%3E%20%0A%3Chtml%3E%20%0A%3Chead%3E%20%0A%3Ctitle%3Etest%3C%2Ftitle%3E%0A%20%3Cscript%20type%3D\u2019text%2Fjavascript\u2019%3E%20var%20startTime%20%3D%20new%20Date()%3B%20%3C%2Fscript%3E%20%0A%3Clink%20href%3D%22http%3A%2F%2F10.0.0.50%3A8081%2Ftest.css%3Fdelay%3D500%22%20type%3D%22text%2Fcss%22%20rel%3D%22stylesheet%22%3E%20%0A%3Cscript%20type%3D%22text%2Fjavascript%22%20src%3D%22http%3A%2F%2F10.0.0.50%3A8081%2Ftest2.js%3Fdelay%3D400%26amp%3Bjsdelay%3D1000%22%3E%3C%2Fscript%3E%20%3C%2Fhead%3E%0A%20%3Cbody%3E%20%0A%3Cp%3E%20Elapsed%20time%20is%3A%0A%20%3Cscript%20type%3D\u2019text%2Fjavascript\u2019%3E%20document.write(new%20Date()%20-%20startTime)%3B%20%3C%2Fscript%3E%20%3C%2Fp%3E%20%3C%2Fbody%3E%20%3C%2Fhtml%3E%20%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] When we include the CSS first, the page takes 1.5 seconds to render: When I include the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,275],"tags":[2580,2585,2586,2572,2581,2583,2577,1128,2582,2578,786,2573,2576,2575,2574,2584,2579],"class_list":["post-1311","post","type-post","status-publish","format-standard","hentry","category-css","category-javascript","tag-or-javascriptvoid0","tag-and-what-is-the-reasoning-behind-it","tag-change-an-html5-inputs-placeholder-color-with-css","tag-check-css-and-js-loading-order","tag-how-do-i-check-if-an-array-includes-an-object-in-javascript","tag-how-do-i-include-a-javascript-file-in-another-javascript-file","tag-how-do-javascript-closures-work","tag-how-to-check-whether-a-string-contains-a-substring-in-javascript","tag-how-to-disable-text-selection-highlighting-using-css","tag-how-to-efficiently-count-the-number-of-keysproperties-of-an-object-in-javascript","tag-is-it-possible-to-apply-css-to-half-of-a-character","tag-is-the-recommendation-to-include-css-before-js","tag-pro-sharepoint-2013-branding-and-responsive-web-development","tag-professional-xml","tag-the-web-application-hackers-handbook-finding-and-exploiting","tag-what-does-use-strict-do-in-javascript","tag-which-href-value-should-i-use-for-javascript-links"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1311"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1311\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}