Everything I have seen says that the HTML code needs it, but mine works fine without it. I'm only using extremely basic HTML without CSS or javascript, if that makes a difference. Could someone please explain?
Everything still works because the browser is plugging it in for you. You should use it because it makes your code more clear and standard.
A developer looking at your code might be confused about what they're seeing at first because they would wonder where the <html>
tag is.
As with any standard, the <html>
tag guarantees that things will work. Currently the tag can be omitted, but I still wouldn't recommend it, to be safe. This is from the W3 spec:
An html element's start tag may be omitted if the first thing inside the html element is not a comment.
An html element's end tag may be omitted if the html element is not immediately followed by a comment.
Everything I have seen says that the HTML code needs it, but mine works fine without it.
That's because you don't need it. The HTML specification says that you can omit the starting tags (and even the ending tags) of many elements, including html
and head
, which makes documents like this actually perfectly valid:
<!DOCTYPE html>
<title>Text to make me non-empty</title>
<p>Hello world!
Browsers will create the html
and head
elements even if you don't write out the tags, so omit them if you want. Do note that not all browsers follow the spec properly, so while this behavior would be ideal, some browsers will parse your HTML improperly and force you to be more explicit with your structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With