Web Performance – Pros and Cons of Including JS/CSS Inside HTML

csshtmlhttpjavascriptperformance

What are the advantages and disadvantages of including JS/CSS inside the HTML instead of separated files?

Specifically concerned about performance issues, for example, if the CSS and JS are small and simple, and they are not going to be used in any other page, is it better to include them with the HTML so there is only a single request to the server? Or is this not a concern anymore with HTTP/2?

Which other technical considerations are important regarding this decision?

Best Answer

The reason why js and css will be seperated is for bundling and minification purposes.

  1. When you write them inside a HTML file, then the file size increases and takes time to load, which will degrade the performance.
  2. When the js and css are separated, we can bundle them into one and then minify them. Then there is no need to fetch big files. And also, the stylesheets and js can be cached in browser which will reduce the time to fetch those files.
  3. Also, if you want to develop in a single file you need to scroll multiple times to write the js and styles. Whereas for separate files, you can place them side by side and write which makes it so easy for development.

If the file is small, it doesn't matter much. It is more of a preference.

We do have some standards from w3c. You can read them here.