1. html
  2. /tags
  3. /span

<span>

Overview

The <span> element acts as an inline container, allowing content to be grouped for specific styling with CSS or for interaction via JavaScript. Its generic nature means it doesn't convey any particular meaning, making it a suitable choice when no other semantic element fits.

Examples and Usage

In web design, specific content often requires distinct styles or interactions. The <span> element provides a simple, inline solution for these requirements.

Consider a brief instructional text where we want to emphasize individual colors:

<p>
  Combining the colors <span class="text-blue">blue</span> and <span class="text-red">red</span> results in <span class="text-purple">purple</span>.
</p>
.text-blue {
    color: blue;
}

.text-red {
    color: red;
}

.text-purple {
    color: purple;
}

In this basic demonstration, the <span> element selectively applies styles to portions of the text. Such granularity can be further extended with JavaScript by targeting id attributes, enabling dynamic behaviors.

Attribute Breakdown

The <span> element doesn't have any specific attributes; it inherits global attributes common to all HTML elements.

Accessibility Aspects

The <span> element doesn't inherently possess specific accessibility roles or properties. According to ARIA specifications, the <span> element defaults to a role labeled generic. However, it's discouraged to utilize this role directly. The element can also accept global aria-* attributes, offering some flexibility in conveying additional context or behaviors.

Associated Elements

  • <div>

Additional Notes

  • The <span> element becomes particularly useful when the content requires distinct styling or attributes but should remain within the flow of the surrounding text.

  • While <span> is excellent for styling and user interactions, for semantic emphasis, elements like <em>, <strong>, and others are preferable.

  • Compared to the block-level <div> element, <span> is inline and is more appropriate for minor modifications within text blocks or lines.

  • Given its neutral semantics, <span> often finds utility in scenarios where content grouping is essential, whether for shared attributes like lang or for specific styling via class or id attributes.

Browser Compatibility

For a detailed breakdown of specific browser nuances and older version support refer to the first link in the Useful Resources below.

BrowserChromeEdgeSafariFirefoxOperaInternet Explorer
SupportYesYes*YesYesYes*Yes*

Useful Resources

Can I use HTML element: span

The HTML Living Standard Specification: span