Comments, HTML, Head and its Children, Body

Before we get into the body elements, let me deal with what doesn't show up in the webpage itself. I'll deal with the following elements:

  1. Comments
  2. (X)HTML Root Element*
  3. Head Element*
    1. Metadata Element
    2. Link Element
    3. Base URI Element
    4. Title Element*
  4. Body Element*

*I know I've already mentioned these, but I am going to recap them and give some more information about them, such as what attributes are allowable here. Several of them allow the core and language attributes, others do not. The core and language attributes are rare enough amongst the head elements that I will mention them with each.

Comments

A comment doesn't show up on the screen at all, nor does it affect your webpage in any way. In fact, the only way to see it is to look at the code itself. Comments are used for two things:

A comment always starts with the character sequence <!-- and always ends with the character sequence -->. There should always. be a space after the opening double dash and before the closing double dash. Double dashes between the opening and closing character sequence should be avoided lest the browser misinterpret that as the end of the comment.

Comments can go pretty much anywhere—even before the Doctype.

Example Comment
<!-- This is an HTML comment. -->

The (X)HTML Root Element

Element Name: html

As stated before, the html element is the root element of everything, with only the Doctype (and XML declaration, if you're using XHTML) existing outside of it.

It can also have only two child elements: head and body, in that order.

Required Attributes

xmlns (XHTML only)
This acts as an identifier for an XML-based language. Without it, a browser may not be able to read that markup language. The value for XHTML is http://www.w3.org/1999/xhtml.

Implied Attributes

The Language Attributes
lang, dir
xml:lang (XHTML only)
This works like the lang attribute but will work with any XML-based language.

The Head Element

Element Name: head

The head does the thinking for the webpage. It contains information about the webpage that both the browser and the server can use.

Implied Attributes

The Language Attributes
lang, dir

The Metadata Element

Element Name: meta

This element's a bit tricky to explain, so I'll use a few comparisons. A metamovie is a movie about movies. A metabook is a book about books. Therefore, metadata is data about data. In this context, it's information about your webpage. You can have as many of these elements as you like.

This is an empty element. It has no end tag.

Implied Attributes

The Language Attributes
lang, dir
http-equiv
This includes an HTTP response header, which tells the server what this page contains and therefore what the server should tell the browser.
name
This describes other properties of the webpage, and you can make these properties up as you wish. Commonly used properties include:
Author
Who's creating the webpage?
Keywords
A few short keywords to help search engines find your webpage.
Date
The date your webpage was written
And so on and so forth.

Required Attributes

content
As the http-equiv and name attributes name the properties of a webpage, the content attribute gives those properties their value.

Specifying Character Encoding

If you recall the character encoding warning I told you about in Your First Webpage, here's how to fix it: include a meta element in the head. The meta element should have a http-equiv attribute specifying the content type, and a content attribute saying that the page is a text page to be interpreted as HTML and that the character set is UTF-8. This can be set in one or two elements (usually one):

Setting Content Type And Character Encoding
For HTML
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
For XHTML
<meta http-equiv="Content-Type" content="application/xhtml+xml" />
<meta http-equiv="Content-Type" content="application/xml" />
<meta http-equiv="Content-Type" content="text/xml" />
<meta http-equiv="Content-Type" content="text/html" />

Note that I omitted the charset information from the XHTML document's meta elements—that information is already contained in the XML declaration (which you should be using with XHTML).

The Base URI Element Element

Element Name: base

Remember in the hyperlinks chapter I talked about relative URIs? This element overrides the normal way relative URIs are calculated by setting a default URI (either relative or absolute) to work with. Because only URIs that come after this element are affected, it's important to have this before any of your link elements.

This is an empty element. It has no end tag. I mentioned that there were only five single-use element in (X)HTML. Four of them are, of course, html, title, head, and body. The base element is the fifth.

Required Attributes

href
Contains the base URI. This is the base element's only attribute.

The Title Element

Element Name: title

Again, the title element contains the information contained in the browser title bar. If you use markup in the title element's contents, it will show up as plain text rather than having any effect whatsoëver. This one of the few non-empty elements that have no child elements.

Implied Attributes

The Language Attributes
lang, dir

To display what I've been talking about, below is an example head element with its child elements.

Example head code, using all the above elements
<head lang="en" dir="ltr">
<!-- Meta Tags, which give both server and user agent information about the HTML document/webpage. -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Author" content="John Kraaikamp">
<meta name="Keywords" content="HTML, Tutorial, Webpages, how to">
<!-- The base element tells the browser what URI to use for determining relative URIs. -->
<base href="http://www.example.org">
<!-- Link elements establish the relationship between different documents. Here, the class attributes give me some idea of what each link element is for. -->
<link href="./Chapters/Part_2/Website_Chap007.html" rel="Prev" rev="Next" type="text/html" class="chapter_links">
<link href="./Chapters/Part_2/Website_Chap009.html" rel="Prev" rev="Next" type="text/html" class="chapter_links">
<link href="./contents.html" rel="Contents" rev="Chapter" type="text/html">
<!-- The title element displays text in the user agent's title bar. -->
<title lang="en" dir="ltr">Your Page Title</title>
</head>

The Body Element

Element Name: body

The body element contains all the information that appears in the window. As the head does the thinking, the body does the doing for the webpage.

Implied Attributes

The Language Attributes
lang, dir
The Core Attributes
class, id, style, title

On To The Body Elements

Unsurprisingly, the body element has the widest range descendant elements, and the largest group of these are the inline elements.

For the sake of simplicity, further code examples will not display the html element, head element and its children, or the body element. However, what code is displayed in the future is intended to be nested inside the body element:

Template For Future Examples
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Example Title</title>
</head>
<body>
<-- Example Code Here. -->
</body>
</html>