Skip to main content

HTML Tags

HTML <span> Tag

Written by Updated HTML <span> Tag exercises

<span> is a hook. It wraps a piece of text so you can point CSS or JavaScript at that piece and nothing else. On its own it changes nothing, no line break, no spacing, no meaning. That is the point of it.

Try it

Two <span>s inside one paragraph. Press Run, then try changing the class on the first one to price and see that only that word changes.

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>span example</title>
  <style>
    .price { font-weight: bold; }
    .free  { color: #15803d; background: #dcfce7; padding: 0 4px; border-radius: 4px; }
  </style>
</head>
<body>
<p>
  Your order total is <span class="price">₹1,299</span>.
  Delivery is <span class="free">free</span> on orders over ₹999.
</p>
</body>
</html>

What it is actually for

Most of the time you style whole elements: a paragraph, a heading, a list item. Sometimes you need to style part of one: a price inside a sentence, a keyword in a search result, a character counter inside a label. HTML has no element for "this bit of text", so <span> is the neutral wrapper you use when no better element exists.

The important word is neutral. <span> tells the browser nothing about what the text means. If the text is important, that is <strong>. If it is stressed, <em>. A date is <time>, a snippet of code is <code>, a highlighted search match is <mark>. Reach for <span> only after you have checked that list.

span vs div

They do the same job at different sizes. <div> is a block: it starts on its own line and stretches across its container. <span> is inline: it sits inside a line of text and is only as wide as its content. Put a <div> in the middle of a sentence and you break the sentence in two; put a <span> there and nothing moves.

<span><div>
Displayinlineblock
Starts a new lineNoYes
Can containText and other inline elementsAnything, including block elements
width / heightIgnoredRespected
Typical usePart of a sentenceA section of the page

A realistic example: a character counter

Forms often show "120 / 280" under a text box. The number changes as you type, so it needs its own element that JavaScript can update without touching the rest of the label. That is a textbook <span>.

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Character counter</title>
  <style>
    label, textarea { display: block; width: 100%; max-width: 360px; }
    .hint { color: #64748b; font-size: 14px; }
    .warn { color: #b91c1c; font-weight: bold; }
  </style>
</head>
<body>
<label for="bio">Short bio</label>
<textarea id="bio" maxlength="120" rows="3"></textarea>
<p class="hint"><span id="count">0</span> / 120 characters</p>

<script>
  const box = document.getElementById("bio");
  const count = document.getElementById("count");
  box.addEventListener("input", () => {
    count.textContent = box.value.length;
    count.classList.toggle("warn", box.value.length > 100);
  });
</script>
</body>
</html>

Only the number is wrapped. The " / 120 characters" text stays plain, so the script never has to rebuild the sentence: it just replaces the one value.

Common mistakes

1. Putting block elements inside a span

<span><p>Hello</p></span> is invalid HTML. A <span> may only contain inline content: text, <a>, <strong>, another <span>. Browsers will render it anyway, but they do so by silently closing the span early, which is why your styles then apply to the wrong thing. If you need to wrap a paragraph, use a <div>.

2. Setting width or height on it

span { width: 200px } does nothing. Inline elements take their size from their text. If you really need a fixed-size inline box (a badge, a coloured dot) add display: inline-block, and then width, height, and vertical padding start to work.

3. Using it where a real element exists

<span class="bold">Warning</span> looks fine and reads as nothing to a screen reader or a search engine. <strong>Warning</strong> carries the meaning and is bold by default. The same goes for <span class="date"> instead of <time>, and <span onclick="…"> instead of a <button>: the span version cannot be reached with the keyboard at all.

4. Nesting spans for every style

<span class="red"><span class="big"><span class="bold">Sale</span></span></span> is three elements doing one element's job. A single <span> can carry several classes: <span class="red big bold">Sale</span>. Deep nesting makes the HTML harder to read and the CSS harder to override, and it gains nothing.

Attributes

<span> has no attributes of its own. It accepts all the global attributes; the ones you will actually use are class and id for CSS and JavaScript, title for a tooltip, and lang when a phrase is in a different language from the rest of the page (<span lang="fr">joie de vivre</span>), which helps screen readers pronounce it.

Browser support

Every browser, back to the earliest versions. There is nothing to check.

Related chapters

HTML <span> Tag exercises

Style part of a sentence

Write this paragraph. Put only the word red in a span with the class hot, and make that class red with a style block: A red door.

Result

Check your answer to see it here.