HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
  • Insights
  • Verify Certificate
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials
HTML5ANDCSS3 logoHTML5ANDCSS3Tools and Tutorials

HTML5andCSS3.org helps developers learn modern frontend skills with practical tutorials, production-ready snippets, and fast web tools built for everyday coding.

Quick Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Copyright
  • Disclaimer

Top Tools

  • Try It Editor
  • JSON Formatter
  • CSS Minifier
  • HTML Minifier
  • Base64 Encoder / Decoder

Contact

  • business@html5andcss3.org
Newsletter

Learn at your own pace, practice with useful tools, and build websites you're proud of.

© 2011-2026 html5andcss3.org. All rights reserved.

How to Use

HTML Tutorial

HTML Basics

Introduction to HTMLHTML EditorsHTML StructureHTML Elements & AttributesHTML HeadingsHTML ParagraphsHTML FormattingHTML CommentsHTML Styles

HTML Text and Links

HTML Text FormattingHTML Quotations & CitationsHTML LinksHTML Email Links

HTML Lists and Tables

HTML ListsHTML TablesTable Styling & Borders

HTML Images and Media

HTML ImagesHTML Picture ElementHTML AudioHTML VideoHTML Embed & Object

HTML Forms

HTML Forms OverviewHTML Form ElementsHTML Input TypesHTML Input AttributesHTML Form AttributesHTML Form Validation

HTML Layout

HTML Block vs Inline ElementsHTML Div & SpanHTML Semantic Layout TagsHTML Iframes

Advanced HTML

HTML Entities & SymbolsHTML EmojisHTML Head Element TagsHTML Meta Tags for SEOHTML Responsive Design

HTML References

HTML5 New TagsDeprecated TagsHTML Global AttributesEvent AttributesComplete HTML Tag ListComplete HTML5 ElementsHTML Tutorial PDF
  1. Home
  2. /
  3. Tutorials
  4. /
  5. HTML Tutorial
  6. /
  7. HTML Div & Span

HTML Layout

HTML Div & Span

The <div> and <span> elements are two of the most commonly used tags in HTML.
They do not add any visual change by themselves but are extremely useful for grouping content, styling, and layout control using CSS.

Introduction

Both <div> and <span> are container elements:

  • <div> is a block-level container.
  • <span> is an inline container.

They are often used with CSS or JavaScript to structure and style specific parts of a web page.

The <div> Element

The <div> tag defines a division or section in a webpage.
It is a block-level element, meaning it starts on a new line and takes up the full width.

Div Syntax

html

<div>Content goes here...</div>

Div Example

html

<div style="background-color: #f0f0f0; padding: 10px;">
  <h2>Welcome to HTML</h2>
  <p>This section is wrapped inside a div element.</p>
</div>

Explanation:

  • The <div> element groups the heading and paragraph.
  • Styling (like background color and padding) is applied to the entire section.

Common Uses of <div>

  • To create page sections like header, footer, sidebar, or content area.
  • To apply CSS styling to grouped elements.
  • To structure layouts for responsive design.
  • To apply JavaScript functions to a specific section.

Example: Page Layout Using <div>

Div Example

html

<div id="header">Header Section</div>
<div id="menu">Menu Section</div>
<div id="content">Main Content Section</div>
<div id="footer">Footer Section</div>

Each <div> can be styled separately using CSS.

The <span> Element

The <span> tag is used to group inline elements for styling or scripting purposes.
Unlike <div>, it does not start a new line and only takes up the width of its content.

Span Syntax

html

<span>Inline text here...</span>

Span Example

html

<p>Welcome to <span style="color: red;">HTML5</span> Tutorial.</p>

Explanation:

  • The <span> tag highlights a specific part of text within a paragraph.
  • CSS is used to color only that section red.

Difference Between <div> and <span>

Feature<div><span>
TypeBlock-levelInline
Starts a new lineYesNo
Used forLarge sections or containersSmall text or inline content
Typical useLayouts, grouping sectionsHighlighting or styling inline text
Default width100%As per content

Combining <div> and <span>

You can use both tags together for flexible styling.

Combined Example

html

<div style="background: #eef; padding: 10px;">
  <h3>User Details</h3>
  <p>Name: <span style="color: blue;">Amit Sharma</span></p>
  <p>Role: <span style="font-weight: bold;">Developer</span></p>
</div>

Explanation:

  • <div> groups the section.
  • <span> styles specific text portions within paragraphs.

Styling <div> and <span> with CSS Classes

Instead of inline styles, it’s best practice to use CSS classes.

Div & Span Style Example

html

<style>
  .box { background: lightgray; padding: 10px; margin: 5px; }
  .highlight { color: green; font-weight: bold; }
</style>

<div class="box">
  <p>This is a <span class="highlight">highlighted</span> word inside a div.</p>
</div>

Explanation:

  • The .box class applies layout and spacing to the <div>.
  • The .highlight class changes color and font weight for text inside <span>.

Practical Example: Layout with Div & Span

Practical Example

html

<div style="border: 1px solid #ccc; padding: 10px;">
  <h2>Product Information</h2>
  <p>Product Name: <span style="color: brown;">Wireless Mouse</span></p>
  <p>Price: <span style="color: green;">$25</span></p>
  <p>Description: <span>A smooth and precise wireless mouse for everyday use.</span></p>
</div>

Explanation:

This structure allows easy styling and layout control. Each <span> can be customized independently.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Conclusion

  • The <div> element is used for structuring and organizing large sections of content.
  • The <span> element is used for styling and targeting specific parts of inline text.
  • Together, they form the building blocks of modern web layouts, especially when combined with CSS and JavaScript.
PreviousHTML Block vs Inline ElementsNextHTML Semantic Layout Tags