What causes the code following the </html> tag to be shifted in front of the </body> tag? Is there a noticeable improvement in performance

After exploring various Stack Overflow posts like this particular SO question, I came across an unconventional Google recommendation on CSS optimization. This recommendation suggested deferring CSS loading with markup that looked like this:


        <div class="blue">Hello, world!</div>
    </body>
</html>
<noscript><link rel="stylesheet" href="small.css"></noscript>

This approach seemed puzzling due to its excessive nature, confusion, invalid HTML structure, and the claim of maintaining CSS rules application order through JavaScript, although no JavaScript was present in the example. This led me to wonder:

When examining their sample code output, all content following the </html> tag moved just before </body>. So the burning question is... WHY?

  1. What prompted this shift? It seems most major browsers automatically reposition any code after </html> prior to </body>. My search for documentation or standards on this matter yielded little results.

  2. Why would Google advocate for this method? Is there a tangible performance benefit to this practice? One might assume placing it before </body> initially would suffice. (and as BoltClock's insightful explanation suggests, is there concrete evidence supporting improved performance from this technique?)

This behavior was observed in IE11, Firefox 26, Chrome 32.x, and Windows Safari 5.1.7. The inspected HTML appeared as follows:


        <div class="blue">Hello, world!</div>
        <noscript><link rel="stylesheet" href="small.css"></noscript>
    </body>
</html>

Inserting additional code post </html> rendered the same outcome.

This phenomenon evokes memories of other peculiar error-corrections, such as how browsers render <image> tags as <img> (reference)...

UPDATE: In my testing, I created this URL for NON-deferred CSS and also this URL for deferred CSS (based on my interpretation of the mentioned article)...

Answer №1

It's quite peculiar, isn't it? It's against the rules to include any elements after the closing </html> tag because the html element serves as the root of an HTML document.

  1. This is a scenario involving HTML rather than XHTML. Unlike in XHTML where errors would result in failure, browsers handling HTML will simply move anything located at the end of a document (excluding comments and whitespace) to the end of the body section and proceed as though everything is normal.

    Prior to the inception of HTML5, there were no established protocols for error correction in such circumstances due to the fact that having any elements beyond the root element was deemed invalid. With HTML5, almost all error handling scenarios are addressed in detail in section 8.2.5. Notably, if there is an unexpected token (not including DOCTYPE, comment, or </html>) during the "after body" or "after after body" insertion modes, the parser will switch to the "in body" mode to process the token, leading to its insertion into the body content per the respective insertion modes.

  2. While I cannot definitively explain why Google would endorse this practice, it could be inferred that Google values performance optimization over strict adherence to standards, especially in cases where noncompliant markup poses minimal disruption. This tendency towards risk-taking can also be observed in other facets of Google's operations, such as with Google Chrome.

    Your suggestion of placing the noscript and link elements just before the closing </body> tag aligns with how browsers and the HTML5 specification handle these situations. However, it should be noted that positioning a link element within a noscript element outside of the head section is technically not compliant with standard practices. Nevertheless, considerations like these often take a backseat to considerations of performance enhancement.

Answer №2

Simply put:

If we place any content after the closing </body> tag, it will automatically be moved inside the body section at the end. This is in accordance with HTML specifications, which dictate that all content must reside within the <body> tags. Thus, there should not be any empty spaces following </body>.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Alignment of elements in a list at the bottom

I am looking to create multi-level tabs using <li> that grow from bottom to top. Here is the current code I have: div{ width: 200px; border: 1px solid black; } ul{ margin: 0px; padding: 0px; } li{ margin: 2px; width: 20px; display: ...

Turn off the ability to cache an HTML file without using PHP

I am currently faced with the challenge of managing a rather outdated, small website stored on a web space that does not support PHP. To overcome this limitation, I have decided to use iframes to avoid having to manually implement the basic structure and m ...

Adding Space Before Bullet Points in HTML: A Quick Guide

Is there a way to add space before bullet points? My requirements are as follows: This is paragraph 1 The first text line The second text line This is paragraph 2 The first text line The second text line I would greatly appreciate any ...

Toggling with Jquery when an image is clicked

I'm trying to wrap my head around the functionality of jquery toggle. My goal is to toggle to the next anchor element with the class plr-anchor when an image with the class go_down is clicked. The information is being populated using maps. Javascript ...

Dealing with CSS Overflow Issues: Fixing the Scroll Bug

My current project involves creating a page with fixed links at the top and submit buttons at the bottom. The content in the middle is scrollable using overflow:auto. I've noticed that when I resize the browser window, the scrollbar slowly disappears ...

Repairing the Performance of the 'Save' Feature

Can the 'Save' button in my code save team assignments for players selected using drag and drop? I'm considering using localStorage, but unsure about implementation. Note: To run the code properly, copy it as an HTML file on your computer. ...

Looking for a way to efficiently add multiple value inputs to a JSON object using jQuery or JavaScript?

Here is the HTML input tag code I am working with: <form id="info"> <input id="A" name="A" type="hidden" nodetye="parent" value="A"> <input id="A1" name="A1" type="text" nodetype="child" value="a1val"> <input id="A2" name ...

The menu field remains open even after clicking on the menu

I have encountered an issue with my code. Here is a DEMO that I created on jsfiddle.net Currently, when you click on the red div, the menu opens. However, if you click on the menu items, the menu area does not close. What do I need to do in order to clo ...

Populate a MySql database with 2 input values

I am facing an issue with inserting values into MySql from a table. When trying to insert two values, only one gets stored in the database and the other remains empty. Here is the structure of my MySql table: Below is the HTML code for my table: <tab ...

Gain access to the iterable within the adjacent tag

I am looking to access an iterable from a sibling tag, but unfortunately it is within a table which complicates things. Simply wrapping the content in a div and moving the iteration outside is not possible due to the structure of table rows. Here is an exa ...

Struggling with finding the appropriate CSS selector?

I'm struggling to find the correct selector. Let me do my best to explain the situation: I am currently working on a project where I am unable to make changes to the HTML and JavaScript due to dynamic content and other constraints. Within this proj ...

JavaScript event listener 'click' not functioning properly

I'm facing an issue with a click event in a script that is associated with a specific div id. When I move the mouse within the div area and click (the cursor remains unchanged), the event listener does not activate. How can I make the div area clickab ...

What could be causing the issue with the functionality of third-level nested SortableJS drag-and-drop?

I am currently utilizing SortableJS to develop a drag-and-drop form builder that consists of three types/levels of draggable items: Sections, Questions, and Options. Sections can be dragged and reorganized amongst each other, Questions can be moved within ...

Can you explain the distinction between angular input and native HTML attributes when used with HTML tags?

Take the input tag as an example, where you have two options to specify a maximum length: <input [maxLength]="20" type="text"> <input maxLength="20" type="text"> Are there any real-world distinctions between using one approach over the othe ...

The fadeOut() function is not functioning properly as the div keeps reappearing even after attempting to

My navigation bar has a unique feature - the subnav expands to fullscreen on hover, while all other subnavs close. However, I encountered an issue with the close button functionality. When clicked, the subnav fades out but then immediately fades back in. ...

I'm encountering difficulties with customizing the root styling of material-ui's DialogActions

Check out the two buttons in a DialogActions just like this. This is the JSX snippet I'm working with: <DialogActions classes={{ root: classes.dialogActionsLeft }}> <Button autoFocus onClick={() => { setOpen(false); }} ...

Establishing a Connection with Node/Express Routing via JavaScript

When developing a web application using HTML, JavaScript, and Node.js with Express, I often find the need to navigate between pages based on user actions. In HTML, one approach is to add an href link and then set up routes in my app.js file to handle the ...

Get the file from the web browser

Hey there, greetings from my part of the world. I have some straightforward questions that I'm not sure have simple answers. Currently, I am working on a web application using the JSP/Servlet framework. In this app, users can download a flat text fil ...

Tips for utilizing CSS to position a semi-circle in the middle of an image

Can CSS create a half circle effect on any image? I want to achieve a half circle effect in an image using CSS. Is this possible or not? ...

Is there a way to right-align the labels and input fields simultaneously?

Is there a way to line up the labels and inputs on the right side so they all appear in the same row? .radioContainer{ width: fit-content; margin: 5px; margin-top: 20px; background-color: aqua; padding-bottom: 25px; } <div class=" ...