HTML, CSS, JavaScript Basics

Chapter 3
HTML Documents and Semantic Structure

Bachelors Program in Full Stack Development
HTML, CSS, JavaScript Basics   •   Bachelor

HTML Documents and Semantic Structure

Introduction

HTML (HyperText Markup Language) is the standard language used to describe the structure and meaning of content on the Web. It is not primarily a programming language and should not be confused with CSS or JavaScript. HTML identifies what content is: a heading, paragraph, navigation link, list, image, form, article, or other structural unit. CSS controls presentation, while JavaScript provides behavior and interaction. This division is commonly called separation of concerns.

A well-structured HTML document benefits users, developers, browsers, search engines, assistive technologies, and automated tools. Semantic HTML communicates the purpose of content rather than merely its visual appearance. For example, a navigation menu should be marked up with `nav`, not simply with a styled `div`. This chapter explains how HTML documents are constructed, how browsers interpret them as trees, and how semantic structure supports accessibility, maintainability, discoverability, and professional front-end development.

Learning Objectives

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

  • Construct a standards-compliant HTML document.
  • Explain the roles of metadata, the document head, and the document body.
  • Use headings, paragraphs, links, lists, and landmarks appropriately.
  • Distinguish semantic elements from generic containers.
  • Describe the HTML document tree and parent–child relationships.
  • Apply separation of concerns when combining HTML, CSS, and JavaScript.
  • Evaluate common structural and accessibility errors.
  • Design a meaningful semantic page for a realistic application.

1. The HTML Document as a Structured Resource

A web page is delivered as a resource, usually identified by a URL. The browser retrieves the resource, parses its HTML, constructs an internal representation of the document, and then combines it with CSS and JavaScript. The result is rendered for the user and exposed to assistive technologies through accessibility information derived partly from the HTML structure.

A minimal modern document is:

```html

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Accessible Web Page</title>

</head>

<body>

<h1>Welcome</h1>

<p>This is a structured HTML document.</p>

</body>

</html>

```

The `<!doctype html>` declaration tells the browser to use standards mode. It is a declaration rather than a normal HTML element. The root `html` element contains the complete document. Its `lang` attribute identifies the primary language, which assists screen readers, translation tools, search engines, and pronunciation systems.

The `head` contains information about the document and references to external resources. The `body` contains the content presented to users. Although some elements may appear visually invisible, they still influence interpretation, search, accessibility, or browser behavior.

1.1 Elements, Tags, and Attributes

An HTML element generally consists of a start tag, content, and an end tag:

```html

<p>Semantic structure improves communication.</p>

```

Here, `p` is the element name. The opening and closing tags delimit the paragraph. Some elements are void elements and do not have closing tags. Examples include `meta`, `link`, `img`, `br`, and `input`:

```html

<img src="logo.png" alt="University logo">

```

Attributes provide additional information or modify an element's meaning. In the example above, `src` identifies the image resource and `alt` supplies alternative text. Attributes should be quoted and should be valid for the element on which they occur.

HTML is generally case-insensitive for element names, but professional code uses lowercase names and consistent formatting. HTML parsers are error-tolerant, but relying on error recovery is unsafe because browsers may interpret invalid markup differently than expected. Standards-compliant markup is therefore a quality and interoperability requirement.

2. Metadata and the Document Head

Metadata describes the document rather than forming its main visible content. Important metadata includes:

  • `charset`: declares the character encoding.
  • `viewport`: influences layout on mobile devices.
  • `title`: identifies the page in browser tabs, history, bookmarks, and search results.
  • `meta name="description"`: may provide a summary used by search systems.
  • `link`: connects stylesheets, icons, and other external resources.
  • `script`: loads or embeds JavaScript, subject to loading and security considerations.

A typical head is:

```html

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<meta name="description" content="A guide to semantic HTML for beginning developers">

<link rel="stylesheet" href="styles.css">

<title>Semantic HTML Guide</title>

</head>

```

The character encoding declaration should appear early in the document. UTF-8 is the normal choice because it supports a wide range of writing systems and symbols. The viewport declaration helps a responsive layout use the device's logical width rather than treating a mobile screen as a narrow window into a desktop-sized page.

