How to Use Span in HTML: Unleashing Your Web Design Potential

By Cristian G. Guasch •  Updated: 09/18/23 •  9 min read

If you’re new to HTML or just brushing up your web development skills, understanding the use of the ‘span’ element can make a significant difference. This versatile but often misunderstood tool is key to controlling how your text appears on a webpage. It’s like the Swiss Army Knife in your HTML toolbox, allowing for precise control over small chunks of content without disrupting the overall flow.

So what exactly does it do? The ‘span’ tag in HTML is an inline container that can be used to style strings of text within a paragraph or other block-level elements. Unlike ‘div’, which creates a new line and block of content, ‘span’ doesn’t disrupt the layout and simply styles wherever it’s placed.

Here’s an example: If I want to change color of one specific word in my paragraph, wrapping it with span tags will do the trick! Use CSS rules linked with this span element – and voila! You’ve got yourself a uniquely styled piece of text surrounded by untouched content. Understanding how to effectively use ‘span’ is fundamental for any aspiring web developer or designer.

Understanding the Span Tag in HTML

Embarking on a journey into HTML, you’ll soon run across the span tag. It’s one of those elements that don’t carry any inherent semantic meaning, yet hold tremendous power when it comes to manipulating inline elements for styling purposes.

In its bare form, <span> might seem insignificant. After all, it doesn’t alter the default browser behavior or visual representation of content. But pair it with CSS and JavaScript, and you’ve got yourself an indispensable tool. I’m talking about tweaking colors, changing fonts, wrapping portions of text – all without disrupting the normal flow of your document.

Let’s illustrate with a few examples:

<span style="color: blue;">This text will be blue.</span>
<span style="font-size: 20px;">This text will be larger than usual.</span>

You see how straightforward that is? And let’s not forget about classes and IDs. By attaching these identifiers to our span tags, we can target specific inline elements with surgical precision:

<span class="highlight">This text stands out!</span>

Then in your CSS:

.highlight {
    background-color: yellow;

Remember though – while <span> is a powerful tool for local changes within blocks of content (think paragraphs or divs), it shouldn’t be used as a substitute for other semantically meaningful tags like paragraph (<p>) or heading (<h1> through <h6>) tags.

Oh! And here’s a bonus tip before I wrap this up – if you need to group block-level elements together instead of inline ones? That’s where its big brother <div> steps in!

So there you have it – from seemingly irrelevant to absolutely essential; that’s what the humble span tag brings to your HTML toolkit!

Syntax and Basic Usage of Span in HTML

Let’s dive right into the world of HTML tags, specifically focusing on the <span> tag. It’s one of those simple yet potent entities that open up a broad spectrum of possibilities when it comes to formatting text on your web page.

The syntax for using a span tag is deceptively simple. You just need to enclose your content within the opening <span> tag and closing </span> tag. For instance, if you want to apply some style to a specific word or phrase within a paragraph, you might do something like this:

<p>This is an <span>important</span> point.</p>

In its basic form, as seen above, the span tag doesn’t affect how your content appears. However, it creates an ‘invisible’ container around the enclosed content which can be manipulated using CSS (Cascading Style Sheets). This is where things start getting interesting!

Let’s assume you want to highlight that important point from our previous example. Here’s how you would do it:

<p>This is an <span style="color: red;">important</span> point.</p>

What happened here? Well, I’ve used inline CSS within my span element to change the color of the enclosed word – “important”. And voila! The word now stands out in bold red.

But what if you had multiple words or phrases throughout your document that needed highlighting? Would you need to manually add in-line styles each time? Thankfully not! That’s where classes come into play. Here’s how we can achieve the same effect with less repetition:

.highlight {
  color: red;

<p>This is an <span class="highlight">important</span> point.</p>
<p>This is another <span class="highlight">crucial</span> point.</p>

In this case, we’ve created a CSS class named “highlight” and assigned it to our span tags. This way, any content enclosed within a span tag with the “highlight” class will be colored red.

You can see how the <span> tag offers you the flexibility to isolate and style specific parts of your text. It’s like giving you a fine-tipped brush in a world where most HTML elements are more like paint rollers! So go on, play around with it, mix it up, and bring your web pages to life!

Styling with CSS and Span in HTML

Alright, it’s time to get our hands dirty with some real coding. I’ll guide you through the process of styling your web pages using CSS and span tags in HTML. It’s simpler than you might think!

The first thing you should know is that span is an inline element in HTML. Essentially, this means it doesn’t start on a new line and only takes up as much width as necessary. This makes span ideal for applying styles to specific parts of your text without affecting the layout of your page.

Let me show you how it works:

<p>My favorite color is <span style="color: blue;">blue</span>.</p>

Here, we’ve used a span tag to change the color of the word “blue” while leaving the rest of the sentence unaltered.

But what if we want to apply similar styling to multiple elements? That’s where CSS classes come into play. Instead of defining styles for each individual span, we can define a class once and use it wherever needed.

Here’s an example:

  .highlight {
    background-color: yellow;
    font-weight: bold;

<p>I love <span class="highlight">chocolate ice cream</span>!</p>
<p>My cat is so <span class="highlight">fluffy</span>.</p>

In this case, any text within a span tag with class “highlight” will have a yellow background and bold font. It’s pretty cool right?

Remember, though, that while spans are incredibly useful for altering small chunks of text or other inline elements within your webpage, they don’t possess any inherent styles or behaviors like some other HTML tags do (like headings or paragraphs). They’re like blank canvases waiting for you to add your stylistic flair!

Finally, keep in mind that while we’ve been talking about text, span can be used with any inline elements. Images, links, or even other spans – if it’s inline, you can wrap a span around it and give it some style.

So there you have it! With the humble span tag and a dash of CSS, you’re well on your way to creating beautifully styled web pages. Keep experimenting and happy coding!

Diving into the sea of HTML can be a bit overwhelming, especially when you’re new to it all. One common element that often causes confusion is the humble span tag. But don’t worry! I’m here to guide you through some practical examples that’ll help demystify how we use this handy little HTML buddy.

Right off the bat, let’s get clear on what a span is in HTML. In simplest terms, it’s an inline container used for styling text within a document. It doesn’t have any inherent formatting or influence on the structure of your page.

Consider this piece of code:

<p>This is my <span style="color:red">favorite</span> color.</p>

See how we’ve just changed the color of one word? That’s because we wrapped ‘favorite’ with our good friend – Mr Span!

Now, let’s take this up a notch and play around with CSS classes:

.bold-and-blue { font-weight: bold; color: blue; }

<p>This is my <span class="bold-and-blue">favorite</span> book.</p>

In this example, we’re defining styles in CSS and using them within our span element. The result? A nice bold and blue ‘favorite’.

But wait! There’s more! Span tags also come in handy when working with JavaScript:

function highlight() {
  var x = document.getElementById("mySpan"); = "orange";

<p>This is my <span id="mySpan" onclick="highlight()">favorite</span> fruit.</p>

Clicking on ‘favorite’ invokes our JavaScript function ‘highlight’, which changes its color to orange. Cool, right?

In conclusion (not really), these are but a few ways you can effectively leverage span tags in your web development journey. Remember, it’s a tool that’s as powerful as your creativity allows!

Cristian G. Guasch

Hey! I'm Cristian Gonzalez, I created HTML Easy to help you learn HTML easily and fast.

Related articles