Span vs. Div: The Difference Explained in 1100 Words or Less

Download Now: Free HTML & CSS Coding Templates
Jamie Juviler
Jamie Juviler

Published:

Go to any major website, open the source code, and you'll likely notice a heap of <div> and <span> tags. Learning the intricacies between span vs div can be overwhelming, mainly because they're remarkably similar. But one thing's for sure: If you're unsure about what these elements are used for, or the difference between span and div, it's more challenging to comprehend how web pages are coded. Download Now: 50 Code Templates [Free Snippets]

three people in an office looking at a computer and learning the difference between span and div tags

This post will walk you through everything you need to know about these prevalent elements. From similarities to differences between span and div and beyond, we've got you covered.

To fully grasp the overlap (and disparities) between span vs div, let's walk through each element separately first.

Free Guide: 25 HTML & CSS Coding Hacks

Tangible tips and coding templates from experts to help you code better and faster.

  • Coding to Convention
  • Being Browser-Friendly
  • Minimizing Bugs
  • Optimizing Performance
Learn more

    Download Free

    All fields are required.

    You're all set!

    Click this link to access this resource at any time.

    div

    The div (division) element is a generic block-level element. You most frequently use it to divide your page content into digestible blocks. A block element is a page element that starts a new line and has a width equal to the entire page or the parent container.

    It's extremely common to see divs used to group related paragraphs, images, headings, and links together. For instance, consider this. A three-paragraph article may be enclosed in a div, and a navigation menu containing links might be enclosed in another div. Using divs this way makes it easier to identify different page sections and apply styling to them with CSS.

    Here's an example of what a div element in HTML looks like:

    <div id=“paragraphs”> <p>This is my first paragraph.<p> <p>This is my second paragraph.</p> <p>This is my final paragraph.</p> </div>

    span

    Span is a generic inline element often used to apply styling to a portion of content. An inline element does not start a new line and only takes up as much space on the page as its content. Span tags are used on small segments of text, links, images, and other HTML elements that appear inline with the surrounding content.

    A basic example of a span element in HTML looks like this:

    <p>This is a paragraph with <span id=“special-text”>a little something extra</span> inside it.</p>

    To summarize, a div tag creates a block-level element while a <span> tag wraps around an inline element. Despite their differences, both are used to group like elements for structural and/or styling purposes.

    Difference Between Span and Div

    We can summarize the primary differences between span vs div like this: A div tag creates a block-level element while a <span> tag wraps around an inline element. Additionally, the <span> tag is used to group smaller pieces of text together, whereas div can be used to group larger ones. Another significant difference between span and div elements is nesting. While it's common to see div elements nested, it's best practice to avoid nesting <span> tags to avoid confusion. 

    Despite their differences, you can use both to group like elements for structural and/or styling purposes.

    span vs div Examples

    Now, let's take a look at a few examples of span vs div that demonstrate their differences and applications.

    Here's an instance of two <div> tags, followed by two <span> tags. I've applied a unique background color to each element to show their size — notice how divs take the entire page width while span tags only take up the space of their content. Additionally, each div adds a line break after its content, but this isn't the case for span elements. 

    See the Pen span vs div 1 by Christina Perricone (@hubspot) on CodePen.

    Up next, we have a more representative example of how you'll see div and span used together on web pages. Here, divs separate larger chunks of content, and spans wrap segments within these divs.

    See the Pen span vs div 2 by Christina Perricone (@hubspot) on CodePen.

    When to Use span vs div

    As you can see in the above examples, both div and span are useful for styling and structuring web pages. It's easy to assign id or class attributes to these elements and target them with CSS rules. You'll usually see span tags used to style inline content like a word in a line of text.

    Div tags, on the other hand, should style larger sections of content or act as a container for child elements. More complex web pages often make heavy use of divs, nesting them in each other to represent different page regions and sub-regions.

    Given the convenience of span and div, it's tempting to use these tags any time you want to add a new page element — for instance, using a <div> tag instead of a <p> tag. But think twice before you do that. In fact, it's wise to limit their use as much as possible. 

    Wondering why? Well, because span and div are "generic" elements, neither conveys meaning about the content it contains or its purpose. You could put many things inside a <div> or a <span> tag, but it's unclear from the tag itself what the purpose of the element is or the content inside.

    The opposite of a generic element is a semantic element, one whose tags communicate the meaning of the element. Most HTML elements are semantic — <p><em><h1> to <h6>, and <button> are some examples you probably already know. 

    Semantic elements make it easier for search engine crawlers, assistive technologies such as screen readers, and web developers to interpret your web page. Plus, they are much tidier than a maze of meaningless <div> and <span> tags.

    Whenever you're considering a div or span to section off a part of your page, first consider whether there's a suitable semantic HTML element that you can use instead. HTML5 introduced several block-level div alternatives that are handy for common page regions, including <header><footer><nav>, and <main>. There are also semantic inline elements for text like <strong><em> (emphasis), <code>, and <q> (quote).

    Free Guide: 25 HTML & CSS Coding Hacks

    Tangible tips and coding templates from experts to help you code better and faster.

    • Coding to Convention
    • Being Browser-Friendly
    • Minimizing Bugs
    • Optimizing Performance
    Learn more

      Download Free

      All fields are required.

      You're all set!

      Click this link to access this resource at any time.

      div and span: Know the Difference

      Divs and spans are among the most common HTML elements, and for good reason — they let you structure and style your HTML pages precisely to your specifications.

      Still, they both have a specific purpose, and understanding the difference between span and dev ensures that you're using each properly and only when necessary. Ultimately, this will save you a lot of confusion and scrapped code later.

      Editor's note: This post was originally published in May 2021 and has been updated for comprehensiveness.

      New Call-to-action

      Topics: HTML

      Related Articles

      Dozens of free coding templates you can start using right now

        CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

        START FREE OR GET A DEMO