Organize your HTML & CSS by placing the <link> tag outside of the <head> for better structure

Would it be acceptable to place the <link> to a CSS file outside of the <head/> tag, maybe in the footer section?

What are the potential advantages and disadvantages of doing this?

I am considering this because I have a CSS file that only contains some CSS3 animations for my website, and I believe moving it to the end of the HTML could improve performance...

Thank you

Answer №1

Cascading Style Sheets (CSS) are typically linked in the <head> of an HTML document. This allows the browser to apply styles to the content as it loads, resulting in a smoother rendering process. Placing CSS at the bottom of the document forces the browser to reapply styles and redraw the entire page, leading to slower loading times and a less visually appealing experience.

In contrast, JavaScript files included in a document will block the loading process until they are fully executed. As a best practice, scripts should be placed at the end of the document to minimize any delays in loading content.

Answer №2

The regulations set by W3 state that <link> tags should only be placed within the <head> section:

Resources

For HTML 4.01: http://www.w3.org/TR/html401/struct/links.html#edef-LINK

For HTML5: http://www.w3.org/TR/html5/document-metadata.html#the-link-element

Verification Concerns: Updated October 27, 2017

In 2013, if a link tag was included in the body of an HTML document, it would not pass validation on validate.w3.org based on HTML 4.01 rules.

(Experiment with HTML 4.01 versus HTML 5.0 validation at )

Initially, the HTML 5.0 specification seemed to indicate that link elements should only exist in the head section. However, validation using an HTML 5.0 validator shows documents as acceptable even with a link in flow content.

An explanation for this inconsistency could be found below.

Referring to the MDN documentation for the link entry (MDN Link entry), it is revealed that if the link element contains an itemprop attribute, it can be placed in flow and phrasing content, including the body.

This might clarify why HTML 5.0 validators do not raise any alerts even without the presence of the itemprop attribute.

The itemprop is linked to the microdata standard and is relatively recent (learn about HTML Microdata) making it worth exploring.

Currently, incorporating a link to a stylesheet within the body is possible, although the benefits are unclear.

Answer №3

Although this conversation is dated, it's important to mention that Google Pagespeed Insights currently suggests deferring the loading of substantial CSS files until they are below the fold in order to prevent them from hindering the loading of HTML.

Answer №4

Absolutely, according to the HTML5 specifications, it is entirely acceptable to place a link element within the body element. The decision of whether this is advantageous or detrimental relies on the content you are linking to. If displaying the linked content immediately is not vital for the initial view of your website, then delaying its loading could be deemed as a beneficial strategy.

Answer №5

The WHATWG HTML Standard actually permits the use of <link> within the body in a variety of specified scenarios, as detailed here.

In terms of the practicality of placing <link> before the closing </body> tag, I recently employed this technique to preload large images in a gallery:

<link rel="preload" href="images/big/01.jpg" as="image">

This allowed users to click on thumbnails without having to wait for the server response, since the image had already been preloaded into the browser cache.

Answer №6

It's important to include the <!DOCTYPE html> declaration before adding any <link> tags in your HTML document. Neglecting to do so could potentially result in webpage malfunctions based on previous encounters.

Answer №7

Like all aspects of software development, things are constantly evolving. It is now recommended to include CSS in the body of the document, as shown below:

<head>
</head>
<body>
  <!-- Use HTTP/2 to push this resource, or inline it for faster loading -->
  <link rel="stylesheet" href="/site-header.css">
  <header>…</header>

  <link rel="stylesheet" href="/article.css">
  <main>…</main>

  <link rel="stylesheet" href="/comment.css">
  <section class="comments">…</section>

  <link rel="stylesheet" href="/about-me.css">
  <section class="about-me">…</section>

  <link rel="stylesheet" href="/site-footer.css">
  <footer>…</footer>
</body>

Source: https://jakearchibald.com/2016/link-in-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

Is there a way to replace a CSS class applied within an iframe?

I have a third-party iframe widget component embedded in my page. The only HTML string present in the body of my page is this iframe, which includes a custom button styled by the widget.jsp. In Chrome's inspector, I can only see the <iframe> com ...

Exploring Vue.js: Leveraging v-bind for multiple classes in Vue.js applications

