Sub topic 2
Choosing elements by role, then styling them
This chapter assumes you know basic HTML elements, CSS selectors and properties, and how font-size, font-weight, and color work. It covers one working rule: pick the HTML element for what the text is, then use CSS to decide how it looks.
Structure and styling are separate jobs
HTML says what a piece of text is. CSS says how it looks. Browsers, screen readers, search engines, and reader modes all read the HTML. Only the visual rendering reads the CSS.
If you pick elements by their default look, the page looks right but means little to anything that can't see it. A <div> styled to look like a heading is still a generic box. A screen reader user can't jump to it, and a search engine can't tell it is a title.
The reverse also fails. If you pick an <h3> because you like its default size, the outline of the page breaks, even though the page looks fine.
The working rule has two steps:
- Choose the element that matches the text's role.
- Style that element with CSS until it looks the way you want.
Common roles and their elements
| Role of the text | Element | Why |
|---|---|---|
| Page title | `<h1>` | Names the whole page |
| Section title | `<h2>`, `<h3>` | Builds the outline |
| Body text | `<p>` | A paragraph of running text |
| Caption for an image | `<figcaption>` inside `<figure>` | Ties the text to its image |
| Name of a form field | `<label>` | Linked to the control |
| Strong importance | `<strong>` | Marks meaning, not just bold |
| Stress or emphasis | `<em>` | Marks meaning, not just italic |
Same text, two markups
Here is a heading built from the wrong element, and the same heading built from the right one.
<!-- Looks like a heading, means nothing -->
<div class="big-bold">Gear</div>
<!-- Same words, with a role -->
<h2>Gear</h2>The first needs CSS to look like a heading and still has no role. The second has a role from the start, and CSS can adjust its look.
Styling the element you chose
Once the roles are right, style by element, not by invented class names that repeat the element's job.
h2 {
font-size: 1.5rem;
font-weight: 700;
line-height: 1.2;
}
p {
font-size: 1rem;
line-height: 1.5;
}
figcaption {
font-size: 0.875rem;
color: #595959;
}
label {
display: block;
font-weight: 600;
}These four rules give headings, body text, captions, and labels distinct looks. Each rule targets a role, so a new <h2> on any page picks up the style with no extra markup.
Visual size does not decide the level
Sometimes the design wants a small heading, or a large piece of body text. Keep the element that matches the role and change the size in CSS.
<h2 class="compact">Packing list</h2>
<p class="lead">Pack light, and pack for the weather you expect.</p>.compact {
font-size: 1.125rem;
}
.lead {
font-size: 1.25rem;
}The first is still a section heading, only a smaller one. The second is still a paragraph, only a larger one. Neither choice changes the outline.
Using `<strong>` and `<em>` with care
<strong> and <em> carry meaning. A screen reader may change its voice for them. If you only want bold or italic for decoration, such as a product name or a foreign word, style it with CSS and a neutral element like <span>, or use <b> or <i> where that fits.
<p>Check the forecast <em>before</em> you leave. <strong>Never</strong> hike alone in a storm.</p>Quick check of your choices
For each piece of text, ask these questions in order:
- What role does this text play on the page?
- Which element names that role?
- Does the default look differ from what I want? If so, change it in CSS and keep the element.
Exercise
A designer wants a small, light-gray line of text under each photo, and you consider using <p class="small gray">. Which element is better, and where does the small gray look go?
Show answer
Use <figcaption> inside a <figure> that also holds the <img>, because the text's role is a caption tied to the image. The small size and gray color go in CSS on the figcaption rule, for example font-size: 0.875rem; color: #595959;.
Exercise
Your page has an <h2> section titled "Food". You want a sub-label inside it that looks small, so you use an <h5>. What is wrong with that, and what should you do?
Show answer
The level should reflect the outline, not the size. A subsection of an <h2> should be an <h3>, with no skipped levels. If the <h3> default is too large, reduce its font-size in CSS.