The reason it made such a difference is that the way HTML parsing is specified, it is impossible to have a <div>
in that location: the first child of <html>
is always <head>
, implicitly if not explicitly; seeing something that is not <head>
means that you have an implicit one, possibly empty. Then, on the other side, a <head>
tag in a location it is not allowed is an error and ignored. So, the sequence of steps the parser follows is (somewhat abbreviated):
- My state is
initial
and I see <html>
. Go to state before html
and try again.
- My state is
before html
and I see <html>
. Create the <html>
element and go to state before head
.
- My state is
before head
and I see <div>
. That's not <head>
, so create a <head>
element, go to state in head
, and try again.
- My state is
in head
and I see <div>
. That's not allowed in <head>
, so we must be implicitly after the head; go to state after head
.
- My state is
after head
and I see <div>
. That's not a <body>
, so create the <body>
element, go to state in body
, and try again.
- [Steps for regular body content processing of the
<div></div>
omitted.]
- My state is
in body
and I see <head>
. Declare a parse error because we can't have a <head>
inside a <body>
, then ignore the tag and look at the next token.
- My state is
in body
and I see <link>
. Insert a <link>
element in the open element (which is <body>
).
- [Continues to process the rest of the document.]
If you use the developer tools in your browser to examine the DOM tree, you will see something like
<html><head></head><body>
<div></div>
<link rel="stylesheet" href="./assets/a.css" type="text/css">
<link rel="stylesheet" href="./assets/b.css" type="text/css">
<img src="./assets/image1.png">
<img src="./assets/image2.png">
<script src="./assets/script1.js"></script>
<script src="./assets/script2.js"></script>
<script src="./assets/script3.js"></script>
</body></html>
I don't know exactly what the rules are for script and style loading when the link
s are not in the <head>
as you intended, but they are presumably different in the way you observed.
The important thing to remember here is that a HTML document always has a <html>
element containing exactly a <head>
element and a <body>
element. If you write something else, it will be forced into that shape, possibly not in the way you intended.