The `title` element is different from a visible `h1`. The title identifies the document as a whole, while the `h1` identifies its primary visible topic. A page should normally have one meaningful title and one principal heading, although the HTML specification does not impose a simplistic universal “one heading only” rule. The correct structure depends on the document's content and hierarchy.

3. Headings, Paragraphs, and Text Structure

Headings communicate hierarchy. HTML provides `h1` through `h6`, with `h1` representing the highest level and `h6` the lowest. Heading levels should reflect logical nesting, not merely desired font sizes:

```html

<h1>Web Accessibility</h1>

<h2>Perceivable Content</h2>

<h3>Text Alternatives</h3>

<h2>Operable Interfaces</h2>

```

A heading should introduce or label a section. Developers should not select a heading solely because a browser's default style looks attractive. CSS should control visual appearance. Skipping from `h2` to `h4` is not automatically invalid, but it can indicate a confused hierarchy and may make navigation more difficult. The central question is whether the levels accurately communicate relationships among topics.

Paragraphs represent blocks of related prose:

```html

<p>Semantic elements express the role of content in a document.</p>

```

The `p` element is not a generic spacing mechanism. Empty paragraphs, repeated line breaks, and nonbreaking spaces should not be used to create layout. Layout belongs to CSS.

Inline semantic elements include `strong`, `em`, `mark`, `code`, `abbr`, and `time`. Their meanings differ:

```html

<p>Use <strong>HTTPS</strong> for secure communication.</p>

<p>Run the <code>npm test</code> command.</p>

<p>The workshop begins at <time datetime="2026-09-10T09:00">9:00 a.m.</time>.</p>

```

`strong` indicates strong importance, while `em` indicates emphasis. These meanings should not be reduced to “bold” and “italic,” since user agents and assistive technologies may present them differently.

4. Links and Navigation

The anchor element creates hyperlinks:

```html

<a href="/courses/web-development">Web Development Courses</a>

```

The `href` attribute identifies the destination. Link text should describe the destination or action. “Read more” repeated many times is weak because it provides little information when links are listed out of context. A stronger alternative is “Read the accessibility guidance.”

Links may be:

  • Absolute, such as `https://example.edu/about`.
  • Root-relative, such as `/about`.
  • Document-relative, such as `guides/semantic.html`.
  • Fragment links, such as `#assessment`, which target an element with a matching `id`.

A navigation region can be expressed as:

```html

<nav aria-label="Primary navigation">

<ul>

<li><a href="/">Home</a></li>

<li><a href="/programs">Programs</a></li>

<li><a href="/contact">Contact</a></li>

</ul>

</nav>

```

A link should be used when activating it leads to another resource or location. A button should be used when activating it performs an action on the current page, such as opening a dialog or submitting data. Substituting a clickable `div` for either element often creates keyboard, focus, and accessibility problems.

When opening a new browsing context with `target="_blank"`, developers should understand the security and usability implications. Modern browsers commonly provide protection against the historical `window.opener` problem, but explicit appropriate values such as `rel="noopener"` remain a useful defensive practice in relevant contexts. More importantly, users should be informed when a link opens a new tab or an external site where this information matters.

5. Lists and Grouped Information

Lists communicate membership and order. An unordered list uses `ul` and `li`:

```html

<ul>

<li>HTML</li>

<li>CSS</li>

<li>JavaScript</li>

</ul>

```

An ordered list uses `ol` when sequence matters:

```html

<ol>

<li>Plan the document structure.</li>

<li>Write semantic markup.</li>

<li>Test with keyboard and assistive technology.</li>

</ol>

```

Description lists use `dl`, `dt`, and `dd` for term–description or name–value relationships:

```html

<dl>

<dt>HTML</dt>

<dd>The language used to structure web content.</dd>

<dt>CSS</dt>

<dd>The language used to describe presentation.</dd>

</dl>

```

Lists should not be used merely to produce indentation or visual bullets. CSS can change markers and layout without changing the underlying meaning.

6. Semantic Elements and Landmarks

