Where in the HTML file should Jquery and Bootstrap be placed?

Where in the HTML file should Jquery and Bootstrap be placed?

Curious at to where I place my Jquery and Bootstrap files. They recommend that you always place at the bottom for performance purposes yet when I check sites that use Jquery/Bootstrap the majority of users always place them at the top. Also should I be loading my own JavaScript files before or after the bootstrap/Jquery files?
I take it that I load the my own css file first before the bootstrap file if I want to override some of their styling, is this correct and does the same apply to javascript files?


Solution 1:

Typically stylesheets in the head and scripts before the closing </body> tag:

    <link rel="stylesheet" href="bootstrap.css">
    <link rel="stylesheet" href="your-other-styles.css">
    <!-- content -->
    <script src="jquery.js"></script>
    <script src="bootstrap.js"></script>
    <script src="your-other-scripts.js"></script>

You’ll want files from vendors such as jQuery and Bootstrap to be included before yours. This means that:

  • CSS: You can override their styles with your own*
  • Scripts: You have access to any objects added to the global scope such as window (jQuery adds $,
    for example)
Related:  Multiple drop-down menus per button group?

However, if you require a script to be available before your content loads (such as Modernizr), then putting it in the <head> ensures it’s ready before any of your content.

Including scripts at the bottom ensures that the actual page content is loaded first; when the scripts are finally downloaded the content (DOM) will be ready for your scripts to manipulate.

* assuming your selector specificity is at least equal to those in your vendor CSS

Solution 2:

Bottom is best to place all your script references at the end of the page before </body>.It should look like below in normal page.

   <link href="path/to/file.css" rel="stylesheet" type="text/css">
   <script src="path/to/file.js" type="text/javascript"></script>

Although in some cases it may be necessary to load JavaScript before page load if any of your function need to access JavaScript before Page Load.Specially if you are working with JQuery UI And Bootstrap.

You can decorate your script tags with the defer attribute so that the browser knows to download your scripts after the HTML has been downloaded:

<script src="Jquery.js" type="text/javascript" defer="defer"></script>


  <script src="demo_async.js" async></script> 

When present, it specifies that the script will be executed asynchronously as soon as it is available.

Related:  Clicking a popover scrolls back to top of the page [Bootstrap and Django]


If you need script to access in page for use then script need to available before using it. Although it need to be sure the browser support defer=”defer”. Async is supported by all major browsers.

Javascript by default block any other parallel downloads. So if you have many tags in the head, calling on multiple external scripts will block the HTML from loading, thus greeting the user with a blank white screen, because no other content on your page will load until the java script files have completely loaded. Another advantage to load script in bottom is that any error caused by external script will not stop the page from Loading to browser.

Style css need to present in top <Head> to access in page.

Solution 3:

It really depends on what you want to achieve, but generally JS is placed at the bottom and CSS in your head section. Make sure that jquery library loads before Bootstrap’s JS library and your custom css file loads after Bootstrap’s CSS, so it will override. You can load Bootstrap’s CSS from their CND (or others, like cloudflare – for example http://cdnjs.com/libraries).

Related:  Showing a Google Map in a modal created with Twitter Bootstrap

Make sure you minify all that & activate compression and you shouldn’t experience any performance issues.

Advanced technics imply using the most important part of your CSS in your head area, then send the rest of the CSS in the bottom area. Or have your whole static content (CSS + JS) hosted on a CDN.