Why is ` – takendarkk Oct 11 '18 at 15:50

  • are you asking about style or script tag? the reason script tag is at bottom of body is to make sure dom finishes loading before the script runs. – Chris Li Oct 11 '18 at 15:51
  • Is this taken off of w3schools? https://www.w3schools.com/html/html_css.asp – slee423 Oct 11 '18 at 15:52
  • I am asking about – jsstackguru Oct 11 '18 at 15:53
  • Some developers put their scripts at the bottom of the as a way to defer loading the script until the all the HTML above it has been parsed. – Tom O. Oct 11 '18 at 16:01
  • 2 Answers2

    3

    The head contains general information about the whole document.

    The body contains the content.

    A stylesheet isn't the content, it is information about how the content should look.


    A script element going at the end of the body is a performance hack.

    It is allowed in the body in the first place because it can inject content directly into its current position (with document.write), although that isn't considered good practice today.

    I would typically put the <script> in the <head>, but set the async attribute and use a DOMContentLoaded event listener to make it run when the content has loaded.

    Quentin
    • 914,110
    • 126
    • 1,211
    • 1,335
    0

    You can put in head also, but it is usually put at the bottom of the body so the styles and page content loads before and then scripts which will result in faster load in time

    V. Alen
    • 88
    • 8