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 the same output as above example with using <span> tag by applying CSS in a 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.
Before Clicking on Button
After Clicking on Button
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.
Supported Browser: Supported browsers are listed below
- Google Chrome
- Internet Explorer
- Difference between <div> and <span> Tag in HTML
- HTML | DOM Span Object
- HTML | <col> span Attribute
- HTML | span Attribute
- HTML | DOM ColumnGroup span Property
- HTML | <colgroup> span Attribute
- HTML | DOM Column span Property
- How to change a span to look like a pre with CSS?
- CSS | column-span Property
- JQuery | Get the text of a span element
- JQuery | Change the text of a span element
- How to define the number of columns a cell should span using HTML5 ?
- How to show a span element for each rows clicked in AngularJS?
- Inline HTML Helper - HTML Helpers in ASP.NET MVC
- HTML Course | Structure of an HTML Document
- HTML | <html> xmlns Attribute
- HTML Course | Basics of HTML
- HTML | DOM HTML Object
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : Akanksha_Rai