Can someone assist me in figuring out how to apply multiple options to v-bind:class? In my Uno game, I need to determine the text color of cards based on their color property stored as a list of objects like this: ([{ Color: green, Value: 6}]. Here is my ...

Changing the position of a sprite using jQuery

Looking for assistance in moving the scroll location onClick of another div using jQuery. The image is a sprite. .top-background{ background: url("../images/mainall.jpg") no-repeat scroll 0px 0px transparent; height: 888px; width:1024px; } ...

Unleashing the power of Bootstrap5 to craft a dynamic sliding effect within a carousel:

I recently tried to set up a carousel on my website using Bootstrap v5.1 by following the official documentation. While the carousel is displaying, I noticed that the slides are transitioning statically instead of sliding gracefully when navigated with arr ...

What could be causing the width of a table row (tr) in a table element to not function properly, while the height is functioning as expected?

My code is as follows: .table1 { background-color: gray; display: flex; justify-content: ; align-items: ; height: 400px; margin-left: 40px; padding-left: ; } .table1 tr { background-color: blue; height: 50px; width: 50px; } <tabl ...

JavaScript analyzing a file's content one line at a time

Here is the image of a file (screenshot) Could you provide guidance on how to parse this file line by line using JavaScript and display each line in its own HTML element? function loadTxt() { jQuery.get('http://localhost:8000/np/nowPlaying.t ...

The color of the links in the Bootstrap navbar refuses to budge

I'm having trouble changing the color of my navbar links to white and spacing them out evenly. I've written some code to do this, but for some reason, it keeps getting overridden. The background of the navbar is an image, and when I remove the im ...

Managing a new browser window using Selenium

I am attempting to automate a download process on a webpage using selenium. Upon clicking a button, a blank popup window appears. After approximately 60 seconds, a link appears within the popup that I want to click on. The code snippet below outlines my ap ...

Is there a way to modify the standard width of semantic-ui sidebars?

I'm trying to adjust the width of semantic-ui sidebars, which are originally 275px wide. Where should I include the css/js code to make them wider or narrower? Here is their default code: .ui.sidebar { width: 275px!important; margin-left: -275 ...

Activating two buttons in jquery will trigger this action

I am currently working on developing a filter button that will perform different actions based on which buttons are pressed. Pressing button1 will trigger one action, while pressing button2 will trigger another action. If button1 is pressed first, followe ...

Loading content beforehand vs. loading it on the fly

I am facing a dilemma with loading a large amount of content within a hidden <div>. This content will only be revealed to the user upon clicking a button. Should I load this content beforehand or wait until it is requested? ...

Can someone help me figure out how to identify the radio tag selected by the user within my JavaScript code?

I have been troubleshooting the code within my showresults function to capture and compare users' answers (input tags selected by the user as answers to each quiz question) with the correct answer in my questions array. However, whenever I attempt thi ...

Why doesn't jQuery UI's addClass method animate visibility like it should?

One of the advantageous features of jQuery UI is its enhancement of the jQuery addClass method, which enables animation by including a second parameter for 'duration', as shown below: $('div').addClass('someclass', 1000); Th ...

Utilize an Ajax function to call the BOT listening URL within a Web method

My recently developed web application features a submit button that triggers an Ajax call to communicate with the BOT. The code snippet used for the Ajax function is as follows: <script> $(document).ready(function () { $("#btnSend& ...

Customize one header to display different banners on each page using PHP conditionals

I created my website for entertainment purposes and I decided to use different banners on each page. Currently, in my header section, random banners appear using the following code: <div style=' height:145px; background-image:url(images/banners/ba ...

My HTML table is not printing at full width

Seeking assistance with creating a printable calendar using an HTML table. The calendar prints perfectly on a Mac, but when attempted on Windows in all browsers, it adds a 3" margin at the top regardless of CSS print settings. The client requires the cal ...

Using Bricklayer.js multiple times on one page: a guide

UPDATE I have incorporated @Alice's solution into this code snippet to demonstrate the purpose of using bricklayer. While working with bricklayer, I noticed that it only affects the first bricklayer and ignores the second one. This behavior is expecte ...

Use setTimeout and setInterval with the initial input without parentheses or double quotation marks

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var count=0; function increaseCount(){ document.getElementById('txt').value=count; count++; setTimeout(increaseCount(),1000); } </script> </head&g ...

Having trouble with the functionality of the cascading menu?

I am having trouble with a drop-down menu. The first level works fine, but I can't seem to get the second level of the menu to display. Appreciate any help you can offer. Thank you. javascript <script type="text/javascript"> $(document).ready( ...

Using a JavaScript if statement to check the height of a specific HTML element

I have been struggling to figure out how to insert an if-else statement in JavaScript that references data about an HTML element. My objective is to create an "onclick" function that enlarges an image with a 200ms delay and another function that returns th ...