Understand the page structure: DOCTYPE, html, head and body
You have already opened a complete page. Now we will examine its outer structure carefully, so that you can build the next one without guessing where things belong. We will separate information about a document from the content intended for its reader.
Explain the document declaration and standards mode.
Identify the outer html element and its main language.
Place document information in head and page content in body.
Understand title, character encoding and the basic viewport setting.
Draw the parent-and-child structure of a small document.
A familiar comparison: an assignment
An assignment has information that helps identify it, such as its name and subject, and content that the reader studies. A web document also separates identifying information and its main content. The comparison is only a memory aid: a head element is not a visible cover sheet printed above the body.
Document structure means how the main parts fit together. An outer part can contain smaller parts. We call the containing element a parent and an element directly inside it a child.
For a normal document like our examples, html contains head and body. Head holds document information. Body holds the content of the page.
Keep document information and page content separate
1DOCTYPE: standards mode
2html: outer element
3head: title and document information
4body: main page content
Head and body are siblings inside html. The doctype declaration is outside html; it is not an ordinary element.
Begin with the declaration
HTML fragment — part of a document
<!DOCTYPE html>
This is the short HTML doctype declaration. In a normal HTML document, it tells browsers to use standards mode rather than older compatibility behaviour. Standards mode is the mode in which browsers follow modern document-layout rules rather than trying to imitate old pages.
It is not an ordinary element with an opening and closing tag. Do not write </!DOCTYPE> or place the declaration inside body. We put it at the start of every complete example.
You may see it described as the HTML5 doctype. Modern HTML continues to develop as a living standard; you do not add a version number such as 6 to this declaration. Older documents may have much longer doctypes. You do not need to copy those for our new pages.
The html element: hold the document together
HTML fragment — part of a document
<htmllang="en">
...
</html>
The dots here are a placeholder for the head and body. They are not text to paste into your real file. The html element is the root element: the outermost element containing the document's elements. The doctype declaration sits outside it.
The lang="en" attribute identifies English as the main language. Language information can help pronunciation in screen-reading software and other language-aware tools. It does not translate sentences, choose a font or decide where a visitor lives.
Choose a language value that matches the actual content. A page mainly written in Hindi should identify that language appropriately. Later chapters will explain language changes within a page and text direction.
Metadata means information about a document, rather than its main reading content. The word sounds technical, but a document's title is a familiar example.
Head can contain metadata and references to resources such as style files. Its contents are not normally rendered as the main body content. However, they still affect the page: a title appears in the tab, and style information can change what the body looks like.
Do not put the visible notice heading in head. Do not confuse head with the separate header element. Header is used for introductory content within a document or section; it is taught in the page-sections chapters.
The title element: name this particular page
A useful title helps identify a page among several open tabs or saved bookmarks. Give each ordinary page one clear title inside head. Write text there, not a heading element.
For example, Peer Learning Schedule is more useful than Page 1 when a student has many tabs open. A title should describe the actual document. It does not guarantee a particular position in search results, and a search service may choose different display text.
The title and h1 can use similar words, but one does not replace the other. The browser does not automatically copy title into a visible body heading.
Character encoding: save and declare UTF-8
HTML fragment — part of a document
<metacharset="UTF-8">
The charset declaration identifies the document's character encoding. An encoding tells software how saved bytes represent characters. Keep this declaration near the start of head and save the actual file as UTF-8.
The line cannot repair a file saved in a different encoding by itself. Think of a label on a notebook: the label should agree with what is inside. If they disagree, some characters may display incorrectly.
The meta element is a void element in HTML. That means it has no child content and no closing tag. Write <meta charset="UTF-8">, not <meta charset="UTF-8"></meta>. Chapter 6 explains void elements in more detail.
A first viewport setting
The viewport is the area in which a browser displays a page. On a phone, an older default layout setting can make a page appear as if a wider desktop page has been shrunk down.
width=device-width asks the browser to use the device's width for the layout viewport.
initial-scale=1 sets the initial zoom scale.
Use this as a starting point for pages intended to work on different screen sizes. It does not automatically make wide tables, fixed-width pictures or every future layout fit a phone. Responsive layout needs suitable content and CSS too. Do not add settings that prevent a reader from zooming.
The body element: the page's main content
Put headings, paragraphs, lists and other page content inside body. In our first examples, everything shown in the preview's main white area comes from the heading and paragraph elements inside it.
An ordinary document has one body element. Do not create a new body for each section. Later, you will use elements such as section and article for groups of content within it.
Part
Its job
A mistake to avoid
Doctype declaration
Select modern standards behaviour
Treating it as a visible heading
html
Contain the document elements
Treating language as a translation command
head
Hold information about the document
Placing normal paragraphs here
title
Give the document a name
Assuming it creates the body's heading
meta charset
Identify the saved character encoding
Saving with an inconsistent encoding
body
Hold the page's main content
Creating several bodies for different sections
Complete example: a peer learning schedule
peer-learning.html
<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1"><title>Peer Learning Schedule</title></head><body><h1>This week's peer learning</h1><p>Monday: explain one HTML page to a partner.</p><p>Thursday: check the page together and fix one mistake.</p></body></html>
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
The title names the document. The body contains one h1 and two p elements. Both meta elements belong in head and have no closing tags. The viewport line supports a suitable starting viewport; the page does not yet need a complex layout.
Draw the parent-and-child relationships
Example fragment — use inside a complete program
HTML document
DOCTYPE declaration
html
head
meta: character encoding
meta: viewport
title: Peer Learning Schedule
body
h1: This week's peer learning
p: Monday activity
p: Thursday activity
Head and body are children of html. They are siblings, which means they share a parent. The h1 and both paragraphs are children of body. Indentation in the source makes these relationships easier for a person to follow.
Browsers build a working representation of a document. Developer tools may show elements the browser created while recovering from omitted or invalid markup. That representation is not always an exact copy of the original file's characters.
Some HTML tags can legally be omitted under specific rules. For learning, we will write the document's head, body and closing tags explicitly. A clear teaching convention should not be confused with a claim that every omitted tag is always invalid.
Problem and solution: a heading was placed in head
Problem: a student puts a visible heading inside head and says it must be correct because the browser still displays something.
Solution: put document information in head and the heading in body. A browser may recover from misplaced content, but that recovery does not make the placement a good authoring pattern. Compare the intended source structure, not only the final pixels.
For the peer learning example, title stays in head. The sentence This week's peer learning belongs inside h1 in body. Then check both the tab title and the page heading.
Common mistakes and useful tips
Calling head and header the same element: they have different roles.
Writing a closing meta tag: meta has no closing tag in HTML.
Adding a second body to separate activities: use content sections inside one body later.
Thinking the viewport setting designs the whole page: content and CSS must also adapt.
Using an unclear title on every page: name the specific document.
Assuming browsers must reject every mistake: browsers often try to display something anyway.
Removing page zoom: readers may need it; do not block it as a shortcut to layout work.
Practice questions
Draw the two direct child elements of html in our complete example.
Where should the visible main heading go?
Explain why <meta charset="UTF-8"></meta> is not our correct HTML pattern.
Change only the peer learning document's title. Predict which part of the body changes.
Does the viewport line guarantee that a 2000-pixel-wide picture will fit a phone? Explain.
Write your own complete practice timetable page with language, charset, viewport, title, one heading and two paragraphs.
Check your practice answers
Head and body. Head contains document information; body contains the main page content.
In body, for example inside an h1 element.
Meta is a void element. Write the start tag and its attributes without a closing meta tag.
No body text changes automatically. The document title changes separately from its heading and paragraphs.
No. It configures the viewport, but the picture and layout still need suitable sizing rules.
Start from the complete document pattern, then choose a specific title and your own timetable text. Check the head/body placement and compare the visible output with your intention.
Interview questions
What does the short HTML doctype do?
It tells a browser to use standards mode for a normal HTML document. It is a declaration, not a body element or a visible heading.
What is metadata in simple words?
It is information about the document. A page title and the character-encoding declaration are examples. That information helps tools interpret or identify the page.
Why include a language attribute?
It identifies the main language of the content and helps language-aware tools, including assistive tools. It must match the authored text and does not translate it.
Why can a page look acceptable even when the source has mistakes?
Browsers have rules for recovering from many markup errors. Displaying content is therefore not the same as proving that the authored document is valid or easy to use.
Quick revision
Put the doctype declaration before the html element.
Html contains head and body in our explicit document pattern.
Head holds document information; body holds the main content.
Use a meaningful title and identify the document's language.
Save and declare UTF-8 consistently.
A viewport setting is a starting point, not a complete responsive design.