HTML, CSS & JavaScript

Chapter 2
HTML Document Structure and Core Elements

Certificate in Full stack programming
HTML, CSS & JavaScript   •   Certificate

HTML Document Structure and Core Elements

Introduction

HTML (HyperText Markup Language) is the standard markup language used to structure content on the World Wide Web. It does not primarily describe visual appearance; instead, it communicates the meaning and organization of content to browsers, assistive technologies, search engines, and other software. CSS is used mainly for presentation, while JavaScript is used mainly for behavior. HTML provides the document structure on which both technologies depend.

A well-constructed HTML document enables a browser to identify the page title, headings, paragraphs, navigation links, images, tabular data, and other content. Correct structure also improves accessibility, search-engine interpretation, maintainability, and compatibility with web standards.

This chapter develops the practical foundations required to create valid HTML documents. It explains document metadata, elements, attributes, nesting, text structure, lists, links, images, tables, file paths, validation, and standards-based practices.

Learning Objectives

By the end of this chapter, you should be able to:

  • explain the role of HTML in a web application;
  • construct a valid HTML5 document;
  • distinguish elements, tags, attributes, and content;
  • apply correct nesting and document hierarchy;
  • use metadata, headings, paragraphs, lists, links, and images;
  • construct accessible and meaningful tables;
  • select appropriate relative and absolute file paths;
  • identify common markup errors and correct them;
  • apply semantic and accessibility-oriented HTML practices.

1. HTML as a Structural Language

HTML represents a document as a tree of related nodes. A page may contain a document root, a head section, and a body section. The body may contain a header, navigation, main content, sections, articles, side content, and a footer. This relationship is often called the document tree or Document Object Model (DOM) structure.

HTML uses markup rather than ordinary prose alone. Markup adds information about the role of content. For example, the text “Course Outline” could be ordinary text, but placing it inside a heading element tells software that it is a heading.

An HTML element commonly consists of an opening tag, content, and a closing tag:

<p>HTML gives structure to web content.</p>

Here, <p> is the opening tag, the sentence is the content, and </p> is the closing tag. Together they form a paragraph element.

Some elements are void elements: they do not contain content and do not have closing tags. Examples include <meta>, <link>, <img>, <br>, and <input>.

Elements, Tags, and Attributes

The terms element and tag are related but not identical. A tag is the markup notation such as <p> or </p>. An element includes the complete structure and its content.

Attributes provide additional information about an element. They are written in the opening tag, normally as a name-value pair:

<a href="contact.html" class="navigation-link">Contact</a>

In this example, href identifies the destination and class identifies a classification that CSS or JavaScript may use. Attribute values should normally be enclosed in quotation marks.

Common attribute categories include:

CategoryExamplesPurpose
Global attributesid, class, title, langApply to many HTML elements
Linking attributeshref, target, relControl links and relationships
Media attributessrc, alt, width, heightDescribe or control media
Table attributescolspan, rowspanControl cell spanning
Form attributesname, value, requiredSupport data collection

An id should be unique within a document. A class may be used on multiple elements.

2. The Basic HTML5 Document

A minimal standards-based HTML document is:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Web Development Fundamentals</title>
  </head>
  <body>
    <h1>Web Development Fundamentals</h1>
    <p>This page introduces the structure of an HTML document.</p>
  </body>
</html>

Each part has a distinct purpose.

The Document Type Declaration

<!doctype html> tells the browser to use modern standards mode. It is not an HTML element and does not require a closing tag. Omitting it can cause browsers to enter a legacy compatibility mode, producing inconsistent rendering.

The html Element

The <html> element is the root element. The lang="en" attribute identifies the principal language as English. Language information assists screen readers, translation tools, search engines, and speech synthesis systems.

The head Element

The head contains metadata: information about the document that is not normally displayed as page content. Common head elements include:

  • <meta charset="utf-8">, which declares the character encoding;
  • <meta name="viewport" content="width=device-width, initial-scale=1">, which supports appropriate layout on mobile devices;
  • <title>, which supplies the browser-tab title and is important for bookmarks and search results;
  • <meta name="description" content="...">, which can provide a concise page description;
  • <link rel="stylesheet" href="styles.css">, which connects an external CSS file;
  • <script src="app.js" defer></script>, which connects JavaScript while allowing HTML parsing to continue.

