Sub topic 2
Use Semantic HTML, Not Styling, to Express Structure
This chapter assumes you can read and write basic HTML, and that you know the difference between an element and its CSS. You do not need JavaScript knowledge. By the end you will be able to tell structure that a machine can read from structure that only looks right in a browser. You will also be able to replace the second kind with the first, and to catch the most common fakes with a short script.
Structure that looks right versus structure that is right
Semantic HTML means choosing elements for what the content is, not for how it looks. An <h2> says "this is a second-level heading". A <div> with large bold text says nothing, even though a reader sees a heading.
A person looking at a rendered page uses visual cues: size, weight, spacing, indentation, and bullet glyphs. A program that reads the markup has none of those cues unless it also renders the page and interprets the pixels. Most do not. The element names and attributes are the only structure they receive.
Three terms are used in this chapter:
- Semantics: the meaning an element carries, such as "list item" or "table header cell".
- Presentation: how the element is drawn, controlled by CSS.
- DOM: the tree of elements the browser builds from the HTML. Many tools read this tree, or the raw HTML it came from.
The rule for this chapter: express structure with elements, and express appearance with CSS. Never the reverse.
Why this matters for AI answer systems
Retrieval-based answer systems fetch a page, extract its content, split it into passages, and pick passages to quote or summarize. The previous chapter covered that pipeline. The relevant point here is the extraction step. Extractors commonly drop scripts and style rules, then convert what remains into text or Markdown. In that conversion, <h2> can become a heading marker, <li> becomes a list item, and <th> becomes a table header. A styled <div> becomes plain text, the same as any other paragraph.
This is a working model, not a specification. Systems differ, and no vendor publishes a complete description of how it parses pages. Semantic markup is still the safer choice for three reasons:
- It costs nothing. The correct element is as easy to write as the wrong one.
- It also serves screen readers, browser reader modes, and search crawlers, which all depend on the same signals.
- It survives any extraction method. Markup that is correct for a browser's accessibility tree is correct for most text extractors.
Mapping visual tricks to the right element
Most structural mistakes come from a small set of shortcuts. The table below pairs each one with the element that should replace it.
| What you want | Common shortcut | Semantic element |
|---|---|---|
| A section title | `<div class="title">` or `<p><b>…</b></p>` | `<h1>` to `<h6>` |
| A bulleted list | Lines starting with `•` separated by `<br>` | `<ul>` with `<li>` |
| A numbered procedure | Paragraphs that begin with "1.", "2." | `<ol>` with `<li>` |
| A comparison grid | Nested `<div>` boxes laid out by CSS grid | `<table>` with `<th>` and `<td>` |
| Emphasis that matters | `<span class="bold">` | `<strong>` or `<em>` |
| A term and its definition | Two adjacent styled paragraphs | `<dl>`, `<dt>`, `<dd>` |
| Navigation links | A `<div>` of links | `<nav>` containing a `<ul>` |
Later chapters cover headings, lists, and tables in detail. Here you only need to see that each shortcut has a direct replacement.
A worked before-and-after
The first version below looks fine in a browser once CSS is applied. Every piece of structure lives in class names and line breaks.
<div class="page">
<div class="big-title">Router Setup Guide</div>
<div class="sub-title">Before you start</div>
<div>You will need:<br>
• A router<br>
• An Ethernet cable<br>
• Your internet account details
</div>
<div class="sub-title">Connect the router</div>
<div>1. Plug the cable into the WAN port.<br>
2. Power on the router.<br>
3. Wait for the status light to turn green.
</div>
</div>An extractor that ignores class names reads this as a handful of undifferentiated text blocks. The bullets are literal • characters, and the numbers are literal text.
The second version carries the same content with real structure.
<article>
<h1>Router Setup Guide</h1>
<section>
<h2>What do you need before you start?</h2>
<ul>
<li>A router</li>
<li>An Ethernet cable</li>
<li>Your internet account details</li>
</ul>
</section>
<section>
<h2>How do you connect the router?</h2>
<ol>
<li>Plug the cable into the WAN port.</li>
<li>Power on the router.</li>
<li>Wait for the status light to turn green.</li>
</ol>
</section>
</article>The visual design can stay the same. Restore it with CSS selectors on h1, h2, ul, and ol, or keep your existing classes on the new elements. A class on a semantic element is fine. The problem is a class standing in for the element.
Landmark elements and page-level structure
Headings and lists describe structure inside the content. Landmark elements describe the page itself.
<main>wraps the primary content of the page. Use one per page.<article>wraps a self-contained piece, such as a guide or a post.<section>groups related content under a heading.<nav>wraps navigation links.<header>and<footer>hold introductory and closing material for the page or for a section.<aside>holds content related to, but separate from, the main flow.
The main benefit is separating the content you want quoted from the boilerplate around it. Menus, cookie notices, and "related posts" blocks add noise. When they sit inside <nav>, <aside>, or <footer>, an extractor has a clear signal to exclude them. When they are bare <div> elements interleaved with your text, they can end up inside a passage.
<body>
<header>
<nav aria-label="Primary">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/guides">Guides</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h1>Router Setup Guide</h1>
<p>This guide covers first-time setup.</p>
</article>
<aside>
<h2>Related guides</h2>
<ul>
<li><a href="/guides/wifi">Improve Wi-Fi coverage</a></li>
</ul>
</aside>
</main>
<footer>
<p>Contact support through the help page.</p>
</footer>
</body>Gotchas and edge cases
Text drawn by CSS is not in the markup
CSS can insert text with ::before and ::after, and it can number list items with counters. That text appears on screen but does not exist in the HTML. If a step number or a label such as "Note:" is generated this way, an extractor will not see it. Put meaningful text in the markup itself. For numbered steps, use <ol> and let the element supply the order.
Visual order can differ from document order
CSS properties such as order, flex-direction: row-reverse, and absolute positioning can move content on screen without changing its position in the source. Extractors read source order. If your page shows the answer above the question visually but the source lists them the other way round, the extracted text follows the source. Keep source order and reading order the same.
Content that appears only after JavaScript runs
If a headline, list, or table is built by JavaScript in the browser, the raw HTML may not contain it. Some fetchers run scripts and some do not. For content you want parsed, include it in the HTML the server sends. You can check this by fetching the page with a plain HTTP client and reading the response.
Choosing an element for its default look
Authors sometimes pick <h4> because it renders at the size they like, or <blockquote> to indent a paragraph. Pick the element by meaning and fix the size with CSS. Heading levels in particular carry outline information, which the next chapter covers.
ARIA is a repair, not a substitute
ARIA attributes such as role="heading" with aria-level="2" can give a generic element a role for assistive technology. They are meant for cases where native elements cannot be used. A native <h2> already includes the role and level, and plain-text extractors that ignore ARIA will still read it correctly. Prefer the native element, and do not add ARIA that repeats what the element already says.
Layout tables
A <table> used only to position items in columns tells a parser that the content is tabular data. Cells then get read as rows and columns of related values, which they are not. Use CSS grid or flexbox for layout, and keep <table> for data.
Meaningful emphasis versus decoration
<strong> marks importance and <em> marks stress. <b> and <i> only change the look. Using the first pair for text that matters costs nothing, and using them as styling hooks across a whole page dilutes the signal.
Detecting fake structure with a script
A quick check can flag the most common shortcuts: non-heading elements with heading-like class names, and text made bold through inline styles. The script below uses only the Python standard library.
import re
from html.parser import HTMLParser
HEADING_CLASS = re.compile(r"(title|heading|headline|subhead)", re.I)
BOLD_STYLE = re.compile(r"font-weight\s*:\s*(bold|[6-9]00)", re.I)
class FakeStructure(HTMLParser):
def __init__(self):
super().__init__()
self.findings = []
def handle_starttag(self, tag, attrs):
if tag not in ("div", "p", "span"):
return
attributes = dict(attrs)
css_class = attributes.get("class") or ""
style = attributes.get("style") or ""
if attributes.get("role") == "heading":
return
if HEADING_CLASS.search(css_class):
self.findings.append(f'<{tag} class="{css_class}"> looks like a heading')
elif BOLD_STYLE.search(style):
self.findings.append(
f'<{tag} style="{style}"> is bold text, possibly a fake heading'
)
sample = (
'<div class="page-title">Router Guide</div>'
'<p style="font-weight: bold">Reset steps</p>'
"<h2>Done</h2>"
'<div class="heading-box" role="heading" aria-level="2">Accepted</div>'
)
checker = FakeStructure()
checker.feed(sample)
for finding in checker.findings:
print(finding)Running it prints two findings:
<div class="page-title"> looks like a heading
<p style="font-weight: bold"> is bold text, possibly a fake headingThe real <h2> is ignored because the script only inspects generic elements. The <div> with role="heading" is skipped because it exposes a heading role, although a native <h2> would still be the better choice. The script reports candidates. It cannot tell a decorative title in a banner from a section title, so a human reviews each result. Chapter 12 builds a fuller audit.
Exercises
Exercise
A page shows a list of five requirements. Each line begins with a "✓" character typed into a <p> element, and CSS adds spacing between them. Which element should replace this markup, and what does that change for a text extractor?
Show answer
Use a <ul> with one <li> per requirement, and remove the typed "✓" characters (add a check mark with CSS list-style if you want one). An extractor then sees a list of five items instead of five unrelated paragraphs, and the check mark glyph is no longer mixed into the text.
Exercise
Your step-by-step guide uses <ol>, but the step numbers come from CSS counters on <div> elements, and the visual order is changed with flex-direction: row-reverse. Name two problems this causes for a parser that reads the source.
Show answer
First, the numbers come from CSS-generated content, so they are not present in the markup, and an extractor cannot see them. Second, the parser reads source order, which differs from the order readers see, so the steps may be extracted in the wrong sequence. The fix is a real <ol> with <li> items written in the order they should be read, with any visual changes done in a way that keeps source order and reading order the same.
Exercise
Why is <div role="heading" aria-level="2"> a weaker choice than <h2>, even though both expose a heading to assistive technology?
Show answer
The native <h2> carries the role and level by itself, works in every parser including those that ignore ARIA, and needs no extra attributes. The ARIA version depends on the consumer understanding the attributes. ARIA is intended for cases where a native element cannot be used.