HTML <span> Tag

Beginner
5 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <span> element is a generic inline container for phrasing content. Alone it does not change meaning. Use it to group text for styling ( class / id), scripting, or shared attributes such as lang.

Prefer a semantic tag when it fits — <strong>, <em>, <mark>, <code>, and so on. For a generic block wrapper, use <div> instead.

Baseline · Widely available.

Not deprecated. Stable across browsers since about July 2015 (and much earlier in practice).

Last resort for meaning.

Use <span> only when no other phrasing element is appropriate. Do not wrap every word “just in case.”

HTML Syntax

syntax.html
<span> phrasing content</span>

Both start and end tags are required. Content must be phrasing content (text and inline elements).

Attributes

<span> has no element-specific attributes. These globals are the ones you will use most:

AttributeDescriptionTry it
classCSS / script hook for one or more shared stylesTry it
idUnique target for CSS, scripts, or in-page linksGlobal attr
langLanguage of this phrase when it differs from the pageTry it
titleAdvisory tooltip (use sparingly; prefer visible text)Global attr

Global Attributes

The <span> tag also supports the Global Attributes in HTML.

Live Preview

Change the class on a real <span> inside a sentence.

A bare <span> looks like normal text until CSS targets its class.

Preview

Add the basil and blend into a paste.

class="demo-span-ingredient"

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Style Hook with class

Group phrases that share the same CSS without changing meaning.

HTML
<p>
  Add the <span class="ingredient">basil</span>,
  <span class="ingredient">pine nuts</span>, and
  <span class="ingredient">garlic</span>
  to a blender.
</p>

<!-- CSS: .ingredient { color: red; } -->
Try It Yourself

How It Works

  1. Wrap each phrase that needs the same look in <span>.
  2. Give them one class (here, ingredient).
  3. Style the class in CSS — HTML stays free of presentation meaning.
  4. Leave other words unwrapped so the markup stays lean.

Example 2 — Shared lang on a Phrase

Mark a short foreign phrase when the rest of the sentence is another language.

HTML
<p>
  The motto
  <span lang="la">Carpe diem</span>
  means “seize the day.”
</p>
Try It Yourself

How It Works

  1. Keep the sentence in the page language (here, English).
  2. Wrap the Latin words in <span lang="la">.
  3. Assistive tech and spellcheckers can treat that phrase correctly.
  4. No visual change required — lang is about language, not color.

Example 3 — Prefer Semantic Tags

Do not fake importance or emphasis with a styled span.

HTML
<!-- Prefer -->
<p><strong>Warning:</strong> save your work.</p>
<p>Press <em>Submit</em> when ready.</p>

<!-- Avoid for meaning -->
<p><span class="bold">Warning:</span> save your work.</p>
Try It Yourself

How It Works

  1. Importance → strong (not a bold-looking span).
  2. Stress emphasis → em.
  3. Keep span for hooks that do not carry that meaning.
  4. Screen readers convey semantic tags better than class names alone.

Example 4 — span vs div

Inline phrasing vs block flow containers.

HTML
<p>
  Inline:
  <span class="note">stays in the sentence</span>.
</p>

<div class="box">
  Block: starts its own box on a new line.
</div>
Try It Yourself

How It Works

  1. Use span inside sentences and other phrasing content.
  2. Use div to group blocks (headings, paragraphs, layouts).
  3. Default CSS: span is inline; div is block.
  4. Neither adds meaning — both are generic hooks.

Edge Cases & Pitfalls

Most mistakes overuse span or replace semantic tags with classes.

semantics

Not a bold / italic substitute

Importance and emphasis belong on strong / em (or b / i when those meanings fit), not on a styled span alone.

bloat

Do not wrap everything

Extra spans make HTML harder to read and maintain. Wrap only what needs a hook.

vs div

Wrong display model

Putting block layouts inside span fights the inline box model. Use div (or a semantic sectioning element) for blocks.

a11y

Class names are not roles

class="button" on a span is not a real control. Use <button> or a proper link.

valid

Phrasing content only

Do not nest paragraphs or other flow-only blocks inside span.

Browser Support

The <span> element is core HTML phrasing content in every major browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

<span>

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera all support span as a generic inline container with no default meaning.

99% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Supported
<span> Universally supported

Bottom line: Safe to use everywhere as an inline hook. Prefer semantic tags whenever the text has meaning beyond styling.

Key Takeaways

  • Role: <span> is a meaning-free inline wrapper for phrasing content.
  • Hooks: use class, id, or lang when you need a target.
  • vs div: span = inline; div = block/flow container.
  • Prefer semantic tags for importance, emphasis, code, and marks.

One line: wrap inline text with <span> only when you need a hook and no better element fits.

Default CSS Settings

Browsers treat span as an inline box with no special look:

default.css
span {
  display: inline;
}

Colors, weights, and backgrounds come entirely from your CSS.

Frequently Asked Questions

The span element is a generic inline container for phrasing content. It has no meaning by itself. Use it to group text for CSS, scripting, or shared attributes such as lang when no semantic tag fits better.
No. span only supports the Global Attributes in HTML (such as class, id, and lang).
span is inline (phrasing content) and sits in the text flow. div is a block-level flow container that typically starts on a new line.
No. Prefer strong for importance and em for stress emphasis. Use span when you only need a styling or scripting hook without that meaning.
By default span has no corresponding ARIA role (generic). It does not announce a landmark. Prefer semantic elements when assistive tech should recognize structure or meaning.
When you need to wrap a phrase for class/id/lang (or similar) and no better element exists — for example coloring ingredient names, or marking a short foreign phrase with lang.

Next: strike

See the obsolete strike element and modern del / s alternatives.

strike tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful