The HTML validator I found today - http://html5.validator.nu/ - says that my use of the <noscript> element is wrong. My XHTML source code is like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<body>
<div id="head">
<noscript>
<p>JavaScript is disabled.</p>
</noscript>
...
The error message from the validator is this:
Error: XHTML element noscript not allowed as child of XHTML element div in this context. (Suppressing further errors from this subtree.)
Contexts in which element noscript may be used:
In a head element of an HTML document, if there are no ancestor noscript elements.
Where phrasing content is expected in HTML documents, if there are no ancestor noscript elements.
Content model for element div:
Flow content.
Now I went on to the Mozilla Documentation and tried to understand what that means. I have found some information about content categories, flow content, phrasing content, what elements belong to each category (whatever "belong" means exactly) and how the <noscript> element may be used. (https://developer.mozilla.org/en/HTML/Element/noscript)
I now know this: <div> must contain flow content. <noscript> must occur in phrasing content. That obviously doesn't match. How can I manage that? Many elements are in both, flow and phrasing, categories though. They don't seem to be disjunct sets, so some can't decide or I don't get it.
How does the HTML specification intend to solve this quirks?
You shouldn’t use an HTML5 validator to validate your XHTML 1.1 document.
The HTML5 spec says about the noscript element:
The noscript element must not be used in XML documents.
So noscript inside div isn’t allowed in XHTML5, but it’s allowed in XHTML 1.1.
You can't use "noscript" in an XHTML/XML document because it disables the parser for the "noscript" content and you can't do that in XML. The parser cannot be temporarily disabled or turned off.
For example, CSE HTML Validator generates this message for XHTML documents:
XHTML documents must not use the "noscript" element. This is because the way "noscript" works by "turning off" the parser when scripts are enabled, which can't be done in XML. Also, the "noscript" element is explicity not allowed in HTML5 for this reason.
If you want to use "noscript" properly, then you'll need to change the document type to HTML5 (recommended) or HTML4 instead of XHTML.
Use the official validator to validate your HTML. The following code validated perfectly fine for me:
<!DOCTYPE html>
<html>
<head>
<title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
</head>
<body>
<div id="head">
<noscript>
<p>JavaScript is disabled.</p>
</noscript>
</div>
</body>
</html>
EDIT: Interestingly, the W3C validator claims that the parser is based on the validator at the link you provided.
'The uploaded document was successfully checked as HTML5. This means that the resource in question identified itself as "HTML5" and that we successfully performed a formal validation of it. The parser implementations we used for this check are based on validator.nu (HTML5).'
Perhaps the validator at http://validator.nu is an older version. Edit edit: Nope, it validates just fine there too. Checked as HTML5 and XHTML 1.0 Strict.
Related
Probably a really dumb question - but in an HTML document, why does the following:
<title></title>
work ok (according to http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_basic)
but then
<title />
will stop the HTML from rendering?
Thanks
As stated in the HTML specification:
The title element must not be empty.
It also states:
Note: If it's reasonable for the Document to have no title, then the title element is probably not required.
The <title> tag must have a start and an end tag, it's required:
Tag omission
A title element must have both a start tag and an end tag.
In an HTML document delivered as text/html, as web pages normally are, the tag <title /> is processed by browsers as if the slash were not there. That is, it is just the start tag. And the content of title is parsed as text – character references/entities are recognized but no markup, except the element’s own end tag. So all the rest will be taken as the content of the title element, leaving no visible content to render.
Formally, <title /> is invalid in all forms of HTML up to and including HTML 4.01. In HTML5, it is permitted, but in HTML serialization, it means just <title>.
In genuine XHTML, i.e. in XHTML when delivered with an XML media type (rare on the web), <title /> means the same as <title></title>. This is valid except according to HTML5 drafts, which require nonempty content here. But it is not recommended, because it does not work that way when a browser, or other program, processes the document as if it were in HTML syntax.
Cf. to Are (non-void) self-closing tags valid in HTML5?
My colleague doesn't really know or understand html. Her job is to input information into the CMS and I've noticed she keeps closing her <hr /> tags like this <hr></hr>.
I've had a Google but I can't find anywhere that says this isn't allowed or could cause problems. I know it's supposed to be <hr /> but is it worth me telling her or is it unnecessary but valid markup?
NB The doctype for our website is
XHTML 1.0 Transitional if that makes any difference.
EDIT
#Jeff had a good idea about validating. I used the following code and apparently this is valid XHTML 1.0 Transitional
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<hr></hr>
</body>
</html>
OK, <hr></hr> is actually a valid XHTML 1.0, too.
So, for XHTML 1.0:
<hr /> is valid
<hr></hr> is valid
<hr> is not valid
... for HTML 4.0:
<hr /> is valid
<hr></hr> is not valid
<hr> is valid
therefore the best option is to use <hr />, which is always valid.
HTML 4 says:
Start tag: required, End tag: forbidden
And as XHTML basically means that HTML tags need to have a closing tag, I would say <hr /> is the only format you should consider.
As the others say, <hr></hr> is valid XHTML (they even use it as example) but for compatibility reasons I would not use it.
<hr /> is merely shorthand for <hr></hr>; both are acceptable in XHTML documents. However, neither are acceptable in HTML documents, where <hr> should be used instead, which in turn is invalid in XHTML.
No. <hr /> should not have a closing tag.
It is invalid HTML.
It is valid XML and therefore technically it's valid xhtml, but you still shouldn't use it, even if you're using xhtml.
This is because all the browsers actually use their HTML parser even when rendering xhtml code, and therefore the a closing </hr> tag is seen as an error. Some browsers may even mis-interpret it as an additional <hr> element.
The only cross-browser compatible way of doing it is either <hr> (ie plain HTML) or <hr /> if you want to have a valid xhtml document.
As far as I know there is no closing hr tag. The tag is purely self closing and the Doc Type Definition will reflect this.
Since it's an empty element, it makes no sense to have open and close tags.
However, XML requires all tags to be closed, hence the <hr /> form.
I suppose in HTML, not XML, <hr></hr>is valid, but unnecessary.
The HR element draws a horizontal rule. This is a block element and does not require a closing tag.
Ref: http://msdn.microsoft.com/en-us/library/bb159725.aspx
When take some tutorial from web, I see many people leaves tags open like <link ..>, <img ..>. But when I use Netbeans to edit them (the HTML/JSP pages), it show a red background on those tags until I add the slash into them. <br> --> <br/>.
Which is the correct way to write HTML-based code?
Both are fine for HTML. Though not for XHTML which is an XML dialect.
Some elements do not need a closing (/>) tag - in particular empty elements (those that do not have content). Examples are <hr> and <br>. These can also be self closing (<hr /> and <br />, respectively). This self closing is equivalent to having a close tag immediately after the open tag.
For XML, such a non closing tag is not valid - it must be closed, either self closing or have a closing tag. So <hr> is not valid XML, but <hr /> and <hr></hr> are.
HTML is not XML, but for better compatibility some tools try to emit as much XML like HTML as possible.
It depends which DOCTYPE you're using. If you're using HTML 4 then you shouldn't use self-closing tags, if XHTML then you should to make valid XML, and if HTML 5 then closing slashes are optional, but not required.
The W3C HTML Validator will throw a warning if you try to use closing tags in HTML 4:
The sequence can be
interpreted in at least two different
ways, depending on the DOCTYPE of the
document. For HTML 4.01 Strict, the
'/' terminates the tag '). However, since many
browsers don't interpret it this way,
even in the presence of an HTML 4.01
Strict DOCTYPE, it is best to avoid it
completely in pure HTML documents and
reserve its use solely for those
written in XHTML.
> is correct for HTML, but incorrect for XHTML. Check your DOCTYPE.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
for HTML strict
and
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
for XHTML strict
I was just checking to see if it was valid to put an <iframe> element inside a <noscript> element as a fall back for displaying dynamic content. It validated fine with the HTML 5 doctype, but for HTML 4.01, I get the following error:
Line 9, Column 35: element "IFRAME" undefined
<iframe name="test" src="test.htm"></iframe>
You have used the element named above in your document, but the document type you are using does not define an element of that name. This error is often caused by:
incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Frameset" document type to get the "" element),
by using vendor proprietary extensions such as "" or "" (this is usually fixed by using CSS to achieve the desired effect instead).
by using upper-case tags in XHTML (in XHTML attributes and elements must be all lower-case).
This is what I whittled the HTML down to:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<iframe name="test" src="test.htm"></iframe>
</div>
</body>
</html>
The <iframe> element is defined in the HTML 4.01 specification at the following URL: http://www.w3.org/TR/html401/present/frames.html#h-16.5.
It passes with a transitional doctype, so I guess my question is "Why is it disallowed in a strict doctype, even though it's defined in the specification?".
"Why is it disallowed in a strict doctype, even though it's defined in the specification?
Lots of things are defined in the specification but not allowed in Strict. <font> springs to mind. These are the things that the developers of the specification considered in need of documenting, were in use in browsers in the day, but which should be transitioned away from.
I can think of two reasons why they might have thought that:
"Why do iframes suck?".
<iframe> does (in theory) little that can't be achieved with <object>
iframe isn't included in html strict. For validation, try using the object element instead.
<object data="test.html" type="text/html"></object>
You should also add width and height attributes to the object element. Note, unlike iframes objects cannot be a target for any page links.
Unless for some reason you specifically need html4 strict validation, it's better to use the html5 doctype.
Am I allowed to place <noscript> in the <head>?
According to the XHTML Strict DTD, no, you're allowed script, style, meta, link, object, title and base only. Transitional allows isindex as well, but still not noscript.
Using the HTML5 Doctype, I have a declaration in my <head> with a <link> element that points to a no-js.css stylesheet. It validates and seems to work fine.
You are as long as you do it in HTML documents and not XHTML.
In a head element [...], the noscript element must contain only link,
style, and meta elements.
See specification.
HTML5 adds support to <noscript> in the <head>: https://www.w3schools.com/tags/tag_noscript.asp