Metadata must be accurate. A misleading title or description reduces usability and may harm search visibility.

The body Element

The body contains content presented to users. It should contain meaningful structural elements rather than a collection of generic containers. For example:

<body>
  <header>
    <h1>City Library</h1>
    <nav aria-label="Primary navigation">
      <a href="index.html">Home</a>
      <a href="catalog.html">Catalog</a>
    </nav>
  </header>

  <main>
    <h2>Featured Resources</h2>
    <p>Explore books, journals, and digital learning materials.</p>
  </main>

  <footer>
    <p>© 2025 City Library</p>
  </footer>
</body>

The elements <header>, <nav>, <main>, and <footer> are semantic elements. They communicate the role of each region more clearly than repeated <div> elements.

3. Nesting and Document Hierarchy

Nesting means placing one element inside another. Elements must be closed in the reverse order in which they were opened:

<p>This is <strong>important</strong> information.</p>

The <strong> element opens and closes inside the paragraph. Incorrect nesting produces ambiguous structure:

<!-- Incorrect -->
<p>This is <strong>important.</p></strong>

Indentation does not change browser interpretation, but consistent indentation makes nesting visible to developers. Each heading level should reflect hierarchy. A document normally begins with one principal <h1>, followed by <h2> headings for major sections and <h3> headings for subsections.

Heading levels should not be selected merely because a particular size is desired. Visual appearance belongs to CSS. Structural level belongs to HTML.

4. Text Content and Semantic Meaning

Headings and Paragraphs

Headings divide content into a meaningful outline:

<h1>Introduction to Programming</h1>
<h2>Programming Languages</h2>
<p>A programming language provides rules for expressing instructions.</p>
<h2>Problem Solving</h2>
<p>Programmers analyze requirements before writing code.</p>

Paragraphs should contain complete units of prose. Do not use repeated <br> elements to create spacing between paragraphs. Use separate <p> elements and control visual spacing with CSS.

Emphasis and Importance

<strong> indicates strong importance, while <em> indicates emphasis. These elements have semantic meaning and are not merely instructions to make text bold or italic. For purely visual styling, CSS is more appropriate.

Other useful text elements include <code> for short code fragments, <mark> for relevant highlighted text, <abbr> for abbreviations, and <blockquote> for extended quotations.

<p>Use the <code>alt</code> attribute to describe an image.</p>
<p><abbr title="HyperText Markup Language">HTML</abbr> structures documents.</p>

5. Lists

Lists communicate collections of related items.

Unordered Lists

An unordered list is appropriate when order is not significant:

<ul>
  <li>HTML</li>
  <li>CSS</li>
  <li>JavaScript</li>
</ul>

Ordered Lists

An ordered list is appropriate when sequence matters:

<ol>
  <li>Identify the requirements.</li>
  <li>Design the page structure.</li>
  <li>Test the implementation.</li>
</ol>

The start and reversed attributes can alter numbering when there is a genuine semantic reason, but ordinary numbering should normally be left to the browser and CSS.

Description Lists

A description list represents terms and their descriptions:

<dl>
  <dt>Element</dt>
  <dd>A structured component of an HTML document.</dd>
  <dt>Attribute</dt>
  <dd>Additional information supplied in an element's opening tag.</dd>
</dl>

Description lists are suitable for glossaries, metadata, and question-and-answer pairs where each term has one or more descriptions.

6. Links and File Paths

The anchor element creates hyperlinks. Its essential destination attribute is href:

<a href="about.html">About the course</a>

A link should describe its destination. “Read more” alone may be unclear when several such links occur on a page. Prefer “Read the accessibility policy” or similar wording.

Relative Paths

A relative path is interpreted from the location of the current HTML file. Suppose a project has this structure:

project/
├── index.html
├── pages/
│   └── contact.html
├── images/
│   └── campus.jpg
└── css/
    └── styles.css

From index.html, the relevant paths are:

<a href="pages/contact.html">Contact</a>
<img src="images/campus.jpg" alt="University campus">
<link rel="stylesheet" href="css/styles.css">

From pages/contact.html, the same image is reached by moving one directory upward:

<img src="../images/campus.jpg" alt="University campus">

The notation .. means the parent directory, while . means the current directory. A leading slash such as /images/campus.jpg generally refers to a path from the website root, not necessarily from the current file.

Absolute URLs include a complete scheme and domain:

<a href="https://www.example.edu">Institutional website</a>

Use HTTPS for external resources where possible. Avoid linking to files using a local computer path such as C:\Users\Student\Desktop\image.jpg; that path will not work for other users or on a web server.

File and directory names should avoid unnecessary spaces and inconsistent capitalization. Many web servers are case-sensitive, so Images/photo.jpg and images/photo.jpg may refer to different paths.

Fragment Links

A fragment identifier links to an element with a matching id:

<a href="#assessment">Go to assessment</a>

<h2 id="assessment">Assessment</h2>

The id value must be unique in the page.

7. Images and Alternative Text

The <img> element embeds an image. It is a void element and requires a source and alternative text:

<img src="images/laboratory.jpg"
     alt="Students conducting an experiment in a laboratory">

The alt attribute is essential for users who cannot see the image and for situations in which the image cannot load. Alternative text should communicate the image’s purpose, not merely repeat its filename.

For a decorative image that conveys no information, use an empty alternative text value:

<img src="images/divider.svg" alt="">

Do not omit alt unless the image is being handled in a different, carefully considered way. If an image contains important text or a complex diagram, provide the necessary information in nearby text or a longer description.

The <figure> and <figcaption> elements associate an image with a visible caption:

<figure>
  <img src="images/network.png" alt="Three computers connected to a central server">
  <figcaption>A basic client-server network.</figcaption>
</figure>

Width and height attributes can help browsers reserve space before an image loads:

<img src="images/logo.png" alt="Training Institute logo" width="240" height="80">

These attributes should represent the image’s intrinsic dimensions or an intentional aspect ratio. CSS is generally preferred for responsive sizing.

8. Tables for Tabular Data

Tables are appropriate when information has relationships between rows and columns. They should not be used to arrange an entire page layout.

A basic accessible table is:

<table>
  <caption>Workshop timetable</caption>
  <thead>
    <tr>
      <th scope="col">Day</th>
      <th scope="col">Topic</th>
      <th scope="col">Room</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">Monday</th>
      <td>HTML structure</td>
      <td>Lab 1</td>
    </tr>
    <tr>
      <th scope="row">Wednesday</th>
      <td>CSS foundations</td>
      <td>Lab 1</td>
    </tr>
  </tbody>
</table>

Important table elements include:

  • <table>: the complete table;
  • <caption>: a title or concise description;
  • <thead>: header rows;
  • <tbody>: principal data rows;
  • <tfoot>: summary or footer rows;
  • <tr>: a table row;
  • <th>: a header cell;
  • <td>: a data cell.

The scope attribute identifies whether a header applies to a column or row. This assists screen readers in associating data with the correct headings.

colspan allows a cell to span columns and rowspan allows it to span rows. They should be used only when the data relationship requires them. Excessive spanning can make tables difficult to understand, especially on small screens.

9. Semantic Layout Elements

HTML5 introduced elements that describe common page regions:

ElementTypical meaning
<header>Introductory content for a page or section
<nav>A major set of navigation links
<main>The unique primary content of the document
<section>A thematic grouping, usually with a heading
<article>Self-contained content that could stand independently
<aside>Related but secondary content
<footer>Closing information for a page or section

A <section> should generally have a heading. An <article> may represent a news item, blog entry, product review, or forum post. A <div> remains useful as a generic container when no more specific semantic element applies, but it should not replace meaningful HTML elements.

Semantic HTML improves communication between developers and user agents. It also supports keyboard navigation and assistive technologies when combined with sound content structure.

10. Validity, Standards, and Accessibility

Valid HTML follows the syntax and content rules defined by current web standards. Validation tools can identify unclosed elements, invalid attributes, duplicate IDs, and other problems. Validation does not guarantee that a page is usable, attractive, secure, or accessible, but it is an important quality check.