Semantic elements communicate the roles of major parts of a page. Common elements include:

  • `header`: introductory content for a page or section.
  • `nav`: a major set of navigation links.
  • `main`: the dominant content of the document.
  • `article`: a self-contained composition that could be distributed independently.
  • `section`: a thematic grouping, usually with a heading.
  • `aside`: content tangentially related to the surrounding content.
  • `footer`: closing or supplementary information for a page or section.

Example:

```html

<body>

<header>

<a href="/">Open Learning Portal</a>

<nav aria-label="Primary navigation">

<a href="/courses">Courses</a>

<a href="/library">Library</a>

</nav>

</header>

<main>

<article>

<header>

<h1>Understanding Semantic HTML</h1>

<p>Published <time datetime="2026-02-14">14 February 2026</time></p>

</header>

<section>

<h2>Why semantics matter</h2>

<p>Meaningful structure improves accessibility and maintenance.</p>

</section>

</article>

<aside>

<h2>Related resources</h2>

<ul><li><a href="/accessibility">Accessibility basics</a></li></ul>

</aside>

</main>

<footer><p>Copyright 2026 Open Learning Portal.</p></footer>

</body>

```

A landmark is a region that assists rapid navigation, especially for screen-reader users. `main`, `nav`, `header`, `footer`, and `aside` can create implicit landmark roles in appropriate contexts. Landmarks should be used meaningfully and not excessively. A page should generally have one primary `main` region. Multiple navigation regions may be valid, but each should be distinguishable, often through an accessible label.

The `section` element is not a replacement for every `div`. It represents a thematic section and normally has a heading. The `article` element is appropriate for content that can stand on its own, such as a news story, forum post, product review, or blog entry. A `div` has no semantic meaning; it is a generic container useful when no more specific element fits.

7. The Document Tree and the DOM

HTML is hierarchical. Consider:

```html

<main>

<h1>Courses</h1>

<ul>

<li><a href="/html">HTML</a></li>

<li><a href="/css">CSS</a></li>

</ul>

</main>

```

The `main` element is the parent of `h1` and `ul`. The `ul` is the parent of both `li` elements. Each `li` is a sibling of the other `li` and a child of `ul`. The `a` elements are descendants of `main` even though they are not its direct children.

The browser parses this source into a Document Object Model (DOM), a node-based representation that scripts and browser tools can inspect and modify. The tree model supports operations such as selecting all links within `main`, changing a heading's text, or adding a list item. JavaScript interacts with the DOM, but the DOM is not identical to the original source text: browsers may insert implied elements or correct certain parse errors during processing.

Understanding the tree helps developers reason about:

  • Inheritance and CSS selectors.
  • Event propagation in JavaScript.
  • Keyboard navigation and focus order.
  • Accessibility relationships.
  • Valid nesting and content models.

Elements must be nested according to their permitted content models. For example, an `li` belongs inside a list, and interactive controls should not be improperly nested inside other interactive controls. Valid nesting reduces unpredictable parsing and improves interoperability.

8. Separation of Concerns

A professional web interface normally separates three responsibilities:

1. HTML expresses structure and meaning.

2. CSS expresses visual presentation, layout, responsive behavior, and animation.

3. JavaScript expresses dynamic behavior and application logic.

For example, HTML should identify a button:

```html

<button type="button" id="show-details">Show details</button>

```

CSS may define its color and spacing:

```css

#show-details {

padding: 0.6rem 1rem;

background: navy;

color: white;

}

```

JavaScript may respond to activation:

```js

document.querySelector('#show-details').addEventListener('click', () => {

document.querySelector('#details').hidden = false;

});

```

This separation does not mean the technologies never interact. CSS can respond to HTML state, and JavaScript can update classes or attributes. The principle means that each technology should perform the task it is designed to perform. Inline style attributes, presentational tags, and JavaScript-generated content should be used only when justified.

The principle of progressive enhancement extends this idea. Begin with useful, accessible HTML; add CSS for improved presentation; then add JavaScript for enhanced interaction. If JavaScript fails, a user should retain as much meaningful functionality as possible. This strategy improves resilience across devices, network conditions, and assistive technologies.

9. Accessibility and Standards-Conscious Markup

