Structure

HTML Span

Inline Content with Span

HTML span is an inline container for styling or scripting small content chunks.

What is the HTML Span Element?

The <span> element is an inline container used to apply styles or scripting to small sections of content without disrupting the document flow. Unlike block elements, spans do not start on a new line and are typically used to target parts of text within a paragraph or other inline elements.

Basic Syntax of the Span Element

The basic syntax of the <span> element is quite simple, as shown above. It can contain text or other inline elements and is often paired with CSS classes or styles to modify its appearance.

Styling with Span

The <span> element is frequently used for styling purposes. By applying CSS styles to a <span>, you can change the appearance of specific parts of your text.

In this example, the <span> element is used to create a section of text that is red and bold within a paragraph. This allows the developer to emphasize parts of the text without affecting the rest of the document structure.

Using Span for JavaScript Manipulation

The <span> element is also useful for JavaScript manipulation. You can use it to isolate parts of a text or element that you want to interact with through scripting.

In this example, a <span> element is made into a clickable text. When the user clicks on it, a JavaScript function changes its color to blue. This demonstrates how spans can be used to create interactive areas within your content.

Span vs. Div: Key Differences

While both <span> and <div> elements are used for styling and scripting, they serve different roles in HTML:

  • <span>: Inline element, does not start a new line, used for small sections of content.
  • <div>: Block element, starts a new line, used for larger sections or blocks of content.

Understanding these differences helps in choosing the right element for your needs, ensuring that your document structure remains clear and semantically correct.

Previous
Div
Next
Lists