HTML div and span: Group Content Correctly
Group larger pieces with div
Use a div as a generic flow-content container when no more specific element fits. It can contain ordinary headings, paragraphs and other flow content. Browsers normally display it as a block, but its meaning is not determined by visual layout.
<div class="card"> <h2>Workshop</h2> <p>Bring a notebook.</p></div>The heading and paragraph are grouped. The class is a hook for later styling; it does not create a card design automatically.
Group part of a line with span
Use span for a generic phrasing-content wrapper, such as a few words within a paragraph. It normally appears inline. Do not place a heading, list or div inside span. A class may be shared across elements, whereas an id must be unique within the document.
<p> Room: <span class="room" id="room-a">A12</span></p><p> Room: <span class="room" id="room-b">B08</span></p>Both spans share a class but have different IDs. The paragraph remains a normal line of text.
Choose meaning before a generic wrapper
A generic container adds no particular landmark or emphasis meaning by itself. Use a meaningful element where appropriate: for example, strong for an urgent warning rather than a span whose class merely suggests importance. Choose div or span when grouping itself is the purpose.
<div class="notice"> <p> <strong>Warning:</strong> Wet floor. </p> <p> Area: <span class="location">Lobby</span> </p></div>The div groups the notice, strong communicates importance, and span marks the location for later use.
Practice and next steps
Group two paragraphs and mark a short phrase with a span. Explain why you would use strong for importance.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML div and span: Group Content Correctly — Knowledge Check
Check the core concepts from this lesson.
Which element is appropriate for a generic wrapper around several paragraphs?
Which is valid inside a span in an ordinary paragraph?
Which element best communicates an urgent warning?
You need a styling hook around one word with no extra semantic meaning. Which element fits?
Does class="warning" on a div make it a native semantic warning?