The <div> and <span> tags are general-purpose HTML containers used to group and style parts of a web page. The <div> tag is used for larger blocks or sections, while <span> is used for small inline pieces of text.
<div> to group larger sections like headers, footers, sidebars, or content areas.<span> to style or script small parts of text inside paragraphs or other inline content.Basic syntax:
<div> ... </div> — wraps a block of content.<span> ... </span> — wraps a small inline part of content.Because <div> is block-level, the browser places it on its own line, stacking one block after another. In contrast, <span> behaves like text, so multiple spans can appear on the same line.
<!-- Block-level container -->
<div>
<p>This is a paragraph inside a div block.</p>
<p>Another paragraph inside the same div.</p>
</div>
<!-- Inline container -->
<p>
This is a sentence with a
<span style="color: red; font-weight: bold;">highlighted word</span>
using span.
</p>
Here is a slightly richer example that uses both <div> and <span> with inline styles to show how they affect layout and text flow.
<!-- Block-level container -->
<div style="border: 2px solid #3399ff; padding: 15px; margin-bottom: 15px;">
<p>This paragraph is inside a <div> block.</p>
<p>Multiple elements can go inside a div.</p>
</div>
<!-- Inline container -->
<p>This sentence has a <span style="background-color: #ffdede; color: #b30000;
font-weight: bold;">highlighted text</span> using span.</p>
This paragraph is inside a <div> block.
Multiple elements can go inside a div.
This sentence has a highlighted text using <span>.
Notice how the <div> creates a separate block with its own background and border, while the <span> only highlights a small part of the text without breaking the line.
<div> for larger blocks or page sections like headers, main content, and footers.<span> to style small parts of text inside a line without changing the layout.<div> is block-level (new line), <span> is inline (same line).class or id to divs and spans instead of relying on inline styles, so your CSS stays reusable.<header>, <section>, and <footer> when they make sense, and keep <div> as a generic fallback.<div> for a header, main content area, and footer.<span> and give it a different color and font weight.<div> and replace some with semantic tags like <section> or <article>.<span> inside a <div> to mix block layout and inline text styling.The <div> and <span> tags are fundamental building blocks for structuring and styling web pages. Use <div> to group larger blocks of content and control layout, and <span> to target small inline pieces of text. Combined with CSS classes and IDs, they give you precise control over how your page looks and behaves.