Common standards-based practices include:

  1. Declare <!doctype html>.
  2. Set the document language with lang.
  3. Use UTF-8 character encoding.
  4. Give every page a meaningful <title>.
  5. Maintain logical heading order.
  6. Provide useful alternative text for informative images.
  7. Use labels and semantic elements appropriately.
  8. Ensure links have descriptive text.
  9. Keep IDs unique.
  10. Use HTML for meaning and CSS for presentation.
  11. Test paths from the actual file location.
  12. Validate markup and test with keyboard navigation.

Accessibility is not a decorative enhancement. It is part of professional web development. Accessible HTML supports people using screen readers, keyboard-only navigation, magnification, voice control, or temporary access adaptations. In many jurisdictions, accessibility is also connected to legal and institutional obligations.

Avoid deprecated presentation elements such as <font> and layout tables. Avoid using characters such as repeated spaces to control layout. Avoid placing a heading inside a paragraph or using a heading solely to produce large text.

11. A Complete Practical Example

The following example combines metadata, semantic structure, headings, a list, a link, an image, and a table:

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Information about the university coding workshop">
    <title>University Coding Workshop</title>
    <link rel="stylesheet" href="css/styles.css">
  </head>
  <body>
    <header>
      <h1>University Coding Workshop</h1>
      <nav aria-label="Primary navigation">
        <a href="index.html">Home</a>
        <a href="pages/register.html">Registration</a>
        <a href="#schedule">Schedule</a>
      </nav>
    </header>

    <main>
      <section>
        <h2>About the Workshop</h2>
        <figure>
          <img src="images/workshop.jpg"
               alt="Students working together at laptop computers">
          <figcaption>Collaborative programming practice.</figcaption>
        </figure>
        <p>The workshop introduces practical techniques for building web pages.</p>
        <ul>
          <li>HTML document structure</li>
          <li>CSS layout fundamentals</li>
          <li>JavaScript interaction</li>
        </ul>
      </section>

      <section id="schedule">
        <h2>Schedule</h2>
        <table>
          <caption>Workshop sessions</caption>
          <thead>
            <tr>
              <th scope="col">Time</th>
              <th scope="col">Session</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th scope="row">09:00</th>
              <td>HTML structure</td>
            </tr>
            <tr>
              <th scope="row">11:00</th>
              <td>CSS foundations</td>
            </tr>
          </tbody>
        </table>
      </section>
    </main>

    <footer>
      <p>Contact the <a href="mailto:[email protected]">training team</a>.</p>
    </footer>
  </body>
</html>

This example is not complete merely because it displays in a browser. It is strong because its structure communicates meaning, its paths reflect a plausible project, its image has alternative text, its table has headers, and its navigation has descriptive destinations.

12. Applied Case: Building a Course Information Page

Background

A fictional training institute wants a course information page for a certificate in full stack programming. The page must be usable on desktop and mobile devices and understandable to prospective learners using assistive technologies.

Problem

An initial page uses <div> elements for every region, an image without alt, headings selected for visual size, and a table without header cells. Links use vague text such as “Click here.” Some images fail because paths were copied from the developer’s local computer.

Analysis

The page may appear acceptable visually, but it has structural and usability weaknesses. Screen readers cannot easily identify navigation or the main content. Users cannot understand the purpose of the image when it fails to load. The table’s relationships are unclear. Local paths are not portable to a server.

Approach and Solution

The developer replaces generic containers with <header>, <nav>, <main>, <section>, and <footer>. A single meaningful <h1> is added, followed by logically ordered <h2> headings. The image receives concise alternative text. Links are rewritten as “View the course schedule” and “Download the admissions guide.” The table receives a caption, column headings, row headings, and appropriate scope values. Relative paths are calculated from each HTML file’s location and tested on a local server.

Outcome

The page becomes easier to maintain, more understandable to assistive technologies, and more portable between development and production environments. Validation identifies a duplicate id, which is corrected before deployment.

