A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/html/html-span-tag/ below:

HTML <span> Tag - GeeksforGeeks

HTML <span> Tag

Last Updated : 02 May, 2025

The HTML <span> tag is an inline container that is used to group and apply styles or scripts to specific parts of text or elements within a document.

Syntax

<span class="">Some Text</span> 

Note: HTML <span> tag supports the Global attribute and Event Attributes.

Examples of HTML <span> Tag

Here are a few examples of the HTML span Tag:

Example 1: Reducing Code and Grouping Styles with <span>

In this example, we use the <span> tag to apply CSS styles directly to specific content, reducing repetitive HTML attributes. This approach ensures cleaner code and a consistent style across elements.

HTML
<!DOCTYPE html>
<html>
  <head>
    <title>GeeksforGeeks span tag</title>

    <!-- style for span tag  -->
    <style>
      span {
        color: green;
        text-decoration: underline;
        font-style: italic;
        font-weight: bold;
        font-size: 26px;
      }
    </style>
  </head>

  <body>
    <span> GeeksforGeeks </span><br />
    <span> GeeksforGeeks </span><br />
    <span> GeeksforGeeks </span><br />
  </body>
</html>

Output:

Span tag with CSS style Example 2: Inline Behavior of <span> Elements

In this example, the <span> tag works as an inline element. Each <span> takes only the space required for its content, allowing multiple <span> elements to appear on the same line without affecting the overall layout.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>GeeksforGeeks span tag</title>
</head>

<body>

    <!-- span tags with inline style/css  -->
    <span style="background-color:powderblue;">
        GfG
    </span>
    <span style="background-color: lightgray;">
        -Contribute-
    </span>
    <span style="background-color: yellow;">
        Article
    </span>
    <span style="background-color: lightgreen;">
        GCET
    </span>
</body>

</html>

Output:

Span_tag_with_CSS_style <span> vs <div> tag

Both <span> and <div> are used as containers in HTML, there are key differences between them:

<span> <div> Inline element Block-level element For styling or grouping inline content For grouping block-level content Does not break the flow of text Starts on a new line and takes up full width Styling or scripting small portions of text Structuring larger sections of content Best Practices for Using the <span> Tag Browsers Support Conclusion

The HTML <span> tag is a versatile inline element used for applying styles or manipulating small sections of content. While it doesn't add any semantic meaning to the content, it plays an essential role in web development by providing hooks for styling and scripting.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4