HTML - <span> Tag



Introduction to <span> Tag

The HTML <span> tag is an inline container for phrasing the text. It can be easily style using CSS and manipulated with JavaScript through class or id attributes.

The <span> tag shpu;d be used only when no other semantic element is suitable. Unlike the <div> tag, which is a block-level element, the <span> tag is an inline-level element. It is used for arguing and applying styles to inline elements.

Syntax

Following is the syntax of <span> tag −

<span>.....</span>

Attributes

The HTML <span> tag supports both Global and Event attributes.

Example: Creating <span> Elements

In the following program, we creating multiple spans using the HTML <span> tag to mark up parts of the text or content in an HTML document. This code creates a webpage with a heading and a paragraph, styling part of the text using the <span> tag.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML span tag</title>
</head>

<body>
   <h3>Tutorialspoint</h3>
   <!-- Creating span Element -->
   <p>Easy Simply Learning on <span>Tutorialspoint.</span></p>
</body>

</html>

Example: Styling <span> Element

Here is another example of the HTML <span> tag. We create multiple input fields for user input and use the <span> tag to mark up parts of the text or content for each input field. This HTML code creates a webpage with a heading and a paragraph, styling the text within the <span> tag in green and 24px font size.

<!DOCTYPE html>
<html lang="en">

<head>
    <title>HTML span tag</title>
    <style>
        span {
            color: green;
            font-size: 24px;
        }
    </style>
</head>

<body>
    <h3>Tutorialspoint</h3>
    <!-- Creating span Element -->
    <p>Easy Simply Learning on <span>Tutorialspoint.</span></p>
</body>

</html>

Example: Parsing <span> Elements

Let's consider the following example, where we run a script to parse content within the <span> tag. This HTML code creates a webpage with a paragraph and a button. Clicking the button displays an alert with the span's text.

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML span Tag</title>
   <style>
      #txt {
         font-size: 30px;
      }
   </style>
</head>

<body>
   <!-- Create span Element-->
   <p>Simply Easy Learning<span id='txt'>Tutorialspoint</span></p>
   <button onclick="Show()">Show</button>
   <script>
      function Show() {
         let demo = document.getElementById('txt');
         alert(demo.innerHTML);
      }
   </script>
</body>

</html>

Supported Browsers

Tag Chrome Edge Firefox Safari Opera
span Yes Yes Yes Yes Yes
html_tags_reference.htm
Advertisements