HTML Span Tag

What is the 'Span' Tag in HTML?

In HTML, the span tag is a generic inline element. With the help of a span tag, we can wrap a particular text and provide the style with the help of a CSS property. We can write the syntax of the span tag with the help of opening and closing the angular bracket.

Syntax:

We can write the syntax of the span tag with the help of opening and closing the angular bracket.

We can also call the span element the generic tag because the developer cannot know anything about it by seeing its name. Also, the user does not know the meaning of the "span". The opposite word of the generic tag is the semantic tag. The name of the semantic tag describes all the tags about the names.

The span tags are also known as the inline tags. The inline tag means the element stays in the current line and does not create any new line. However, the semantic tag creates the element in a new line.

Note: HTML <span> is much similar as <div> tag, but <div> is used for block-level elements and <span> tag is used for inline elements.

What does 'span' do in HTML?

The <span> tag does not affect the rendering of the page. But the <span> tag is so much more powerful. We can also assign the global attribute to any other inline elements. The attributes used inside the inline tag are ID and class. Let's understand with the example.

Example 1:

Output:

HTML span tag

We can apply the CSS property to the HTML element inside the HTML <span> tag. We can provide the CSS property with the help of the internal CSS property. We can also implement other CSS properties to the span tag. In HTML, there is a lang attribute that is used to notify the browser that there is a temporary language change.

Example 2:

Output:

HTML span tag

With the help of a span tag, we can target a particular section using JavaScript. Let us take the below example. In the example below, a span tag hides some text. When we click on that span tag, then it reveals the text.

Example 3:

Output:

HTML span tag

After clicking the span tag, the hidden text will be displayed as per the below output.

HTML span tag

With the help of a span tag, we can also make the text bold and italic. But for the coding practice, we have to use a strong tag. Because for bold and italic, now we have semantic tags like <strong> and <em>. Let us see the below example.

Example 4:

Output:

HTML span tag

Before seeing the difference between the span tag and the div tag, let us see the difference in the view of the coding output.

Example 5:

Output:

HTML span tag

Difference Between Div Tag and Span Tag

When we create the webpage, <div> and <span> tags are commonly used. We can also call <div> tag a block-level element, whereas <span> tag is an inline element.

<div> Tag<span> Tag
<div> tag is also known as a block-level element.<span> tag is known as an inline-level element.
It takes the whole width of the web page.It only takes the required width of the webpage.
Some examples of the <div> tag are Headings, Paragraph, forms, etc.Some examples of <span> tags are attribute, image, etc.
We can make use of <div> tag for the creation of the web layout of the web page.We can make use of <span> tag for the container of some text.
In <div> tag, there is no need for attributes; we can only use the common CSS property.In <span> tag, there is no need for attributes; we can only use the common CSS property.

With the help of the <span> tag, We can not create a line break similar to the <div> tag. With the help of <span> tag, we can separate the things from the other elements and place them in a single line.


Next TopicHTML strike Tag