Semantic HTML is a foundation for accessibility, but semantics alone do not guarantee an accessible experience. Developers should consider:

  • Meaningful heading order.
  • Descriptive link text.
  • Keyboard access and visible focus.
  • Text alternatives for informative images.
  • Sufficient color contrast, implemented through CSS.
  • Correct language declaration.
  • Labels for form controls.
  • Logical reading and focus order.
  • Appropriate use of ARIA only when native HTML is insufficient.

The `alt` attribute illustrates context-sensitive decisions. An informative image needs a concise text alternative:

```html

<img src="campus.jpg" alt="Students working in the university library">

```

A decorative image should generally have empty alternative text:

```html

<img src="ornament.svg" alt="">

```

Omitting `alt` is not equivalent to declaring an image decorative; assistive technology may announce the filename or image role. Conversely, repeating nearby visible text creates unnecessary verbosity.

Native elements should be preferred over manually recreating their behavior with ARIA and JavaScript. A native `button` already supports keyboard interaction, focus semantics, and a recognized role. A `div role="button"` requires developers to reproduce these behaviors correctly and is therefore more error-prone.

HTML validation tools, browser developer tools, keyboard-only testing, and screen-reader testing can reveal structural problems. Automated tools are valuable but incomplete: human evaluation is still necessary for meaning, clarity, and task completion.

10. Worked Example: Structuring a Course Page

Suppose a university is creating a page for a course titled “Web Programming Foundations.” A weak design uses nested `div` elements, headings selected for font size, and links labeled “Click here.” A semantic redesign begins by identifying the page's information architecture:

  • Site identity and primary navigation.
  • One main course document.
  • Course overview, learning outcomes, and assessment sections.
  • A related-resources aside.
  • Footer information.

A suitable structure is:

```html

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width, initial-scale=1">

<title>Web Programming Foundations | University Courses</title>

<link rel="stylesheet" href="course.css">

</head>

<body>

<header class="site-header">

<a href="/" class="site-name">University Courses</a>

<nav aria-label="Course site navigation">

<ul>

<li><a href="/courses">All courses</a></li>

<li><a href="/support">Student support</a></li>

</ul>

</nav>

</header>

<main>

<article>

<header>

<p>Full Stack Development</p>

<h1>Web Programming Foundations</h1>

<p>Semester: <time datetime="2026-09">Autumn 2026</time></p>

</header>

<section aria-labelledby="overview-heading">

<h2 id="overview-heading">Course overview</h2>

<p>This course introduces HTML, CSS, JavaScript, and the principles of accessible interface construction.</p>

</section>

<section aria-labelledby="outcomes-heading">

<h2 id="outcomes-heading">Learning outcomes</h2>

<ul>

<li>Construct valid HTML documents.</li>

<li>Apply semantic structure and accessible naming.</li>

<li>Separate structure, presentation, and behavior.</li>

</ul>

</section>

<section aria-labelledby="assessment-heading">

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

<ol>

<li>Semantic page exercise</li>

<li>Responsive interface project</li>

<li>Final practical examination</li>

</ol>

</section>

</article>

<aside aria-labelledby="resources-heading">

<h2 id="resources-heading">Related resources</h2>

<ul>

<li><a href="/library/html">HTML reference materials</a></li>

<li><a href="/support/accessibility">Accessibility support</a></li>

</ul>

</aside>

</main>

<footer>

<p><a href="/privacy">Privacy notice</a></p>

</footer>

</body>

</html>

```

This example demonstrates a document-level title, a visible primary heading, meaningful sections, lists used according to purpose, descriptive links, landmarks, and a language declaration. CSS can change the appearance without changing the document's meaning. JavaScript can later add features such as filtering resources, but the basic course information remains understandable without it.

11. Case Study: An Accessible Student Services Portal

Background

This illustrative case concerns a fictional university student-services portal. The portal provides links to registration, financial aid, counseling, and library services.

Problem

The first version placed every item inside `div` elements. Navigation was implemented with clickable text, headings were chosen by font size, and several links used the text “More.” Screen-reader users found it difficult to identify the main content and distinguish the different navigation groups.

Analysis

The development team mapped the content before rewriting the code. They identified a site header, primary navigation, a main service directory, individual service summaries, and a related-help region. They also determined that each service card was self-contained content and that “More” needed to be replaced by descriptive link text.