Learning Points

  • Visual appearance does not prove structural correctness.
  • Semantic elements communicate purpose.
  • Relative paths depend on the current document location.
  • Accessibility should be included during design, not added at the end.
  • Validation and testing are complementary activities.

13. Development Workflow and Troubleshooting

A practical workflow is:

  1. Define the page purpose and content.
  2. Sketch the semantic structure before styling.
  3. Create the HTML5 skeleton.
  4. Add metadata and a meaningful title.
  5. Add headings and content in logical order.
  6. Insert lists, links, images, and tables where appropriate.
  7. Check nesting, quotation marks, and closing tags.
  8. Test every path and link.
  9. Validate the document.
  10. Test keyboard access, mobile layout, and alternative text.

Frequent problems include:

ProblemLikely causeCorrective action
Image does not appearIncorrect src or filename caseCheck path from current file
CSS does not loadIncorrect hrefVerify directory and filename
Page title is missingNo <title> in <head>Add a meaningful title
Screen reader receives little contextExcessive generic containersUse semantic elements and headings
Table is confusingMissing headers or captionAdd <caption>, <th>, and scope
Link is unclearVague link textDescribe the destination
Unexpected layout modeMissing doctypeAdd <!doctype html>

Browser developer tools can inspect the DOM and network requests. They are useful for locating failed resources, but they do not replace careful source review or accessibility testing.

14. Contemporary Practice and Professional Implications

Modern HTML development increasingly emphasizes semantic structure, accessibility, responsive design, performance, and maintainability. Frameworks and component systems may generate HTML, but developers still need to understand the resulting markup. Poorly generated or misused components can produce incorrect headings, inaccessible controls, invalid nesting, or confusing landmarks.

Search engines also depend on meaningful document structure. Semantic HTML does not guarantee a particular ranking, but clear titles, headings, links, and content relationships make a page easier for software to interpret.

Security is also relevant. HTML itself is not a complete security mechanism, but developers should avoid inserting untrusted content into documents without appropriate sanitization. External links opened in new browsing contexts should be considered carefully, and external resources should use trustworthy origins. Privacy-conscious development requires restraint in metadata and third-party resources.

Artificial intelligence tools can assist with generating markup, but generated HTML must be reviewed. A tool may invent paths, omit alternative text, use inappropriate headings, or produce redundant elements. Professional responsibility remains with the developer who deploys the page.

Challenges and Limitations

HTML provides structure, but it does not solve every interface problem. CSS is required for visual layout and responsive styling. JavaScript is required for many interactive behaviors. HTML semantics may also be misunderstood: a semantic element does not automatically guarantee accessibility if its content, keyboard behavior, or focus management is defective.

Tables can become difficult to use on narrow screens. Large data tables may require careful responsive strategies, summaries, or alternative presentations. Images require editorial judgment; an inaccurate alternative description may be worse than a concise one. Validation catches syntax issues but cannot judge whether the content is truthful, understandable, or ethically appropriate.

Chapter Summary

HTML is the structural language of the web. A valid document begins with a doctype, a root <html> element, a language declaration, a metadata-containing <head>, and a content-containing <body>. Elements express meaning, attributes provide additional information, and correct nesting creates a reliable document tree.

Headings establish hierarchy, paragraphs represent prose, lists represent collections, anchors create links, and images require suitable alternative text. Tables should be reserved for tabular relationships and should include captions and header cells. Relative file paths are interpreted from the current document’s location, making project organization and case-sensitive filenames important.

Semantic elements such as <header>, <nav>, <main>, <section>, <article>, <aside>, and <footer> improve clarity and accessibility. Standards-based development combines valid syntax with meaningful content, keyboard testing, path verification, validation, and responsible accessibility practice.

Key Terms

Element: A complete HTML structure consisting of markup and, where applicable, content.

Tag: Markup notation such as <p> or </p> used to create or close an element.

Attribute: Additional information placed in an element’s opening tag.

Nesting: Placing one HTML element inside another.

Void element: An element that does not contain content or require a closing tag.

Doctype: A declaration that tells the browser to use the modern standards mode.

Metadata: Information about a document, generally placed in the <head>.

Semantic HTML: Markup that communicates the purpose and meaning of content.

