Forum Moderators: not2easy
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<title>test</title>
<style type="text/css">
body {color:#ffc;background:#000;}
</style>
<p>This is a test I'm defining a style on the
body element - but in the markup, the <body> and </body> tags are absent. In most CSS-aware browsers I've tested, the CSS still gives the document a black background with yellow text. The HTML and CSS both validate. Netscape 4 ignores the rule, however - but NN4 is usually wrong.
Trouble is, I can't find anything in the specs which covers this particular situation, so I don't know how dependent I can be on it. Perhaps NN4 is right (you never know!)? Am I taking a risk by styling on implied but absent elements?
Edit reason: correct slight markup error
[edited by: encyclo at 2:52 pm (utc) on Nov. 1, 2004]
Likewise, if finding a body tag is a precursor for finding Hx and P tags, the lack of the body tag would spell doom for site rankings.
I don't have any idea if these things are true, but they're at least worth looking into.
I'm wondering if this technique might confuse SE spiders.
Yes, it's a definite possibility: I'm testing that too with a few select pages. There are a few (but very few) sites with markup as pared-down as this, and I'm analysing the effect as much as is possible. However, pages with this kind of minimalist markup were common at the very beginnings of the web (when page size was crucial), even if they are very rare now. I would have thought that most spiders should be able to cope with it quite well. The biggest problems, for example, occur when there is an opening
<head> but no corresponding </head> - but if both are missing... With this test, however, I'm just seeing how far I can push the simplicity of the markup and still have the page display correctly in modern browsers and according to the specifications. Trouble is, I can't tell from the specifications if what I'm doing is conformant, even if validating. It certainly doesn't work with XHTML, for example (not surprising).
in practice, the HTML, HEAD and BODY start and end tags can be omitted from the markup as these can be inferred in all cases by parsers conforming to the HTML 3.2 DTD.
[w3.org ]
The HTML element...................
Start tag: optional, End tag: optional
7.4.1 The HEAD element................
Start tag: optional, End tag: optional
7.5.1 The BODY element..................
Start tag: optional, End tag: optional
My understanding of the subject is that a compliant browser will read the DTD and assume the presence of <html>, <head>, and <body>. If you read (for example) the 4.01 Transitional Doctype it includes:
<!ENTITY % HTML.Version "-//W3C//DTD HTML 4.01 Transitional//EN"
-- Typical usage:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
...
</head>
<body>
...
</body>
</html>The URI used as a system identifier with the public identifier allows
the user agent to download the DTD and entity sets as needed.
Note the last sentence in particular.
Please do note that a <title></title> is required:
7.4.2 The TITLE element<!-- The TITLE element is not considered part of the flow of text.
It should be displayed, for example as the page header or
window title. Exactly one title is required per document.
-->
<!ELEMENT TITLE - - (#PCDATA) -(%head.misc;) -- document title -->
<!ATTLIST TITLE %i18n>Start tag: required, End tag: required
Do facts (a) and (b) reliably imply we can leave out these tags?
Well if you're talking about real-world functionality, you've already answered your own question with:
In most CSS-aware browsers I've tested, the CSS still gives the document a black background with yellow text.
and
The HTML and CSS both validate.
Which means the browsers accept the ommission and either the validator does too (on the basis that the tags are designated optional) or the validator doesn't even look for missing <head> and <body> tags (again, on the same basis).
in practice, the HTML, HEAD and BODY start and end tags can be omitted from the markup as these can be inferred in all cases by parsers conforming to the HTML 3.2 DTD.
That's just the confirmation I was looking for: the tags can be absent, but they are inferred, so the CSS can apply. I've always known that those tags were optional (they have been since the very first HTML specifications), but I wasn't sure if applied CSS to a missing element was reasonable. I now think it is completely within the specifications.
It is important to note to anyone reading this thread in the future who is trying this sort of markup that if you want to omit the
head element, you must omit both the opening <head> and the closing </head> tags, otherwise you will have problems, in particular with the page being spidered correctly. Oh, and don't try this with XHTML - only HTML allows the tags to be left out. Thanks to all who have replied.