HTML <span> Tag β Inline Container
π Introduction
The <span> tag is an inline container used to group small pieces of text or elements.
It does not add any visual style by default.
Instead, it is used when you want to:
- Style part of a text
- Target specific text with CSS
- Manipulate content using JavaScript
π§± Syntax
<span>text here</span>
π‘ Example
<!DOCTYPE html>
<html>
<head>
<title>Span Example</title>
</head>
<body>
<p>I am learning <span>HTML</span> step by step.</p>
</body>
</html>
π₯οΈ Output Explanation
- The word βHTMLβ is wrapped in
<span> - By default, it looks normal
- But it can be styled separately
π― Why <span> is Important
1. π¨ Styling Part of Text
<p>I love <span style="color:red;">HTML</span> and CSS.</p>
β Only βHTMLβ becomes red
2. βοΈ JavaScript Targeting
<span id="word">Hello</span>
<script>
document.getElementById("word").innerText = "Hi";
</script>
β Used to change small parts of content dynamically
3. π§© Fine Control in Layout
Used when you donβt want a full block element like <div>.
βοΈ <span> vs <div>
| Tag | Type | Use |
|---|---|---|
<span> |
Inline | Small text parts |
<div> |
Block | Large sections/layout |
Example:
<span>Inline text</span>
<div>Block section</div>
π« Common Mistakes
- Using
<span>for layout β - Expecting it to add spacing β
- Replacing
<div>with<span>β
π¨ Styling <span>
Very common use:
<p>This is <span style="font-weight:bold;">important</span> text.</p>
π§ͺ Mini Exercise
Create a page with:
- A sentence about your skills
- Use
<span>to:- Color one skill
- Make another skill bold
- Highlight a keyword