
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How do we style HTML elements using the span tag ?
We use <span> tag to color a part of text or a part of the document. This tag is mainly used to group similar content together for easy styling. We use inline style for <span> tag, as it applies a style to inline elements.
If we want to make some text or any other content different from the rest, we wrap it in a <span> tag, and add class attribute to identify, then add attribute value for styling.
Syntax
Following is the syntax for the <span> tag.
<span class=" ">Text…</span>
Example 1
Given below is an example to style HTML element using span tag.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p> <span style="color:green;font-weight:bolder"> Major Sandeep Unnikrishnan</span>was an officer in the Indian Army serving in the elite 51 Special Action Group of the National Security Guards. He was martyred in action during the November 2008 Mumbai attacks. <span style="background-color: yellow;"> He was consequently awarded the Ashoka Chakra, India's highest peacetime gallantry award, on 26 January 2009 by the president Pratibha Patil.</span> </p> </body> </html>
Following is the output for the above example program.
Example 2
Given below is an example to style HTML element using span tag.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p> <span style="text-transform: uppercase; font-weight:bolder"> Major Sandeep Unnikrishnan</span> was an officer in the Indian Army serving in the elite 51 Special Action Group of the National Security Guards. He was martyred in action during the November 2008 Mumbai attacks. <span style="font-family:cursive; color: green"> He was consequently awarded the Ashoka Chakra, India's highest peacetime gallantry award, on 26 January 2009 by the president Pratibha Patil.</span> </p> </body> </html>
Following is the output for the above example program.
Example 3
You can try to run the following code to learn the usage of a <span> tag. Here, we will be using the <span> tag inside the <p> tag.
<!DOCTYPE html> <html> <head> <title>HTML span Tag</title> </head> <body> <p>Java is a Programming language widely followed. <span style = "color:#FF0000;">Developed by James Gosling. </span>This is demo text.</p> <p> <span style = "color:#8866ff;"> Current Version: 1.8 </span> </p> </body> </html>
- Related Articles
- How do we style HTML elements using the division tag ?
- How do we do variable formatting using the tag in HTML?
- How do we do computer output formatting using the tag in HTML?
- Why do we use head tag in HTML Page?
- How to create a style tag using JavaScript?
- How do we set the alignment according to surrounding elements in HTML?
- How to use the tag to define style information for an HTML page?
- How do we embed custom data attributes on all HTML elements?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How do we send an email using HTML forms?
- How do I center tag using CSS?
- HTML DOM Span object
- How to Select Multiple Files using HTML Input Tag?
- How do we upload external file on a website using HTML forms?
- HTML DOM Column span Property
