Why Use The <html> Tag? [duplicate] - html

This question already has answers here:
Is it necessary to write HEAD, BODY and HTML tags?
(6 answers)
Closed 9 years ago.
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.

It's part of the standard. It helps tell the browser it is HTML vs XML or some other type of markup and makes it clear what kind of document it is.
http://www.w3schools.com/tags/tag_html.asp
The browser is just being nice to you and showing what is there without the tag.

It is what tells various parties reading the document that it is an HTML document and that this is where it starts.

Related

Does HTML DOCTYPE inserts an html tag too? [duplicate]

This question already has answers here:
Is it necessary to write HEAD, BODY and HTML tags?
(6 answers)
Closed 7 years ago.
We use HTML5 declaration these days <!DOCTYPE html> to declare current page as html5 standard.
I found one use through html5boilerplate as it can be useful for specifying language, javascript detection class="no-js". But that doesn't change my mind to omit it, i always forget it anyway.
I want to know if it also automatically inserts an <html> tag too.
Like if i wrote in start of my page
<!DOCTYPE html>
instead of
<!DOCTYPE html><html>
I tried every browser without <html>, and everything worked fine, html5 standards were accepted and browsers also added tag.
What you are doing when you omit the <html> tag is not relying on the doctype tag to automagically "insert" one after itself, but rather you are relying on the fact that some of the HTML standards did not define it as a requirement (notably HTML5, and HTML1 where it was totally absent).
This means that browsers will just have to cope with the fact that the <html> tag might be absent and can not be relied on, and thus don't horribly malfunction if you omit it.
This question has more info on the topic: Is it necessary to write HEAD, BODY and HTML tags?

Why should i wrap my document in <html> tags? [duplicate]

