Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Use The <html> Tag? [duplicate]

Tags:

html

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?

like image 275
kookman98 Avatar asked Dec 12 '22 08:12

kookman98


2 Answers

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.

like image 86
m59 Avatar answered Dec 13 '22 20:12

m59


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.

like image 36
Blender Avatar answered Dec 13 '22 22:12

Blender