Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › HTML Snippets › Reference › <span>

HTML Reference

<span> — The Inline Container

The inline counterpart to <div>. Styles a word or phrase without breaking the flow of text.

Last updated: October 2026

Definition: The <span> element is a generic inline container with no semantic meaning. It groups a portion of text for styling or JavaScript manipulation without introducing a line break.

🎬 Live Example

A span inside a paragraph highlights a word without breaking the line.

This is a regular paragraph. The word highlighted is wrapped in a <span> and turns yellow. Notice how the line does not break around it.

💡 The span is inline — the surrounding text flows naturally around it. A <div> here would push the text to a new line.

📝

Basic Syntax

<p>This is a <span class="highlight">highlighted</span> word.</p>

The <span> has no attributes of its own — only global attributes.

⚙️ Global Attributes

Like <div>, <span> has no specific attributes. It uses the standard global attributes:

Attribute Purpose
idUnique identifier for CSS or JavaScript.
classOne or more class names for styling.
styleInline CSS.
data-*Custom data attributes for JavaScript.
titleTooltip on hover.
langLanguage of the content inside the span.
aria-*ARIA attributes for accessibility.
1

Highlight a Word

The simplest use — style a specific word inside a paragraph.

<p>
    The <span class="text-blue-500">quick</span> brown fox
    jumps over the lazy dog.
</p>

<style>
    .text-blue-500 { color: #3b82f6; font-weight: 700; }
</style>
2

Price Highlighting

Common in e-commerce — style a price inline with the description.

<div class="product">
    <h3>Wireless Headphones</h3>
    <p>
        Premium sound with active noise cancellation.
        Now only <span class="price">$149</span> — down from $199!
    </p>
</div>
3

Language Tagging

Mark inline foreign words for screen readers and search engines.

<p>
    The Spanish word for "hello" is
    <span lang="es">hola</span>.
</p>

Screen readers will pronounce "hola" with Spanish phonetics.

4

JavaScript Targeting

Use a span as a hook to update text dynamically.

<p>
    Items in cart: <span id="cart-count">0</span>
</p>

<script>
    const count = document.getElementById("cart-count");
    let items = 0;

    function addToCart() {
        items++;
        count.textContent = items;
    }
</script>

🎯 span vs div

The single most important distinction to know:

Feature <span> <div>
DisplayInlineBlock
Line breakNoYes
Can hold block elementsNoYes
Typical useStyling a word or phraseLayout containers
Common exampleHighlight a wordWrap a card

💡 Rule of thumb: If it sits inside a paragraph, use <span>. If it wraps a block of content, use <div>.

🎯 Semantic Inline Alternatives

Before using <span>, check if a semantic inline element fits:

If the content is... Use this instead
Important / strong emphasis<strong>
Emphasized (italic meaning)<em>
Highlighted / marked<mark>
Code snippet inline<code>
Keyboard input<kbd>
Abbreviation<abbr>
Time / date<time>
Nothing semantic fits<span>

⚠️ Common Mistakes

♿ Accessibility Notes

❓ Frequently Asked Questions

What is the HTML <span> tag?

The <span> tag is a generic inline container with no semantic meaning. It groups inline content for styling or JavaScript manipulation without breaking the flow of text.

What is the difference between <span> and <div>?

<span> is inline (flows with text, no line break). <div> is block-level (forces a line break). Use <span> for styling a word or phrase inside a paragraph. Use <div> for structural containers.

When should I use a <span>?

Use a <span> when you need to style or target part of a text line — a specific word, a number, or a phrase — without affecting the layout. It is ideal for inline styling and JavaScript hooks.

Can <span> contain block elements?

No. <span> is inline and cannot legally contain block-level elements like <div>, <p>, or <h1>. Doing so produces invalid HTML. Use <div> if you need to wrap block content.

Is <span> bad for SEO?

No. <span> is neutral for SEO. It does not convey meaning to search engines, but it does not hurt them either. Use it for styling or scripting. For meaning, use semantic elements like <strong>, <em>, or <mark>.

🎯 What's Next?

← All HTML Reference