HTML <span> Tag

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Text & Semantics

What You’ll Learn

The <span> tag is a versatile inline container for styling portions of text. This guide covers syntax, global attributes, common use cases, span vs div, and best practices for beginners.

01

Inline Styling

Highlight words or phrases inside a sentence without breaking the line.

02

CSS Hooks

Use class and id to target text with stylesheets.

03

Core Syntax

Write valid opening and closing span tags.

04

Grouping

Wrap inline elements together for consistent styling.

05

span vs div

Know when to use inline vs block containers.

06

Best Practices

Prefer CSS classes over excessive inline styles.

What Is the <span> Tag?

The <span> tag is a generic inline container used to group text or inline elements and apply styles to a specific portion of content. Unlike block-level elements, span does not add line breaks — it stays on the same line as surrounding text.

💡
No default semantic meaning

span is a styling hook, not a semantic tag. Use strong, em, or code when the content itself carries meaning; reach for span when you need a neutral wrapper for CSS or JavaScript.

In the vast landscape of HTML elements, span is one of the most frequently used tags for precise inline formatting in tutorials, blogs, and production websites.

📝 Syntax

Wrap the desired content within opening and closing span tags:

syntax.html
<span>Your text here</span>

Styled Inline Example

styled-span.html
<p>
  This is
  <span class="highlight" style="color: #FF5733;">
    styled text
  </span>
  inside a paragraph.
</p>

Syntax Rules

  • span is phrasing content — use inside block elements like p, li, or td.
  • Self-closing syntax (<span />) is not valid in HTML.
  • Do not use span as a substitute for block layout — use div for sections.
  • Prefer class over repeated inline style attributes for maintainability.

⚡ Quick Reference

TopicCode SnippetNotes
Basic span<span>...</span>Inline wrapper
With class<span class="highlight">CSS hook
Inline stylestyle="color: red;"Quick tweaks
Bold wordstyle="font-weight: bold;"Emphasis
vs divspan = inlinediv = block
Browser supportUniversalAll browsers

⚖️ <span> vs <div>

Feature<span><div>
DisplayInline (stays on same line)Block (starts new line)
Typical useStyle a word or phraseLayout sections and containers
Semantic meaningNone by defaultNone by default
Can wrap block elements?No (invalid nesting)Yes
Example<span class="price">$9</span><div class="card">...</div>

🧰 Attributes

The <span> tag has no tag-specific attributes. Combine it with global attributes like class and style to apply CSS rules.

span.html
<span class="highlight" style="color: #FF5733;">Styled text here</span>
class / id Global

Primary hooks for CSS selectors and JavaScript targeting.

class="highlight"
style Global

Inline color, font-weight, or spacing for quick one-off styling.

style="color: #FF5733;"
title Optional

Advisory tooltip text on hover for extra context.

title="Sale price"
lang Optional

Declare language when the wrapped text differs from the page.

lang="fr"
data-* Scripting

Store custom values for JavaScript without extra markup.

data-role="price"
hidden Optional

Hide the span from display when toggling visibility in scripts.

hidden

Examples Gallery

Inline styling, bold emphasis, and grouped inline elements with copy-ready code and live previews.

👀 Live Preview

Highlight a single word inside a sentence:

This is an example of styled text using the span tag.

📚 Common Use Cases

Use <span> for inline styling, highlighting words, grouping inline elements, and attaching CSS classes to portions of text.

Inline Styling with class and style

The primary purpose of span is to apply inline styling to specific portions of text. This is useful for highlighting or emphasizing certain words or phrases:

styled-span.html
<p>
  This is an example of
  <span class="highlight" style="color: #FF5733;">Styled Text Here</span>
  using the span tag.
</p>
Try It Yourself

Inline Bold Emphasis

Use span with inline font-weight to emphasize a single word inside a line of text:

inline-styling.html
<p>
  In this example, we use the
  <span style="font-weight: bold;">bold</span>
  style for emphasis.
</p>
Try It Yourself

Grouping Inline Elements

The span tag is commonly used to group and style inline elements together. This helps apply consistent styling to code snippets or grouped text:

grouping-inline-elements.html
<p>
  Here is a JavaScript variable:
  <span class="code">const variable = 'Hello, World!';</span>
</p>
Try It Yourself

♿ Accessibility

  • Use semantic tags first — Prefer strong, em, or code when meaning matters; use span only as a styling wrapper.
  • Do not convey meaning by color alone — Pair color highlights with weight, underline, or accessible text.
  • Keep contrast readable — Styled span text must meet color contrast guidelines.
  • Avoid empty spans — Do not leave <span></span> with no content or purpose.

🧠 How <span> Works

1

Author wraps inline content

A word, phrase, or inline element is placed inside span.

Markup
2

CSS targets the wrapper

Styles apply via class, id, or inline style.

Style
3

Browser renders inline

No line break is added; styled text flows with surrounding content.

Display
=

Precise inline formatting

Only the wrapped portion changes appearance while the sentence stays on one line.

Browser Support

The <span> tag is supported in all major browsers, including Internet Explorer.

Baseline · HTML4 / HTML5

Inline styling everywhere

All browsers render <span> as a generic inline container.

100% Core tag support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported · EOL
Full support
Opera Fully supported
Full support
<span> tag 100% supported

Bottom line: Use <span> confidently for inline styling in any browser.

Conclusion

Mastering the <span> tag empowers you to apply precise styling to specific portions of your HTML content. Whether emphasizing text or grouping inline elements, span is a valuable tool in your web development toolkit.

💡 Best Practices

✅ Do

  • Use span when styling a small portion of text within a larger block
  • Combine with CSS classes for consistent, reusable styling
  • Prefer semantic tags when content carries meaning
  • Test styled text for contrast and readability

❌ Don’t

  • Use span for block-level layout (use div instead)
  • Rely on excessive inline style attributes everywhere
  • Substitute span for accessible semantic elements
  • Nest block elements inside span

Key Takeaways

Knowledge Unlocked

Six truths every developer should know about <span>

Bookmark these before you style inline text on your pages.

6
Core concepts
🔑 02

class / style

CSS hooks.

Attributes
📝 03

Highlight

Style words.

Use case
📦 04

Grouping

Inline sets.

Pattern
⚖️ 05

vs div

Inline vs block.

Comparison
🌐 06

100% Support

All browsers.

Compatibility

❓ Frequently Asked Questions

It is a generic inline container for grouping text and applying CSS to a specific portion without adding line breaks.
span is inline and stays on the same line. div is block-level and starts a new line for layout sections.
No. Use global attributes like class, id, style, and data-*.
No. Use semantic tags like strong, em, or code when meaning matters. Use span as a neutral styling hook.
Yes. Full support in every major browser including Internet Explorer.

Style inline text

Practice <span> for highlights, emphasis, and grouped inline content in the Try It editor.

Try styled span example →

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.

6 people found this page helpful