HTML Span

The HTML <span> element is a generic inline container used to group a small part of text or other inline content. It is commonly used with CSS to apply styles to specific words or phrases without affecting the surrounding content.

Unlike the <div> element, which is commonly used for larger sections and containers, <span> is designed for inline content.

What Is the HTML Span Element?

The <span> element is a generic inline container. By itself, it does not add any visual styling or special meaning to the content.

Basic Example

<p>Learn <span>HTML</span> with Web4Learners.</p>

The word HTML is wrapped inside a <span> element.

To make the span visually different, you can use CSS:

<style>
  .highlight {
    color: blue;
    font-weight: bold;
  }
</style>

<p>Learn <span class="highlight">HTML</span> with Web4Learners.</p>

Only the word HTML receives the styles.

HTML Span Syntax

The basic syntax is:

<span>Content</span>

For example:

<p>This is <span>important</span> information.</p>

The <span> element has both an opening tag and a closing tag.

You can also use attributes with <span>:

<span class="highlight">Important Text</span>

Here, the class attribute allows CSS to target the span.

Why Use HTML Span?

The main purpose of <span> is to target a specific portion of inline content.

For example, suppose you have a paragraph:

<p>HTML is easy to learn.</p>

If you want only the word easy to be blue, you can use <span>:

<p>HTML is <span class="blue-text">easy</span> to learn.</p>

Then use CSS:

.blue-text {
  color: blue;
}

This allows you to style part of the sentence without changing the rest of the paragraph.

Span with CSS

The <span> element is frequently used with CSS classes.

<style>
  .red {
    color: red;
  }

  .bold {
    font-weight: bold;
  }
</style>

<p>
  This is <span class="red">red text</span>
  and this is <span class="bold">bold text</span>.
</p>

Each span can have a different class and therefore a different style.

You can also apply multiple classes to the same span:

<p>
  <span class="red bold">Important Text</span>
</p>

The text will receive the styles from both classes.

Span Without CSS

A <span> does not automatically change the appearance of text.

For example: <p>Hello <span>World</span></p>

Normally, the browser displays this almost the same as:

<p>Hello World</p>

The span becomes useful when you need to target that particular content with CSS or JavaScript.

Span with JavaScript

The <span> element can also be used with JavaScript to change or manipulate specific content.

<p>Current status: <span id="status">Offline</span></p>

<script>
  document.getElementById("status").textContent = "Online";
</script>

When the JavaScript runs, the text inside the span changes from Offline to Online.

This technique is commonly used for:

  • Status messages
  • Counters
  • Notifications
  • Dynamic prices
  • Live data
  • Interactive interfaces

Span with IDs

You can assign an ID to a span when you need to identify a specific piece of content.

<p>
  Your score is <span id="score">100</span>.
</p>

JavaScript can then access the element:

const score = document.getElementById(“score”);

This is useful when the content needs to be updated dynamically.

Span with Classes

Classes are often preferred when the same styling needs to be applied to multiple spans.

<p>
  <span class="keyword">HTML</span> is a
  <span class="keyword">markup language</span>.
</p>

CSS:

.keyword {
  color: blue;
  font-weight: bold;
}

Both spans receive the same styling.

HTML Span vs Div

<span> and <div> are both generic HTML containers, but they are commonly used for different purposes.

Feature<span><div>
TypeInlineBlock-level
Common useSmall portions of contentLarger sections
New lineNormally noNormally yes
Common purposeStyling or targeting inline contentGrouping and page layout
ExampleHighlighting a wordCreating a content section

Example of <span>:

<p>This is <span class=”highlight”>important</span>.</p>

Example of <div>:

<div class="card">
  <h2>HTML</h2>
  <p>Learn HTML basics.</p>
</div>

The span is used inside the sentence, while the div groups a larger section of content.

Span vs Semantic HTML

It is important to remember that <span> does not describe what the content means.

For example:

<span>Important</span>

The browser does not know that the word is important just because it is inside a span.

If the content has semantic meaning, use an appropriate semantic element.

For example:

<strong>Important</strong>

The <strong> element indicates strong importance.

Similarly, use elements such as <em>, <mark>, <time>, or other appropriate HTML elements when they better describe the content.

Use <span> when you need a generic inline container and no more meaningful HTML element is appropriate.

Span and Text Styling

One of the most common uses of <span> is highlighting specific words.

<p>
  HTML is a <span class="highlight">markup language</span>
  used to create web pages.
</p>

CSS:

.highlight {
  background-color: yellow;
}

You can also style individual words with different properties:

.highlight {
  color: blue;
  font-size: 20px;
  font-weight: bold;
}

Nested Span Elements

Spans can be placed inside other inline elements when appropriate.

<p>
  Learn <span class="html">HTML</span> and
  <span class="css">CSS</span>.
</p>

However, avoid unnecessary nesting because excessive containers can make your HTML difficult to understand and maintain.

Common Mistakes with Span

One common mistake is using <span> when a semantic HTML element is more appropriate.

For example:

<span class="heading">HTML Tutorial</span>

If this is actually a heading, use:

<h1>HTML Tutorial</h1>

Another mistake is using <span> to create large page layouts. For larger sections, <div>, <section>, <article>, <header>, and other suitable elements are usually better choices.

Best Practices for HTML Span

Follow these practices when using <span>:

  • Use <span> for small inline portions of content.
  • Use classes for reusable styling.
  • Use IDs when targeting one specific element.
  • Use CSS to control presentation.
  • Use JavaScript when dynamic content needs to change.
  • Prefer semantic HTML when it provides a more meaningful description.
  • Avoid unnecessary spans.
  • Avoid using spans as replacements for headings, paragraphs, buttons, or links.

Complete Example

Here is a simple example combining HTML, CSS, and JavaScript:

<!DOCTYPE html>
<html>
<head>
  <title>HTML Span Example</title>

  <style>
    .highlight {
      color: blue;
      font-weight: bold;
    }
  </style>
</head>

<body>

  <h1>HTML Span</h1>

  <p>
    Learn
    <span class="highlight" id="topic">HTML</span>
    with Web4Learners.
  </p>

  <button onclick="changeTopic()">Change Text</button>

  <script>
    function changeTopic() {
      document.getElementById("topic").textContent = "Web Development";
    }
  </script>

</body>
</html>

In this example, the <span> is used to identify a specific piece of text. CSS styles the text, while JavaScript changes its content when the button is clicked.

2 thoughts on “HTML Span”

  1. Pingback: HTML Strong -
  2. Pingback: HTML Small Text -

Comments are closed.