Sub topic 2
Choosing elements by role, then styling them
This chapter assumes you know basic HTML elements, how a document is structured, CSS selectors and properties, and how font-size, font-weight, and color work. It covers one idea: decide what a piece of text is, mark it up with the element for that role, and only then decide how it looks.
Structure and styling are separate jobs
HTML says what a piece of text is. CSS says how it looks. The two are independent, and mixing them up causes most text hierarchy bugs.
- Semantic structure is the meaning carried by the element you pick:
h2means "a section heading",pmeans "a paragraph",figcaptionmeans "a caption for this figure",labelmeans "the name of this form control". - Visual styling is the appearance you give an element: size, weight, spacing, and color.
Browsers, screen readers, search engines, and reader modes read the structure. They mostly ignore how big or bold the text looks. A sighted reader does the opposite and uses appearance to decide what matters.
A good page keeps both signals in agreement. The element says "heading", and the styling looks like a heading.
What assistive technology does with structure
Screen reader users often move through a page by jumping between headings, or by listing all the form fields. This only works when the markup carries the roles.
- A screen reader can list every
h1toh6on the page and let the user jump to one. - A screen reader announces the text of a
labelwhen focus lands on its control. - A reader mode or browser extension can pull out the main text because it is in
pelements inside amainelement.
Text that only looks like a heading gives none of these benefits.
The common mistake: styling a role into existence
Here is a typical example of markup that looks right and is wrong.
<div class="big-bold">Gear</div>
<div>Start with footwear, then add layers.</div>
<div class="small-grey">Leather boots after a week on the trail.</div>With the right CSS, this looks like a heading, a paragraph, and a caption. To a screen reader it is three anonymous blocks of text. There is no heading to jump to and no link between the caption and an image.
Now the same content with elements chosen by role.
<h2>Gear</h2>
<p>Start with footwear, then add layers.</p>
<figure>
<img src="boots.jpg" alt="A pair of leather hiking boots on a rock">
<figcaption>Leather boots after a week on the trail.</figcaption>
</figure>Nothing visual has been specified yet. The browser's default stylesheet already gives the h2 a larger, bolder look. That default is a starting point, and you will replace it with your own styles.
The reverse mistake: using a role for its look
The opposite error is also common. A developer wants bigger text, so they reach for a heading element.
<h3>Free shipping on orders over $50</h3>If this is a promotional line and not the title of a section, h3 is the wrong element. It adds a false entry to the document outline and may break the heading order. Use a p and style it.
<p class="promo">Free shipping on orders over $50</p>The same rule applies to blockquote used only to indent text, strong used only to make text heavy, and table used only for layout.
Picking the element: a decision guide
Ask "what is this text for?" and not "how should it look?". The table lists the roles this tutorial covers.
| Role of the text | Element | Notes |
|---|---|---|
| Title of the page | `h1` | Usually one per page, describing the page's main subject |
| Title of a section | `h2` to `h6` | Level reflects nesting, not size |
| Running prose | `p` | The default for body text |
| Caption for an image, chart, or code | `figcaption` inside `figure` | Tied to its content by structure |
| Name of a form control | `label` | Linked to the control with `for` and `id` |
| Short note or fine print | `small` | Side comments, legal text, not a general "make it smaller" tool |
| Important emphasis | `strong` | Seriousness or urgency |
| Stress emphasis | `em` | Changes the meaning of a sentence when spoken |
| Related list of items | `ul` or `ol` with `li` | One `li` per item |
If no element matches the role, use a p or a span with a class, and style it. Do not stretch a semantic element to fit.
`strong` and `em` versus `b` and `i`
strong and em carry meaning. b and i are for text that is offset from its surroundings without added importance, such as a product name or a foreign phrase. When you only want a visual change, use a class and the CSS properties font-weight or font-style.
<p>Do <em>not</em> leave food in your tent.</p>
<p><strong>Warning:</strong> water sources freeze above 3,000 m.</p>
<p class="term">Base weight</p>Style by role, not by appearance
Once the elements are right, write CSS selectors that target the roles. Selectors tied to elements stay in sync with the structure.
h1 { font-size: 2rem; }
h2 { font-size: 1.5rem; }
p { line-height: 1.5; }
figcaption { font-size: 0.875rem; color: #595959; }
label { font-weight: 600; }Compare this with class names that describe appearance.
.big-bold { font-size: 2rem; font-weight: 700; }
.small-grey { font-size: 0.875rem; color: #595959; }Names such as .big-bold break down when the design changes. If the heading style becomes italic and blue, the class is now misnamed. Role names such as .page-title or .caption survive redesigns.
Keep the tag and the level independent of the size
A frequent need is a heading that is the right level in the outline but should look smaller. Give it a class and change the style, not the element.
<h2 class="section-title">Food</h2>
<h3 class="section-title section-title--minor">Breakfast</h3>.section-title { font-size: 1.5rem; line-height: 1.2; }
.section-title--minor { font-size: 1.125rem; }The outline is still h2 followed by h3. Only the look changed.
The opposite case works the same way. If a visual designer wants an h2 to look like an h4, keep h2 in the markup and restyle it. Never choose h4 for its size.
A worked example
Start with the unstyled, semantic page. This is the page from the plan, with a form added.
<main>
<h1>Trail Guide</h1>
<p>A short introduction to planning a multi-day hike.</p>
<h2>Gear</h2>
<p>Start with footwear, then add layers.</p>
<figure>
<img src="boots.jpg" alt="A pair of leather hiking boots on a rock">
<figcaption>Leather boots after a week on the trail.</figcaption>
</figure>
<h2>Food</h2>
<h3>Breakfast</h3>
<p>Oats keep well and cook quickly.</p>
<h2>Get the checklist</h2>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">
</main>Before you add any CSS, open this in a browser. You should see that:
- The
h1is the largest text, followed by theh2and then theh3. - Paragraphs sit at the base size.
- The caption is plain text under the image, because the browser has no default smaller size for
figcaption. - The label and input appear next to each other.
The browser already conveys much of the hierarchy. Your CSS then refines it.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.25rem;
--text-xl: 1.5rem;
--text-2xl: 2rem;
--leading-body: 1.5;
--leading-heading: 1.2;
}
body {
font-size: var(--text-base);
line-height: var(--leading-body);
}
h1 { font-size: var(--text-2xl); line-height: var(--leading-heading); }
h2 { font-size: var(--text-xl); line-height: var(--leading-heading); }
h3 { font-size: var(--text-lg); line-height: var(--leading-heading); }
p { max-width: 65ch; }
figcaption {
font-size: var(--text-sm);
color: #595959;
}
label {
display: block;
font-size: var(--text-base);
font-weight: 600;
margin-bottom: 0.25rem;
}Notice that every rule targets an element by its role. No class is needed for headings, body text, captions, or labels. The type scale itself is covered in the next chapter, so treat the custom properties here as a given.
How the pieces connect
The flow below shows the order of decisions. The role comes first, the element follows, and the style comes last.
Edge cases and gotchas
Default styles are not a design
The browser's default stylesheet differs between browsers and is not tuned for your content. It sets the default heading sizes and margins, and it may add margins to p and figure. Treat these defaults as a reasonable fallback. Always set the properties you care about.
`font-size` on headings changes the outline? No.
Changing the size of an h2 has no effect on the document outline. The level in the markup is what counts. This is why you can restyle freely, and also why a large div is not a heading.
`small` is not a size control
The small element means side comments such as copyright lines. It happens to render smaller, but that is only the default. If you only want smaller text for a caption, use figcaption and a font size.
One `h1`
The HTML specification allows more than one h1, but most guidance, and most assistive technology behavior, works best when a page has a single h1 naming its main subject. Using one keeps the outline predictable.
Placeholder text is not a label
A placeholder attribute disappears when the user types, and it often has low contrast. It does not replace a label. Labels are covered in detail in a later chapter, but the rule starts here: the name of a control belongs in a label.
<!-- Weak: no label -->
<input type="email" placeholder="Email address">
<!-- Better -->
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email">Using `display` or `content` to fake roles
You can add role="heading" aria-level="2" to a div. This works for assistive technology, but it is a fallback for cases where you cannot change the element. A real h2 is shorter, works without extra attributes, and is understood everywhere.
A quick audit method
You can test whether your structure carries the roles by removing the CSS.
- Open the page and disable styles. In most browsers, you can do this from the developer tools or an extension.
- Check that headings are still visibly larger than body text and appear in order.
- Check that each caption sits next to its figure.
- Check that each label is next to its input.
- Re-enable styles and confirm the appearance matches the roles.
If the page is unreadable or the hierarchy is lost without CSS, some role is being carried only by styling.
Summary of rules
- Choose the element for the text's role first.
- Never choose an element for its default size or weight.
- Express visual differences with CSS, using classes when the look must differ from the element's usual style.
- Name classes after roles, not appearances.
- Keep the heading level for the outline, and the font size for the design.
Exercise
A designer wants the sentence "Sale ends Sunday" to look like a large, bold heading, but it is a promotional line and not the title of any section. A colleague proposes <h2>Sale ends Sunday</h2>. What do you write instead, and why?
Show answer
Use a paragraph and style it: <p class="promo">Sale ends Sunday</p> with CSS such as .promo { font-size: 1.5rem; font-weight: 700; }. The text does not name a section, so an h2 would add a false entry to the document outline and could break the heading order for screen reader users. Size and weight come from CSS, so the element can match the role while the look matches the design.
Exercise
Your page has an h2 for "Food" and an h3 for "Breakfast". The design shows "Breakfast" in the same size as body text, with bold weight. Which change keeps the structure correct?
Show answer
Keep the h3 and change its style, for example h3 { font-size: 1rem; font-weight: 700; } or a class on that h3. Replacing it with a p or a strong would remove "Breakfast" from the outline, and choosing a different heading level for its size would misstate the nesting. The level stays tied to the structure and the size is set in CSS.