HTML - <span> Tag



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

It should be used only when no other semantic element is suitable. The <span> tag is different from the <div> tag, the <div> tag is a block-level element, whereas the <span> is an inline-level element. It is used to grouping and applying styles to inline elements.

Syntax

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

Attribute

HTML span tag supports Global and Event attributes of HTML.

Examples of HTML span Tag

Bellow examples will illustrate the usage of span tag. Where, when and how to use span tag to mark up a part of the text.

Creating span Element

In the following program, we are creating multiple spans using the HTML <span> tag to mark up the part text or content in an HTML.

<!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>

Styling the span Element

Following is another example of the HTML <span> tag. Here, we are creating multiple input fields to take user input and using the <span> tag to markup a part of text or content for each input field.

<!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>

Parsing span Elements

Let's look at the following example, where we are going to run the script for parsing content used in the <span> tag.

<!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