HTML span Element

Subject: html

✨ HTML <span> Element

The <span> element is an inline container used to group text or inline elements within a document. It doesn’t start on a new line and doesn’t add visual change by default. Instead, it's used to apply CSS styles or JavaScript behaviors to inline portions of content.


❓ Purpose of <span>

  • 🎨 Apply styles or classes to small parts of text
  • 🧠 Target specific text or inline content with JavaScript
  • 🧩 Group inline content without disrupting document flow

🔧 Syntax


✅ Example: Styling Text with <span>

💡 In this example, the word “highlighted” is wrapped in a <span> tag and styled using a class.


🕒 When to Use <span>

  • Applying inline styles to part of a sentence
  • Wrapping specific text inside a paragraph without breaking layout
  • Adding classes, IDs, or attributes for styling or scripting
  • When no semantic tag suits the content

🔄 Difference Between <div> and <span>

Feature<div><span>
Display typeBlock-levelInline
Starts new line?✅ Yes❌ No
PurposeLayout/structureInline styling
Can contain block?✅ Yes❌ No
Use caseStructure/layoutInline formatting

🎨 Example: Multiple <span> Usage


🧠 Key Takeaways

  • <span> is a non-semantic inline container
  • Use it for styling or scripting parts of inline text
  • It does not affect layout or block structure
  • Ideal for small-scale formatting like highlighting, coloring, or targeting words
  • Prefer semantic tags when available — use <span> only when necessary