The HTML span element is a generic inline container for inline elements and content. It used to group elements for styling purposes (by using the class or id attributes), A better way to use it when no other any other semantic element is available. span is very similar to the div tag, but div is a block-level tag and span is an inline tag. Span tag is a paired tag means it has both open(<) and closing (>) tag and it is mandatory to close the tag.
- The span tag is used to grouping of inline-elements.
- The span tag does not make any visual change by itself.
- span is very similar to the div tag, but div is a block-level tag and span is an inline tag.
Tag is written like:
<span class="">Some Text.............</span>
In below example suppose we want to write three times GeeksforGeeks in three lines with bold, italic, underline, in green color with font-family = courier new so we need to use many HTML tags such as <b>, <i>, <u>, <font> for every time in every line and we want to make changes need to modify every tag.
But by using <span> tag we can reduce code and HTML Attributes see below example will display same output as above example with using <span> tag by applying css in span tag.
As we know span is an inline tag it takes space as much as required and leaves space for other element let see it in below example all four span element will display in the same line because each tag takes only necessary space and rest of space free for other elements.
A span tag can be used to set color/background color a part of a text: In below example inside paragraph applying three times span tag with different style.
Output:(Before Clicking on Button)
Output:(After Clicking on Button)
- Internet Explorer
- Mozilla Firefox
Difference Between Div tag and span tag
The div and span tag are two common tags when creating pages using HTML and perform different functionality on them while div tag is a block level element and span is inline element The div tag creates a line break and by default creates a division between the text that comes after the tag as begun and until the tag ends with </div>. div tag creates separate boxes or containers for all elements inside this tag like text, images, paragraphs.
|Properties||Div Tag||Span Tag|
|Space/Width||Contain Whole Width Available||Takes only required Width|
|Examples||Headings, Paragraph, form||Attribute, image|
|Uses||Web-layout||container for some text|
|Attributes||Not required, with common css, class||Not required, with common css, class|
The span tag does not create a line break similar to a div tag, but rather allows the user to separate things from other elements around them on a page within the same line. avoiding of the line break, results only that selected text to change, keeping all the other elements around them same.
Below example will display the difference between span and div tag while div tag contains whole width and span tag contain only required width and rest parts are free for another element.
- HTML | DOM Span Object
- HTML | DOM ColumnGroup span Property
- HTML | DOM Column span Property
- CSS | column-span Property
- HTML Course | Structure of an HTML Document
- HTML Course | Basics of HTML
- HTML | DOM HTML Object
- HTML <ins> Tag
- HTML | <dd> Tag
- Div Tag | HTML
- HTML | <s> Tag
- HTML 5 | <wbr> Tag
- HTML | <u> Tag
- HTML 5 | <bdi> Tag
- HTML | <dfn> Tag
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.