HTML <span> Tag
<span> tag is used for identifying inline elements in the document. Description of the tag, examples.
The HTML <span> tag is used to define a small piece of content or text within a larger document that needs to be styled differently than the surrounding text. It is an inline element that can be used to apply styles, such as color or font, to a specific section of text.
The <span> tag does not have any semantic meaning on its own, but it can be used in conjunction with other tags, such as <div> or <p>, to apply additional styling or functionality to specific parts of the content. It is a useful tool for web designers and developers who need to make small adjustments to text without affecting the overall structure of the page.
For example, you can use the <span> tag to highlight a specific word within a paragraph or to apply a different font size to a single character. The <span> tag can also be used with CSS to create hover effects, animations, and other dynamic features on a web page.
The <span> tag is similar to the <div> tag, but there are some differences. While the HTML <span> tag is used to define a small piece of content or text within a larger document that needs to be styled differently than the surrounding text, the <div> tag is used to define a larger section or block of content that can contain other HTML elements. The <div> tag is a container element that is often used to group related content together and apply styling to the entire block. It is a block-level element, which means it takes up the full width of its parent container and forces a line break before and after the element.
Syntax
The <span> tag comes in pairs. The content is written between the opening (<span>) and closing (</span>) tags.
In the example below, we apply a style directly inside the tag.
Example of the HTML <span> tag:
An example of an HTML <span> tag
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<p>My cat has <span style="color:#8ebf42;">green</span> eyes.</p>
</body>
</html>Let's see another example where we add a class attribute to the tag and apply styles to its content separately.
Example of the HTML <span> tag with the class attribute:
An example of HTML <span> tag with the class attribute
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.letter {
color: red;
font-size: 300%; /* Font size in percents */
position: relative; /* Relative positioning */
top: 7px; /* Move from above */
}
</style>
</head>
<body>
<p>
<span class="letter">О</span>
She brought in disgusting, disturbing yellow flowers in her hands.
And these flowers stood out on her black coat.
</p>
<p>Michael Bulgakov</p>
</body>
</html>Attributes
The <span> tag supports the Global attributes and the Event Attributes.
Global Attributes refer to attributes that are used on any HTML element. These attributes are common for all elements in HTML.
An event occurs when a browser reacts to a particular user’s action. The user generates an event when clicking on a mouse, playing video, uploading a document or an image, or performing any other action on the website.
Practice
What is the correct usage of the HTML span tag?