Forum Moderators: not2easy

Message Too Old, No Replies

Styling non-existent (but implied) tags

Any caveats?

         

encyclo

2:12 pm on Nov 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I take the following document as an example:

<!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]

BonRouge

2:29 pm on Nov 1, 2004 (gmt 0)

10+ Year Member



I have no idea what the answer to your question is. I'm just wondering why you'd want to do that...?

encyclo

2:47 pm on Nov 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm just wondering why you'd want to do that...?

Because I'm awkward like that! ;) Seriously, I like to remove as much cruft as possible from the markup, and I'm testing to try to find the boundaries.

createErrorMsg

3:41 pm on Nov 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm wondering if this technique might confuse SE spiders. For instance, you've cut out the <head></head> tags, as well. If the SE spiders look first for head tags, then within them for title tags in order to index your site, the lack of <head> tags might cause them to skip the site completely.

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.

encyclo

4:37 pm on Nov 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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).

iamlost

12:41 am on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



[w3.org ]

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

vkaryl

1:58 am on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My gosh, encyclo and iamlost, that's very interesting! I'm going to have to find time to really read some of the specs I think....

createErrorMsg

2:18 am on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So now we know that both (a) it works in the browsers, and (b) it's supposed to work in the browsers. Does the original question - how much can we depend on this - remain unanswered, or do facts (a) and (b) reliably imply that we can leave out these tags?

ronin

9:05 pm on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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).

encyclo

11:19 pm on Nov 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



iamlost's quote is the key for me:

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.