Generic block-level container.
Learn more → Semantic Elements Always prefer semantic elements when available.
View all elements →
Specifications
Section titled “Specifications”
<span> - The Generic Inline Container
The <span> Element
Section titled “The <span> Element”The span element is a generic inline container for phrasing content. It has no semantic meaning on its own and is used for styling purposes or as a hook for JavaScript.
Quick Example
Section titled “Quick Example” Result
Syntax
Section titled “Syntax”<span>inline content</span>The <span> element is purely a container with no inherent meaning.
When to Use <span>
Section titled “When to Use <span>”Use <span> when you need to:
- Apply CSS styling to part of inline content
- Target specific text with JavaScript
- Group inline elements for manipulation
- No appropriate semantic element exists
Common Use Cases
Section titled “Common Use Cases”Styling Specific Text
Section titled “Styling Specific Text” Result
Highlighting Keywords
Section titled “Highlighting Keywords” Result
Multi-Color Text
Section titled “Multi-Color Text” Result
Icon with Text
Section titled “Icon with Text” Result
JavaScript Hooks
Section titled “JavaScript Hooks” Result
Data Attributes
Section titled “Data Attributes” Result
When NOT to Use <span>
Section titled “When NOT to Use <span>”Use Semantic Alternatives
Section titled “Use Semantic Alternatives”<!-- Don't use span for emphasis --><p>This is <span class="important">very important</span>.</p>
<!-- Don't use span for code --><p>Use the <span class="code">console.log()</span> function.</p><!-- Use semantic elements --><p>This is <strong>very important</strong>.</p>
<!-- Use code element --><p>Use the <code>console.log()</code> function.</p>Semantic Elements to Consider First
Section titled “Semantic Elements to Consider First”Instead of <span> | Use |
|---|---|
| Emphasized text | <em> or <strong> |
| Code snippets | <code> |
| Keyboard input | <kbd> |
| Sample output | <samp> |
| Variables | <var> |
| Abbreviations | <abbr> |
| Citations | <cite> |
| Definitions | <dfn> |
| Time/dates | <time> |
| Quotations | <q> |
Real-World Examples
Section titled “Real-World Examples”Badge Components
Section titled “Badge Components” Result
Tooltip Trigger
Section titled “Tooltip Trigger” Result
Dynamic Content
Section titled “Dynamic Content” Result
Formatted Numbers
Section titled “Formatted Numbers” Result
Styling Patterns
Section titled “Styling Patterns”Inline Notification
Section titled “Inline Notification” Result
Tag Labels
Section titled “Tag Labels” Result
Accessibility
Section titled “Accessibility”No Semantic Meaning
Section titled “No Semantic Meaning”Remember that <span> has no semantic meaning for screen readers. Add ARIA attributes when necessary:
<!-- Add role and label when needed --><span role="status" aria-live="polite" id="status"> Loading...</span>Prefer Semantic Elements
Section titled “Prefer Semantic Elements”<p>Error: <span style="color: red;">Invalid input</span></p><p><strong>Error:</strong> Invalid input</p><!-- or --><p role="alert">Error: Invalid input</p>Don’t Use for Structure
Section titled “Don’t Use for Structure”<span>Heading</span><span>This is a paragraph</span><h2>Heading</h2><p>This is a paragraph</p><span> vs <div>
Section titled “<span> vs <div>”<!-- Inline container --><p>This is <span class="highlight">inline</span> content.</p>Doesn’t break line flow.
<!-- Block container --><div class="section"> <p>This is block content.</p></div>Creates a block-level container.
Browser Support
Section titled “Browser Support”| Browser | Version | Notes |
|---|---|---|
| Chrome | 1+ | Full support |
| Firefox | 1+ | Full support |
| Safari | 1+ | Full support |
| Edge | 12+ | Full support |
| IE | 3+ | Full support |
The <span> element has been supported since the earliest browsers.