Q&A
What is the difference between div and span?
Overview
The core rule: <div> = block, <span> = inline. This single difference drives when to reach for each one. Block elements stack vertically; inline elements sit in the text flow. Both are "generic" (carry no meaning of their own) and are styled or scripted via class/id attributes, which is exactly their use case - grouping elements for CSS or JavaScript hooks when no semantic tag applies.
graph LR
A["<div>: block-level"] --> B["Starts on new line<br/>Full parent width by default<br/>Contains block or inline content"]
C["<span>: inline"] --> D["Flows in text<br/>Shrinks to content width<br/>Should contain only inline content"]
style A fill:#1e1b4b,color:#fff
Visual Behavior
<p>
This is <span style="color:purple">purple text</span> inside a paragraph.
</p>
<div style="background:#eee">First block section</div>
<div style="background:#ddd">Second block section - on its own line</div>
<span> wraps inline content without creating a line break; <div> always starts a new block.
The Rules
<div>- block; occupies the full width, stacks vertically, can contain block and inline elements, responds towidth/height/margintop and bottom.<span>- inline; flows with text, width is just the content size,width/height/margin-top/margin-bottomhave no effect (usedisplay: inline-blockif you need those).
<!-- div: layout grouping -->
<div class="card">
<div class="card-header">Title</div>
<div class="card-body">Content</div>
</div>
<!-- span: inline text styling -->
<p>Balance: <span class="highlight">,200</span> remaining.</p>
<p>Press <span class="key">Ctrl</span> + <span class="key">C</span> to copy.</p>
Semantic vs Generic
Both <div> and <span> are semantically empty - they convey no meaning to browsers, assistive technology, or search engines. Before reaching for them, check whether a semantic element fits:
| Instead of | Consider |
|---|---|
<div> for navigation |
<nav> |
<div> for a page header |
<header> |
<div> for main content |
<main> or <article> |
<div> for a sidebar |
<aside> |
<span> for emphasized text |
<em> or <strong> |
<span> for a citation |
<cite> |
Use <div> and <span> when you genuinely need a grouping or hook with no semantic meaning - for CSS grid/flex containers, JavaScript targets, custom component wrappers, etc.
Comparison Table
| Property | <div> |
<span> |
|---|---|---|
| Display type | block |
inline |
| Line break | Before and after | None |
| Default width | 100% of parent | Content width |
width/height CSS |
Works | No effect (unless inline-block) |
margin-top/bottom |
Works | No effect |
| Can contain | Block + inline | Inline only |
| Semantic meaning | None | None |
When Each Is Appropriate
<div>- layout containers, grid/flex wrappers, card components, modal overlays, section groupings, React component root nodes.<span>- highlight a word/phrase, apply an icon class inline, wrap a price or count for styling, target specific text for JavaScript.
Why This Matters in Interviews
HTML interviews use this to check box-model and layout fundamentals. Know that the block-vs-inline distinction governs behavior, the CSS differences (width/height on inline elements), and - crucially - that semantics come first (<section>, <em>, etc.) and <div>/<span> are "last resort" generic wrappers. This also feeds into why display: inline-block and display: flex/grid on <div> work the way they do.
Common Pitfalls
- Using
<div>everywhere instead of semantic elements (hurts accessibility and SEO). - Nesting block elements inside
<span>- invalid HTML (inline elements can't contain block). - Expecting
widthormargin-topon a<span>to work (usedisplay: inline-block). - "Div soup" - deeply nested, meaningless
<div>chains that make code unreadable.