Question on the <body> Tags

So i’m doing the Introduction to HTML and it’s going over having all your code contained within the “<body>” tags for in to show up on the page. But when I just put “<p> Hello There </p>” without any body tags i still get content on screen when I Run the code.

5 Likes

So what’s your question? Are you confused as to whether the <body> tags are required or not?

7 Likes

That’s because HTML isn’t a strict language. The <body> and <head> tags are mostly to seperate between content and style/flow of your page.

EDIT: Same goes for not needing <DOCTYPE html> or the <html> tags to run anything. Its just common to do it, and it is nessasary to have the <head> and <body> tags to do other things.

8 Likes

HTML has an extensive and strict standard. This standard also describes the recovery mechanisms for many parse errors (https://html.spec.whatwg.org/#parse-errors).

Standard is a standard, but browser vendors might decide not to implement certain features, or might implement them in a slightly different way. The standard itself also states that:

The error handling for parse errors is well-defined (that’s the processing rules described throughout this specification), but user agents, while parsing an HTML document, may abort the parser at the first parse error that they encounter for which they do not wish to apply the rules described in this specification.

Because of this when you use invalid HTML code your site might behave unpredictably depending on which web browser your users use. This is a major problem. Write correct HTML.


No. I strongly disagree. Please provide any source that states that doctype is not required in HTML5.

4 Likes

I think what @trevorhodges means is that you can run code in a code editor without them, and it won’t stop you for getting an output.

5 Likes

A typical modern browser, in our operating environment will gladly render HTML if what we give it resembles HTML. We should stipulate that the browser is fully capable of rendering a raw text.

HTML

A quick brown fox jumps over the lazy dog

Save that as a file with .html as the extension and run it in the browser. The text will render in whatever the browser uses as default font-family (likely Times or a variant of it) and font-size around 14 to 16 px.

If we supply raw text that is marked up, we see that the markup is not visually rendered when we run the page.

<p>A quick brown fox jumps over the lazy dog</p>

We will only see the text in the same font and size as before, only now the text is a node of a P-Node. The earlier raw text was a text node of the HTML-Node which the browser supplied in order to construct the DOM.

We can do all sorts of lazy stuff on our own machine. It will still render. In the existing example we have no declared root element but yet we could still get the browser to render our HTML. Neither did we declare a DOCTYPE. What spec is our browser going to default to? HTML 4? or HTML5? It certainly won’t be XHTML.

At this stage we might get away with a TITLE, LINK, or STYLE element, though I’m not going to try. Those elements belong inside a HEAD element.

So we don’t have title bar text, and no external or embedded style sheet. The elements still have the global attribute, style which is inline so even at this raw stage we can style our elements.

<p style="font-size: 18px; color: #008888">A quick brown fox jumps over the lazy dog</p>

Bottom line, we can practically toss out the rule book on our local machine, and I would encourage learners to run lots of these kinds of experiments so they truly get to witness rendering at this grass roots level. In fact I back this up by recommending only working with HTML for an extended period to really work out what well formed, valid and accessible documents are in terms of Standards.

We should not be publishing anything to the web that is not validated (including CSS) . That means we do not relax the spec recommendations, we conform to them.

HTML is declarative. As strictness (or rather succinctness) goes, it doesn’t get any more certain. A parsing error may occur in any number of circumstances, particularly when something is declared that cannot be carried out, or parsed into the DOM. Browsers are famous for trying their hardest to make something out of a bad situation and may even render as we expected, despite having to resort to a computed solution.

Fine and good if we mess around with this on our own machine, but a web facing document should never assume anything. We have no idea what kind of user agents are requesting our documents. All we have is some kind of assurance that they all tend to adhere to the recommendations of working groups so if we do, too, our documents should render as expected, or reasonably, and gracefully degrade to the level of the user agent’s ability to parse and render them.

First rule of web publishing… Just because it looks good on your machines and devices, doesn’t mean it will look good on other machines and devices. All of the past twenty years has been spent getting us to where we are today, and yet the standards are still being tweaked. New HTML specs are evolving steadily. CSS continues to grow. ECMAScript continues to evolve. Standards are at the core of all this advancement.

7 Likes