This question already has answers here:
why we use <html> tag although my website runs perfect without <html> tag
(4 answers)
Closed 8 years ago.
As far as I can work out, surrounding my code in html tags, does the same thing as !DOCTYPE html, so why should I use them.
Is:
<!DOCTYPE html>
<html>
...
</html>
The same as:
<!DOCTYPE html>
...
I have tried a few different sites, and they are all identical when I remove the HTML tags. These sites contain numerous different languages, PHP JavaScript and a few others, but none seem affected.
I was surprised that no one has already asked this question, as it seems fairly obvious.
I realise that html tags are used to tell the browser how to render a page, but that is the job of the <!DOCTYPE...>.
I have tried googling (is that how you say it?) and found nothing, except a comment that they should be used, but no explanation.
This seems like a simple question, but I feel may have a more in-depth answer.
Thanks for any help you can give.
The HTML start tag and end tag are (by specification) optional, so they have little practical use (assuming you aren't working in XML) other than to stop people who don't know they are optional thinking of badly of you.
You do need a start tag in order to have attributes on an element though, and you should specify the language the document is written in with a lang attribute.
The HTML tag is an optional tag i.e. you can have it or not have it unless the first element is a comment. There are other optional tags like BODY and HEAD and there are conditions on their optionality.
http://www.w3.org/TR/html-markup/html.html
http://www.w3.org/TR/html-markup/body.html
http://www.w3.org/TR/html-markup/head.html

Closing empty tags, HTML, XML

I posed a similar question some time ago and got a pretty good answer, now I would like to slightly modify that question in light of something I noticed while working with Google Maps.
The question is, I've always used <div style="clear:both"></div> to clear a float. There are other times when I've needed to create an empty element to be populated with JS, for example. Now, since HTML is a subset of XML, why can't I use <div style="clear:both" /> instead of typing the ugly closing tag.
I was given a great answer that I admittedly don't fully understand in my previous question, but while working with Google Maps I noticed that Google had the same idea that I did. In their very first code sample, they use <div id="map-canvas"/> without the ending tag.
So my new question is, even if this is not quite proper HTML, would there ever realistically be case where this would not work?
Please and thanks.
Now, since HTML is a subset of XML
HTML is not a subset of XML.
HTML 4 and earlier were SGML applications, but browsers never implemented the SGML specification properly.
XHTML 1.x is an XML application, but browsers will only use XML parsing rules if you serve XHTML with an XML content-type (like application/xhtml+xml).
HTML 5 has its own parsing rules that better reflect what browsers actually do. It allows a / character at the end of elements where the end tag must be omitted for the sake of people who are addicted to XML or have poor syntax highlighting software, but only those elements.
I noticed that Google had the same idea that I did. In their very first code sample, they use <div id="map-canvas"/> without the ending tag.
That is an error and is not allowed in HTML. It only "works" in browsers because the end of the document comes before the start of any element or text that is allowed as a child node of a div element.
The question is, I've always used <div style="clear:both"></div> to clear a float.
That's a nasty approach to the problem in the first place. It requires an extra element, and can add space where the element is rendered. Better, in almost every case, to set overflow: hidden on the containing element to cause it to wrap the floats.
HTML is not a subset of XML. HTML has a different structure. Some elements are singular (like image) and don't need a closing tag or ending / at all. The ones who do, need to be closed by a proper closing tag.
Some people use XHTML, though, which is basically HTML using XML syntax. It depends on the doctype you use.
Google's example is wrong in this case. It uses a Html 5 doctype. If you run their snippet through the W3 validator, it tells you:
Line 26, Column 26: Self-closing syntax (/>) used on a non-void HTML
element. Ignoring the slash and treating as a start tag.
<div id="map-canvas"/>
And that's what will probably happen in most browsers as well. They read it as if the div is just opened there. At a certain point it will be automatically closed. The / is ignored.
About the clearing of floats: the way you are doing it is old, and ugly because you need extra markup, for what is basically a CSS issue. Fortunately, there are better ways, a couple of which are described in detail here: What methods of ‘clearfix’ can I use?

Can a HTML document be created without the <html> tag? [duplicate]

This question already has answers here:
Is it necessary to write HEAD, BODY and HTML tags?
(6 answers)
Closed 8 years ago.
I found this sample HTML code on gitHub - https://github.com/jasondavies/d3-cloud/blob/master/examples/simple.html and noticed that this page has no <html> and </html> tags and no </body> tag. I was told that these were optional. I understand <tag />, but how can one create an html document without the <html> tag? What is going on in this example?
The author tells me that it is.
Yes, it can be created and it will work in some browsers like Chrome and Firefox without problems but it will break in others. Generally, it's not recommended to do that because, as I already said, it might break functionality in some browsers.
Also, according to the HTML5 specs the html, body and head tags are completely optional. However, it appears that a non-empty <title> tag is required in both HTML4 and HTML5.
EDIT: if you are looking for more competent answer you should check this answer:
Is it necessary to write HEAD, BODY and HTML tags?
<html> and <body> tags are a rule in every html page. Some people doesn't use them but you should! In some old browser a page without <html> tag could be read in a wrong way.
So put them!
Read here: http://webdesign.about.com/od/beginningtutorials/qt/html-quick-and-dirty.htm
It really depends on the service. As a rule, I tend to put the <html> in any HTML that I'm returning to a client machine. Chrome, for example, will automatically generate the <html> tag for you. Others, like IE will not, and will have a difficult time understanding the code.
My recommendation is that you always include the <html> in your HTML output, because this is the best, cross-browser compliant way of formatting HTML.
Example
If you open this file:
<h3>Hello World!</h3>
In a Chrome browser, and view the elements in the developer console, you will see:
<html>
<head>
</head>
<body>
<h3>Hello!</h3>
</body>
</html>
Chrome can handle this, but other browsers can not.
the start /end tags of a few HTML5 elements can be omitted when specific conditions are met
e.g. The html start tag is optional unless the first thing inside the html element is not a comment
http://w3-video.com/Web_Technologies/HTML5/html/html5_html_tag_optional.php
but if you don't want to learn these conditions, just don't omit the start/end tags of HTML5 elements, that's the easy way

Doesn't all HTML tags need to be closed?

I see some HTML codes like <tag attribute='x'> without the closing </tag>, surprisingly in w3schools.com.
One example is here: http://www.w3schools.com/tags/tag_base.asp
And I know that some tags do work without closing them, but in the long run they can result in invisible failures, and moreover, my coder instincts tell me that all tags need to be closed.
So are there some tags (or some contexts) that do not need to be closed?
Thanks !
No tags do not always have to be closed; the HTML5 spec enumerates when and where this behavior is appropriate. However it is much easier for browsers to parse the HTML if tags are closed properly, IE9 does not follow the HTML5 parsing rules and may break if tags are not closed properly.
There are tags which are not containers and therefore can't be closed because they are one unit, for example the image tag:
<img src="abc.jpg">
However if you want to follow html standard, you need to write this like:
<img src="abc.jpg" />