Approach and Solution

The team used `header`, `nav`, `main`, `article`, `section`, `aside`, and `footer`. Each service article received a heading, and navigation regions received distinguishing labels. Actions that led to pages were changed into anchors; actions that opened an on-page dialog were changed into real buttons. CSS supplied the card layout and visual hierarchy. JavaScript enhanced search but did not hide the service directory when scripting was unavailable.

The team then tested the page with keyboard navigation, a screen reader, HTML validation, multiple viewport sizes, and representative users. They revised heading levels and improved link names based on the results.

Outcome

Users could jump directly to the main content, navigate by headings and landmarks, and understand link destinations without reading surrounding text. Developers also found the code easier to maintain because structure, presentation, and behavior were separated.

Learning Points

The case demonstrates that semantic structure is not decorative markup. It is an information architecture decision with direct consequences for accessibility, maintainability, testing, and user efficiency. Requirements should be translated into document meaning before visual styling begins.

12. Challenges, Limitations, and Contemporary Practice

Semantic HTML cannot solve every interface problem. Complex widgets, dynamic application state, internationalization, and unusual interaction models may require JavaScript and ARIA. Even then, excessive custom controls increase testing and maintenance costs. A semantic element also does not guarantee good content: a page can contain `main` and `nav` yet remain confusing or inaccessible.

Search engines may use structure as one signal among many, but semantic markup is not a substitute for useful content, reliable navigation, performance, or secure implementation. Likewise, accessibility conformance involves more than HTML; CSS, scripts, media, documents, and organizational processes all matter.

Contemporary development increasingly uses component frameworks and server-side rendering. These tools can generate excellent HTML, but abstraction can also obscure the final document tree. Developers should inspect rendered output rather than assuming that a component named `Card` or `Layout` produces appropriate semantics. Design systems should document semantic expectations, keyboard behavior, accessible names, and heading rules.

AI-assisted coding can generate HTML quickly, but generated markup must be reviewed. Common risks include unnecessary `div` elements, incorrect heading levels, missing alternative text, inappropriate ARIA, and links with vague names. Professional responsibility remains with the developer who deploys the system. Validation, accessibility testing, privacy protection, and human judgment cannot be delegated blindly to an automated generator.

Best Practices Checklist

  • Start with a valid document skeleton and declare the document language.
  • Use UTF-8 and a meaningful page title.
  • Create headings according to information hierarchy.
  • Use semantic landmarks and keep them purposeful.
  • Choose links for navigation and buttons for actions.
  • Use lists for genuinely grouped or ordered information.
  • Prefer native HTML controls to custom ARIA widgets.
  • Provide appropriate alternative text for images.
  • Keep HTML, CSS, and JavaScript responsibilities distinct.
  • Test the rendered document with validators, keyboards, varied screens, and assistive technologies.
  • Inspect framework-generated markup and review AI-generated code critically.

Chapter Summary

HTML is a structural and semantic language for describing web content. A complete document contains a doctype, root element, metadata in the head, and user-facing content in the body. Metadata such as character encoding, viewport information, and the document title supports interoperability and usability.

Headings and paragraphs organize written content; anchors create navigation; lists express grouped information; and semantic elements such as `main`, `nav`, `article`, `section`, `aside`, `header`, and `footer` identify meaningful page regions. The browser interprets markup as a document tree and exposes a related DOM for styling, scripting, and accessibility processing. Understanding parent, child, sibling, and descendant relationships is fundamental to front-end development.

Separation of concerns assigns structure to HTML, presentation to CSS, and behavior to JavaScript. Progressive enhancement builds on useful HTML rather than making core communication dependent on scripting. Semantic HTML improves accessibility and maintainability, but it must be combined with meaningful content, correct keyboard behavior, testing, and careful professional judgment. Standards, validation, and human evaluation remain essential even when frameworks or AI tools generate code.

Key Terms

HTML: The standard language for structuring web documents.

Element: A structural unit of HTML, such as a paragraph or heading.

Tag: The syntactic notation used to mark the beginning or end of an element.

Attribute: Additional information or configuration attached to an HTML element.

Metadata: Information about a document or resource rather than its primary visible content.

