💡 Tip: Login now to track your course progress and unlock your downloadable certificates instantly!

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
<div>Content goes here...</div>
Div Example
<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
<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
<span>Inline text here...</span>
Span Example
<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
<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
<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
<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.

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.

Sign In Form

User your email and password to singn in

Don’t have an account, signup here : 

HTML5 & CSS3

Tools and Tutorials

Want a Website

Want a website for your business ?

Please fill out the details below, so we can reach out to you.

Registration Form

Signup to track your record and much more.

We have sent you an email with a registration link. Please click the link to verify your email address, or enter the 6-digit OTP provided in the email.

Didn't receive the OTP. Regenerate OTP Resend