What Does span Do in HTML? Unlocking Its Full Potential

Marilena

By Marilena

5 min read
Bookmark this post

The <span> tag is a powerful yet often overlooked element in HTML. It’s commonly used for adding style and interactivity, but what does span do in HTML exactly? In this guide, we’ll explore its full potential and how to use it effectively in your web pages.

What is a span tag?

We use HTML <span> tag to target and manipulate specific parts within an HTML element without affecting the layout of surrounding elements. In simple terms, it acts as an inline container for styling small portions of text.

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

Why and when to use span in HTML?

Since <span> does not introduce additional structure or spacing, it is ideal for precise, targeted styling such as:

  • Highlighting text, changing colors, or formatting specific letters, words, or even a small paragraph.
  • Wrapping text for CSS animations or visual effects.

The difference between span and div

While both <div> and <span> are used for grouping and styling elements, they behave differently in terms of layout and structure. The table below highlights their key differences:

span
div
inline element
block element
stays within same line
starts new line and takes full width
does not affect layout
breaks the normal text flow

HTML span using inline styling

Below, I’ve prepared an example to illustrate how we use <span>, along with an image to help you visualize it better. 🤓
In this example, we use <span> to change the style of multiple parts of the text within a <p> element. The <span> tag wraps around specific words we want to highlight or style differently without affecting the entire line.

<p>
  My sandwich has 
  <span style="color:yellow">yellow</span> cheese,
  <span style="color:red">red</span> tomatoes and
  <span style="color:green">green</span> fresh lettuce!!
</p>
HTML
What does span do in HTML: Text displaying "My sandwich has yellow cheese, red tomatoes and green fresh lettuce". Each color word is written with its color as we set the span HTML tag.

Styling span in CSS: Best practices

We can also style <span> elements beyond CSS inline styling. Below, I include an example and an image to picture it.
In this example, we place the <span> element within a <div> element. The CSS code applies specific styling (color, font style, and text-decoration) only to the text inside the <span> tag, allowing us to highlight or change the look of a selected part without affecting the rest of the text.

<div>
  My sandwich is <span>amazing!!</span>
</div>
HTML
div > span {
  color: red;
  font-style: italic;
  text-decoration: underline;
}
CSS
What does span do in HTML: Text displaying  "My sandwich is amazing". The word amazing is styling differently than the rest sentence as we set the span HTML tag.

🔖 Here’s a trick: With div > span, we’re styling only <span> elements that are direct children of a <div>. This way, we target only this specific span, without affecting other <span> elements in the code! 🔴

Curious to learn more about how to focus on the exact elements you want? Look at my CSS selectors post — it’s all about precision! 🎯

Targeting letters one by one using span in CSS

Want to take it a step further? We can make it even more impressive by styling each letter of one word individually!! 🧨 ✨

<h1>
  My sandwich is 
  <span class="letter-a">a</span>
  <span class="letter-m">m</span>
  <span class="letter-a">a</span>
  <span class="letter-z">z</span>
  <span class="letter-i">i</span>
  <span class="letter-n">n</span>
  <span class="letter-g">g</span>
  <span class="exclamation-mark">!!</span>
</h1>
HTML
.letter-a {
  color: deeppink;
}

.letter-m {
  color: green;
}

.letter-z {
  color: indigo;
}

.letter-i {
  color: purple;
}

.letter-n {
  color: blue;
}

.letter-g {
  color: darkorchid;
}

.exclamation-mark {
  color: magenta;
}
CSS
What does span do in HTML: Text displaying  "My sandwich is amazing". The word amazing is styling differently than the rest sentence as we set the span HTML tag.

Learn how to use HTML span the fun way

What about making our learning more fun and using the metaphor of a sandwich to represent how the <span> elements are surrounded by their container element (HTML opening and closing tags)!

Here’s the idea 💡

Sandwich with Span as the Filling 🍔

Imagine an illustration where a slice of text is styled like a sandwich with:

  • Bread Slices: Representing HTML tags surrounding the <span> element.
  • Fillings (cheese, tomato, lettuce): Representing the words or phrases wrapped in the <span> elements, styled differently to stand out, just like all these fillings inside a sandwich.

This metaphor will show the positioning of <span> clearly! 🥳

What does span do in HTML - A hamburger represantation of a span tag

🌼 Hope you found my post interesting and helpful. Thanks for being here! 🌼

DigitalOcean Referral Badge
Tags

html

guest
0 Comments
Inline Feedbacks
View all comments

Continue reading

Front, Programming

Make a Unique Folded Corner Effect – Step By Step Guide

Front, Programming

Keyframes in CSS Explained – Make Your Animations Stand Out

Back, Programming

How To Use Absolute Paths In A Node Project (2024)

Programming

Javascript Array Methods – Easy Examples That Make Learning A Blast

Front, Programming

How To Make CSS Reflection – The Easy Way

Front, Programming

The Unique CSS Opacity – Check Out This Amazing Property

Front, Programming

Contrast And Brightness: Empower CSS Filter For Powerful Images

Front, Programming

Opacity And Saturate: Empower CSS Filter For Unique Images

Front, Programming

CSS Last Child Selector – The Most Valuable Selector

Front, Programming

Most Useful HTML Elements for Maximizing Better Results

Front, Programming

HTML Formatting Elements Made Simple: With Easy And Practical Examples

Front, Programming

HTML Lists Made Easy: Ordered, Unordered, and Special Lists

Subscribe to our newsletter

Dive into the Fun Side of Tech! Posts, News, and More Delivered to Your Inbox!

Intuit Mailchimp