Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Use the span and div Elements Correctly?

HTML
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 27, 2026
5 min read

Use <div> as a generic block-level container to group sections of a page for layout or styling, and use <span> as a generic inline container to style or label part of a line of text. The html div vs span rule is simple: <div> starts on a new line and takes the full width, while <span> stays in the flow of the text. Reach for either one only when no meaningful element fits.

Both have no meaning of their own. They exist so that you can attach a class, an id or an attribute to a piece of content.

Table of Contents

  • The difference between block and inline
  • Using div to group and lay out content
  • Using span to style part of a line
  • Prefer semantic elements when they exist
  • Accessibility, ARIA and classes
  • Common mistakes
  • Frequently Asked Questions
    • Can I put a span around a div?
    • Can I change div to display inline?
    • Do div and span affect SEO?
    • Is div soup really a problem?
  • Summary
  • Related Articles

The difference between block and inline

A <div> is a block-level element: it starts on a new line and stretches to the full width of its parent, and you can set width, height, margins and padding on it. A <span> is inline: it only takes up the space of its content, does not create a line break, and ignores width and height.

html

The two <div> elements appear on separate lines, while the <span> sits inside the sentence. These are the browser default display values, and you can change them with the CSS display property.

Using div to group and lay out content

A <div> groups several elements so that you can style them together or position them with CSS Grid or Flexbox.

html
css

The container applies one set of rules to the whole group. A wrapper like this is also what you need for components in React or Vue, or when a script must find a region of the page.

Using span to style part of a line

A <span> marks a few words so you can change their style or hook a script to them.

html
css

A span is also useful for language changes, with <span lang="fr">bonjour</span>, and for text that a script updates, such as <span id="count">0</span>.

Prefer semantic elements when they exist

Because a <div> or <span> says nothing about the content, search engines and screen readers get no help from them. Before using one, check whether a semantic element fits.

Instead ofUseReason
<div class="header"><header>Marks the page or section header
<div class="nav"><nav>Marks navigation links
<div class="main"><main>The main content landmark
<div class="footer"><footer>Footer landmark
<div class="article"><article>Self-contained content
<div class="button"><button>Keyboard and screen reader support built in
<span class="strong"><strong>Marks strong importance
<span class="em"><em>Marks stress emphasis
<span class="date"><time>Machine-readable date

The button row is the most important: a clickable <div> does not get focus, Enter or Space handling, or a role, so it fails for keyboard users. If you must make one, you have to add all of that yourself, and a real <button> is better.

Accessibility, ARIA and classes

A <div> or <span> has no role, so assistive technology ignores it and reads only the content. If you build a custom widget from these elements, you need ARIA roles, states and keyboard handling. Use classes to describe what content is, for example class="price", and not how it looks, for example class="red-bold". Avoid deep stacks of nested <div> elements, which people call div soup, because they make the markup hard to read and slow to maintain. Modern CSS often removes the need for extra wrappers.

Common mistakes

  • Putting a <div> inside a <p>: a paragraph can contain only inline content, so the browser closes the paragraph early. Use a <span> or change the structure.
  • Setting width or height on a <span>: it has no effect unless you change display to inline-block or block.
  • Using <div> for text that should be a heading, which breaks the outline of the page.
  • Using many <span> elements for spacing instead of CSS margins or padding.
html

Frequently Asked Questions

Can I put a span around a div?

Not as valid HTML in most cases: an inline element should not contain block elements. Use a <div> or change the wrapper to a block element with CSS.

Can I change div to display inline?

Yes. Set display: inline or inline-block in CSS. The element is still a generic container.

Do div and span affect SEO?

They have no direct effect. Semantic elements help engines and readers understand structure, so use them where they apply.

Is div soup really a problem?

It makes pages harder to maintain and to navigate for assistive technology. Use landmarks and headings, and add wrappers only when needed.

Summary

Use <div> for generic blocks and <span> for generic inline pieces, and only when no semantic element describes the content. Remember that block and inline behavior can be changed in CSS, keep class names meaningful, and use real buttons, headings and landmarks for accessibility.

Related Articles

  • How Do You Show Dates and Times With the time Element?
  • How Do You Write Hindi Text and Set Languages in HTML?
  • Learn HTML: Complete Study Roadmap from Beginner to Advanced
Share