HTML » Tags » span

Version: 4.0

Compability: Explorer 4, 5  Netscape 4, 6

Syntax:
<span> ... </span>

The <span> tag is used to designate an inline portion of an HTML document as a span element and to apply any of the core attributes of this tag against the contents of that element. The behavior of this tag is inline because the effects occur in the normal flow of the text and images (without the tag inserting any additional linefeeds or carriage returns). span tags, which can be nested, allow a significant degree of control and manipulation of a localized part of your web page.

For example, you could use the class or style core attributes to apply the effects of Cascading Style Sheets or you could use the lang attribute to display a foreign language text.
 
In contrast, you should use the div tag when you want to apply attributes to a block of code. Note that the behavior of the div tag is not inline, but rather is block-line, since this tag causes a line break to occur both before and after the div element.
 
With the advent of Cascading Style Sheets, both the div and span tags have gained importance and usefulness.
 
The closing tag is mandatory.
 

Examples

Code:
<span class="CODE">
This line is blue.
</span>
Output:
This line is blue.
Explanation:

As an example, all of the code samples on the DevGuru site are in blue colored text. This is done by enclosing the code sample inside a pair of span tag elements and designating a class attribute of "CODE". In turn, the "CODE" class has been assigned a text color of blue in the style sheets file (a .css file) that is located in the Include/ directory on the DevGuru web site. Thus, only one file is needed to define the appearance of the entire site.

Language(s): HTML
Code:
<span style="color: blue;">
This line is blue.
</span>
Output:
This line is blue.
Explanation:

However, if you do not wish to create a .css file, you can still get the exact same results by using the style attribute for the span tag and the style sheet color property.

Language(s): HTML

See Also: