Reference · Beginner
The span element
Use span as an inline styling or scripting hook only when the text has no more specific semantic element.
- Published
- Last checked
Definition
span is a generic inline container. It carries no meaning on its own. Use it
when a short run of phrasing content needs a class, language declaration, data
attribute, or scripting target and no semantic text element describes the job.
Minimal syntax
<p>Your order is <span class="order-status">ready</span>.</p>
The sentence remains a paragraph. The span simply gives the status word a
stable hook. CSS may change its presentation, and a script may replace its text,
but neither behavior turns the container into a control.
Choose meaning first
Use em for stress emphasis, strong for importance, code for code, time
for a machine-readable date or duration, and a for navigation. A class on a
span can make text look important without exposing that importance to reader
modes or assistive technology. Styling is not a substitute for meaning.
Browser and accessibility behavior
Browsers keep a plain span in the surrounding text flow. It is not normally a
separate accessibility object and is not keyboard focusable. Adding a click
handler does not give it button behavior, a name, Enter and Space activation, or
disabled state. If the user can operate it, begin with a native interactive
element instead.
Common failures
- A clickable
spanworks only with a pointer. - A
spanreplacesstrongoremsolely to control typography. - Every word receives its own container, making source and debugging noisy.
- A status is updated visually but is never announced where an announcement is needed.
Verification checklist
Remove CSS and confirm the sentence still communicates the intended meaning. Inspect the accessibility tree: the generic wrapper should not introduce an unexpected role or name. If the node has interaction, replace it with a button or link and test keyboard behavior. If a script updates it, decide deliberately whether the change should be silent, associated with a control, or exposed as a status message.