DOM: The document tree representation created from HTML and made available to browser software.

Heading hierarchy: The ordered relationship among headings from <h1> through <h6>.

Relative path: A resource path interpreted from the current document’s location.

Absolute URL: A complete web address including its scheme and domain.

Fragment identifier: A link reference beginning with # that targets an element’s id.

Alternative text: Text that communicates the purpose of an image when it cannot be seen.

Tabular data: Information organized through meaningful row and column relationships.

Caption: A title or description associated with a table or figure.

Validation: Checking markup against relevant syntax and standards rules.

Accessibility: Designing content so that people with diverse abilities can use it.

Global attribute: An attribute available to many types of HTML elements.

Deprecated markup: Older markup that should generally be replaced by current standards-based methods.

Review Questions

Short Answer Questions

  1. What is the purpose of the <!doctype html> declaration?
  2. Distinguish between an HTML element and an HTML attribute.
  3. Why is the alt attribute important for images?
  4. What is the difference between an unordered list and an ordered list?
  5. Why should tables not normally be used for page layout?

Long Answer Questions

  1. Explain the structure of a valid HTML5 document and the purpose of its principal elements.
  2. Describe how headings, paragraphs, semantic layout elements, and nesting contribute to document meaning.
  3. Explain relative and absolute paths using an example project directory.
  4. Describe how to construct an accessible HTML table, including the roles of captions, header cells, and scope.
  5. Discuss how validation, accessibility testing, and semantic HTML contribute to professional web development.

Analytical / Application Questions

  1. A page displays an image during local development but not after deployment. The source uses C:\Users\Student\Desktop\logo.png. Diagnose the problem and propose a portable solution.
  2. Review a page that uses <h1> for every heading, vague links labelled “Click here,” and a table containing only <td> cells. Identify the structural and accessibility problems and recommend specific corrections.
  3. Design the semantic HTML structure for a course page containing a site header, navigation, course description, instructor profile, weekly schedule, registration link, and footer. Justify the elements you select.

Multiple Choice Questions with Answers

Question 1

Which element contains the visible main content of an HTML document?

A. <head>

B. <body>

C. <meta>

D. <title>

Correct Answer: B. <body>

Question 2

What is the primary purpose of <!doctype html>?

A. To load a CSS file

B. To define the page language

C. To request modern standards mode

D. To create the document title

Correct Answer: C. To request modern standards mode

Question 3

Which attribute provides the destination of a hyperlink?

A. src

B. alt

C. href

D. rel

Correct Answer: C. href

Question 4

From pages/contact.html, which path reaches images/logo.png when both directories are inside the same project directory?

A. images/logo.png

B. ../images/logo.png

C. ../../images/logo.png

D. contact/images/logo.png

Correct Answer: B. ../images/logo.png

Question 5

Which element is most appropriate for a major group of navigation links?

A. <nav>

B. <aside>

C. <figure>

D. <footer>

Correct Answer: A. <nav>

Question 6

Which markup correctly identifies a table column heading?

A. <td scope="col">Name</td>

B. <th scope="col">Name</th>

C. <header scope="column">Name</header>

D. <caption scope="col">Name</caption>

Correct Answer: B. <th scope="col">Name</th>

Question 7

What should alternative text for an informative image primarily communicate?

A. The image’s file size

B. The image’s purpose or relevant content

C. The image’s CSS class

D. The image’s upload date

Correct Answer: B. The image’s purpose or relevant content

Question 8

Which statement about an id attribute is correct?

A. It must be unique within the document

B. It can contain only numbers

C. It is used only for images

D. It must be repeated on every heading

Correct Answer: A. It must be unique within the document

Question 9

Which element represents a self-contained piece of content that could stand independently?

A. <article>

B. <br>

C. <span>

D. <link>

Correct Answer: A. <article>

Question 10

Which practice best supports standards-based HTML development?

A. Using headings only to create large text

B. Using local computer paths for images

C. Using semantic elements and validating the markup

D. Using tables to control the entire page layout

Correct Answer: C. Using semantic elements and validating the markup

HTML, CSS & JavaScript
Chapter 2 of 15