<!DOCTYPE html>
<meta charset="utf-8">
<body>
Hello, world!
SOURCE FOR CODE
If so, besides removing "Hello, world!" is there any tag that's able to be removed and it still be valid, and how do you know it's still valid?
It's not valid. To check it you can run it in W3C Validator
The error is: Element head is missing a required instance of child element title.
...
UPDATE
As vcsjones stated the head element is optional. That's the title one is required. Credit to mootinator for pointing out that the body is also optional.
So the simplest valid document will be:
<!DOCTYPE html>
<title></title>
(Assuming the HTML syntax of HTML5.)
Note that in some situations the title element is optional, too.
From HTML5’s definition of head:
The title element is a required child in most situations, but when a higher-level protocol provides title information, e.g. in the Subject line of an e-mail when HTML is used as an e-mail authoring format, the title element can be omitted.
So the minimal markup for a document that gets a title from a "higher-level protocol" is this:
<!DOCTYPE html>
If the document is the value of an iframe-srcdoc it’s this (assuming a title is provided by the container document):
<html>
And for a stand-alone document it’s this (the title element needs some actual content, as noted by kapep, so the "…" is just an example):
<!DOCTYPE html>
<title>…</title>
The title tag can't be empty or only consist of whitespace. So if the document is in a context where the title tag is required, you will have to set a valid title value.
The title content model is defined as "Text that is not inter-element whitespace".
"Empty Text nodes and Text nodes consisting of just sequences of [space characters]" are inter-element whitespace. Space characters are space, tab, line feed, form feed and carriage return.
If the title tag is empty, the W3C Validator complains that "Element title must not be empty". The Validator is fine with only adding just spaces, even though that is not correct according to the specs.
It is valid if you add another non-space character:
<!DOCTYPE html>
<title>x</title>
You could use other space characters like non-break space or zero-width non-break space if you want to fake an "empty" title.
The smallest HTML document for which the Nu Html Checker (the only HTML validator currently endorsed by the WHATWG) does not produce any errors nor warnings is the following:
<!DOCTYPE html>
<html lang="">
<title>x</title>
Related
If I put some text inside <head> tag of an HTML page, this text is rendered by popular browsers like Chrome or Firefox (see the MWE below). Is it an expected behavior?
<!DOCTYPE html>
<html lang="en">
<head> Foo Bar </head>
</html>
Text nodes are not allowed inside the head element, but the end tag for it is optional, so the text node implicitly ends the head element and starts the body.
The end tag for the head element has no matching start tag so is discarded as an error.
This is expected error recovery behaviour.
Use a markup validator.
As an addition to quentin's answer - it is good to realise that browsers handle html errors loosley. They try to 'render as much as they can. This is in one way to aid developers and in an other way better for user experience. This makes it so a single error somewhere does not stop the whole document from rendering (or showing a fat warning) - which is generally more appreciated by the end-user.
Interesting (background) info:
Tag soup
Error tolerance in browsers
Is there any drawback to never using
<html> and <body>
on your web pages that are written in HTML and PHP?
I mean, everything works perfectly fine with it or without it, so why use it?
They are explicitly optional in the spec (so the document will still be valid).
This has been true since the original spec (which says <!ELEMENT HTML O O (( HEAD | BODY | %oldstyle)*, PLAINTEXT?)>, O O meaning Start Tag Optional, End Tag Optional) through to the current spec (which says "An html element's start tag can be omitted if the first thing inside the html element is not a comment. An html element's end tag can be omitted if the html element is not immediately followed by a comment.").
They are only mandatory in XHTML since XML has no concept of optional tags.
I've never seen any browser or user-agent fail to handle them correctly in an HTML document. (Note that while the tags are optional, the elements are not, so browsers will insert an HTML, HEAD and BODY elements even if the tags are missing, so any script which tries to find them in the DOM will still work).
The only technical drawback is that you can't put attributes on tags which aren't there, and a lang attribute for the HTML element is useful.
Leaving them out can confuse other people who have to maintain your code who don't know that the tags are optional though.
Both <head> and <body> tags are optional in HTML5. In fact it is recommended by Google's HTML style guide to not use them:
<!-- Not recommended -->
<!DOCTYPE html>
<html>
<head>
<title>Spending money, spending bytes</title>
</head>
<body>
<p>Sic.</p>
</body>
</html>
<!-- Recommended -->
<!DOCTYPE html>
<title>Saving money, saving bytes</title>
<p>Qed.
By not using those tags, some drawbacks include:
that it is drastically different from what is typically learned for developers, so it may cause some confusion.
a restriction that a comment cannot be immediately after the <html> tag that is omitted.
Reiterating the optional nature of the tags from the spec:
An html element's start tag may be omitted if the first thing inside the html element is not a comment.
A body element's start tag may be omitted if the element is empty, or if the first thing inside the body element is not a space character or a comment, except if the first thing inside the body element is a meta, link, script, style, or template element.
See:
https://google.github.io/styleguide/htmlcssguide.xml?showone=Optional_Tags#Optional_Tags
https://html.spec.whatwg.org/multipage/syntax.html#syntax-tag-omission
If you do not use <html> and <body> than your HTML document will be not valid, some libraries/plugins may not work too.
Validators accept both form of tag <meta> and <meta/>. HTML5 specification says that no end tag should be present, hence the form of <meta><meta/> is prohibited. But I could not find any information about form <meta/>.
As per this HTML5 standard: http://www.w3.org/TR/html-markup/syntax.html#void-element
Start tags consist of the following parts, in exactly the following
order:
A "<" character.
The element’s tag name.
Optionally, one or more attributes, each of which must be preceded by one or more space characters.
Optionally, one or more space characters.
Optionally, a "/" character, which may be present only if the element is a void element.
A ">" character.
The meta is a void element and hence the part #5 would apply with a caveat that "optionally, a / character, which may be present..."
<meta ... />
And so, you may omit the part #5 i.e. the closing "/", and hence this is also valid:
<meta ... >
Further down the spec says:
Void elements only have a start tag; end tags must not be specified
for void elements.
To summarize, end tag is not required. Self closing is not required. It will not hurt if end tag or self-close is present.
.
It depends on whether you use HTML5 or XHTML5 syntax. In XHTML5 it is required and the parser will freak out if you don't use / when closing tag. Generaly all XML elements must have closing tag.
Try this snippet of code in Validator.nu
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Document</title>
<meta charset="UTF-8"/>
</head>
<body>
</body>
</html>
Try to remove / from meta charset and observe the result. Don't forget to set correct preset for XHTML5.
Looking at Mozilla's documentation for it, as well as from what I've observed in general, you just don't close the tag.
<meta charset="utf-8">
The above is valid HTML5. There's no reason to include any sort of close tag or anything resembling a closing tag on it.
If I want to redirect to another page in my HTML file, do in have to place the meta tag in the head or can I place it at the top of the file before the DOCTYPE? Thank you.
You can't place a meta tag above the DOCTYPE. The DOCTYPE must always be the first element in an HTML document, and meta tags must only be placed in the head.
Documents must consist of the following parts, in the given order:
Optionally, a single "BOM" (U+FEFF) character.
Any number of comments and space characters.
A DOCTYPE.
Any number of comments and space characters.
The root element, in the form of an html element.
Any number of comments and space characters.
Source: http://www.w3.org/TR/html5/syntax.html#writing
For purposes of this question, the spec says that a document must start with a DOCTYPE and be followed by a root html element. While a meta tag might still work, there is no guarantee of it doing so today and continuing to do so in the future.
The meta tag has to be inside the <head></head> section. You can not add anything before <!DOCTYPE html>
Here is detailed description of DOCTYPE
W3C deprecates the use it, but they do offer an example on W3C:
<HEAD>
<TITLE>Don't use this!</TITLE>
<META http-equiv="refresh" content="5;http://www.example.com/newpage">
</HEAD>
<BODY>
<P>If your browser supports Refresh, you'll be transported to our
new site
in 5 seconds, otherwise, select the link manually.
</BODY>
GIYF: H76: Using meta refresh to create an instant client-side redirect
You should insert the following line in the head section of your HTML page, replacing http:example.com/ with the actual web page to which you want to redirect your viewers:
< meta http-equiv="refresh" content="2;url=http://example.com/" />
Here is an example with the correct line inserted in a typical HTML page. Note that it comes above the title tag.
<html>
<head>
<meta http-equiv="refresh" content="2;url=http://example.com" />
<title>Page Moved</title>
</head>
<body>
This page has moved. Click here to go to the new page.
</body>
</html>
I've just been reading the HTML5 author spec.
It states that the <html>, <head> and <body> tags are optional.
Does that mean that you can leave them out completely and still have a valid HTML5 document?
If I'm interpreting this correctly, it means this should be completely valid:
<!DOCTYPE html>
<p>Hello!</p>
Is this correct?
You can check out the spec here:
http://dev.w3.org/html5/spec-author-view/syntax.html#syntax
"8.1.2.4 Optional tags" is the bit out about it being OK to omit <html>, <head> and <body>
The title element is indeed required, but as Jukka Korpela notes, it also must be non-empty. Furthermore, the content model of the title element is:
Text that is not inter-element whitespace.
Therefore, having just a space character in the title element is not considered valid HTML. You can check this in W3C validator.
So, an example of a minimal and valid HTML5 document is the following:
<!doctype html><title>a</title>
This is the minimal HTML5-valid document:
<!doctype html><title> </title>
W3C HTML validator maintainer here. FYI with regard to the validator behavior, as of today, the validator now enforces the requirement in the HTML spec that the title element must contain at least one non-whitespace character -
http://validator.w3.org/nu/?doc=data%3Atext%2Fhtml%3Bcharset%3Dutf-8%2C%3C%2521doctype%2520html%3E%3Ctitle%3E%2520%2520%2520%3C%252Ftitle%3E
While the <html>, <head> and <body> start and end tags are optional, the <title> tags are required, except in special circumstances, so no, your sample is not (ordinarily) valid.