3

Looking at the Google HTML/CSS Style Guide, I don't see anything specific. It seems like putting the CSS and JavaScript in the HTML file would perform better than loading external files.

For production Websites, should I include everything in 1 file, or no?

8
  • You definitely shouldn't put big CSS or JavaScript files in the HTML document, because that prevents caching. Commented Jul 3, 2016 at 21:15
  • That's actually a good point, assuming it's true. Commented Jul 3, 2016 at 21:20
  • Very similar question on Programmers Stack Exchange: Is it better to put the JS code on the html file or in an external file? Commented Jul 3, 2016 at 21:22
  • It's only true for inline JavaScript @Gothdo Commented Jul 3, 2016 at 21:24
  • Why down voted? Thats actually interesting guestion.. I think answer is related to caching somehow. But for pure SPA, inlining everything might make sense. Commented Jul 3, 2016 at 21:33

2 Answers 2

2

Usually when you develop, you split up your code in your project tree. So you have a large amount of js, and maybe css / sass / less files.. which represents smaller components for your project.

When you are going to launch your project to production, you usually have build program which concatenates all the files into a single bundle and this bundle is also minified, for faster loading like bootstrap.min.js for a example.

Take a look (google) for some production ready packer like:

  • brunch
  • webpack
Sign up to request clarification or add additional context in comments.

1 Comment

@lexith bro, please this answer should cover how its usually done, the why question is definitive good, but to cover all aspects it would make it less understandable for a newbie, IMO
-2

It depends on your preference . but in most cases you would like to write code in separate files because when you work on big projects it can start to get confusing when you have css ,js and html in the same file . plus the difference it will make is minimal to none.

1 Comment

I agree, when developing. However, when ready to publish to production, what is best practice? For example, I know that it is best practice to pack all JavaScript into a single file vs loading multiple external scripts. So, why not just put all the JS in with the HTML? Caching seems to be a good reason why to use an external file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.