HyperText Markup Language/Head and Body

From Wikibooks, open books for an open world
Jump to: navigation, search

An HTML file is divided into two basic sections: the head and the body, each demarcated by their respective tags. Thus, the essential structure of an HTML document looks like this :

<!DOCTYPE html>
<html lang="..."> 
   <head>
        ...
   </head>
   <body>
        ...
   </body>
</html>

The HEAD element[edit]

All data in the head section of an HTML document is considered "meta-data", meaning "data about data". The information in this section is not normally displayed directly. Instead elements such as style affect the appearance of other elements in the document. Some items in the head section may be used by programs such as search engines to learn more about the page for reference purposes.

The head element should always contain a title element which sets the title commonly displayed by the web browser in the title bar of the window. Here is an example of the use of the title element:

<head>
   <title>This is the Title</title>
</head>

There can only be one title in the header section.

There may be any number of the following elements inside the head element:

style 
Used to embed style rules in a document. In most cases, a consistent look across multiple web pages is desireed, so style is specified in a separate stylesheet file, linked using the link element. Thus, style is used in the head when the style applies to this page only.
link 
Used to link the page to various external files, such as a style sheet or the location of the RSS feed for the page. The type of link is set using the rel attribute. The type attribute specifies the MIME type of the document found at the location given by the href attribute. This allows the browser to ignore links to MIME types it does not support. Examples:
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="alternate" type="application/rss+xml" href="rss.aspx" title="RSS 2.0">
script 
Used to link to an external Javascript file or to embed Javascript in the page. Linking to an external file is the preferred technique in real web pages though many examples embed the script for simplicity.
meta 
Used to set additional meta-data properties for the HTML document, such as related keywords, etc. Examples:
<meta charset=utf-8">
<meta name="keywords" content="web, HTML, markup, hypertext">
object 
Embeds a generic object. This element is not commonly used in the head, but rather in the body section.

There may also be a single base element. This element sets the base URI for resolving relative URIs. It is rarely necessary to use this element.

The TITLE element[edit]

The title element contains your document title and identifies its contents in a global context. The title is typically displayed in the title bar at the top of the browser's window.
It is also displayed on the bookmark list of the browser.
Title is also used to identify your page for search engines.
Example: <html> <head> <title>Some Amazing Web Page</title> </head> </html>

The BODY element[edit]

Unlike the head element, any plain text placed between the <body> tags will be displayed on the web page by the browser.

What to avoid. The following example is bad practice:

<body text='black' link='red' alink='pink' vlink='blue'
      bgcolor='#DDDDDD' background='wallpaper.gif'>
   ...
</body>

The current standard is HTML5, and text, link, alink, vlink, bgcolor and background attributes have all been long deprecated. You may find them in older files, but they should not be used now. They have been superseded by the CSS rules given below (using these rules is discussed in a later section HyperText Markup Language/CSS). The values from the previous example have been used as examples in these rules.

text 
body { color:black }
bgcolor 
body { background-color:#DDDDDD }
background 
body { background-image: url(wallpaper.gif) }
link 
a:link { color:red }
alink 
a:active { color:pink } (an active link is a link that is being clicked or has the keyboard focus)
vlink 
a:visited { color:blue }
hover (not an html attribute) 
a:hover { color:green } ('hover' is the style of a link when the mouse pointer is over it)