Doctype: A declaration that places the browser in the intended standards mode.

Semantic HTML: Markup that communicates the meaning or role of content.

Landmark: A major navigable region of a page, such as `main` or `nav`.

Heading hierarchy: The ordered organization of headings from `h1` through `h6`.

Hyperlink: A reference that allows a user to navigate to another resource or location.

Unordered list: A list in which item sequence is not intrinsically important.

Ordered list: A list in which sequence or ranking matters.

Description list: A set of terms and associated descriptions or values.

DOM: The browser's node-based Document Object Model representation of a document.

Document tree: The hierarchical relationship among HTML nodes.

Parent element: An element that directly contains another node.

Descendant: Any node contained within another node, directly or indirectly.

Separation of concerns: The practice of assigning structure, presentation, and behavior to appropriate technologies.

Progressive enhancement: Building a basic functional experience first and adding enhanced capabilities progressively.

ARIA: Accessible Rich Internet Applications, a set of attributes for communicating additional interface semantics when native HTML is insufficient.

Review Questions

Short Answer Questions

1. What are the principal roles of the HTML `head` and `body` elements?

2. Why is the `lang` attribute important?

3. Distinguish between a link and a button.

4. What is a landmark, and name two HTML elements that can create landmarks.

5. Explain the difference between a parent, child, and descendant in the document tree.

Long Answer Questions

1. Explain how metadata contributes to the usability and interoperability of an HTML document.

2. Compare `section`, `article`, `div`, and `aside`, including an appropriate use for each.

3. Discuss how semantic HTML supports accessibility and why semantic markup alone is not sufficient.

4. Explain separation of concerns and progressive enhancement using a practical web-page example.

5. Describe how a browser transforms HTML source into a document tree and why developers should understand the DOM.

Analytical / Application Questions

1. A page uses clickable `div` elements for navigation, headings selected only for visual size, and repeated links labeled “More.” Analyze the problems and propose a semantic redesign.

2. Design the semantic structure for a university news page containing site navigation, one news story, related stories, publication date, and a footer. Justify each major element.

3. An AI-generated page contains five nested `div` elements, no `lang` attribute, an omitted image alternative, and JavaScript-dependent navigation. Evaluate the risks and create a prioritized correction plan.

Multiple Choice Questions with Answers

Question 1

Which element identifies the primary content of an HTML document?

A. main

B. section

C. aside

D. header

Correct Answer: A. main

Question 2

What is the main purpose of the `<!doctype html>` declaration?

A. To load the CSS file

B. To identify the document's primary language

C. To request standards mode in the browser

D. To create the document's main heading

Correct Answer: C. To request standards mode in the browser

Question 3

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

A. footer

B. nav

C. article

D. details

Correct Answer: B. nav

Question 4

Which HTML element should normally be used for an action such as opening a dialog?

A. a

B. div

C. button

D. section

Correct Answer: C. button

Question 5

What does the `lang="en"` attribute communicate?

A. The page uses English as its primary language

B. The page is available only on English servers

C. The page must use an English stylesheet

D. The page contains only English hyperlinks

Correct Answer: A. The page uses English as its primary language

Question 6

Which list type is most appropriate when the order of steps matters?

A. ul

B. ol

C. dl

D. menu

Correct Answer: B. ol

Question 7

In the document tree, an `li` element inside a `ul` is the `ul` element's:

A. Sibling

B. Attribute

C. Parent

D. Child

Correct Answer: D. Child

Question 8

Which technology is primarily responsible for presentation and layout?

A. HTML

B. CSS

C. JavaScript

D. DOM

Correct Answer: B. CSS

Question 9

What is an appropriate alternative text for a decorative image?

A. The image filename

B. A detailed description unrelated to the page

C. An empty `alt` value

D. The word “decorative” in visible text

Correct Answer: C. An empty `alt` value

Question 10

Why should native HTML controls generally be preferred to custom ARIA widgets?

A. Native controls require no HTML

B. Native controls provide established semantics and interaction behavior

C. ARIA widgets cannot be styled

D. Native controls always eliminate the need for testing

Correct Answer: B. Native controls provide established semantics and interaction behavior

HTML, CSS, JavaScript Basics
Chapter 3 of 15