Imagine keeping two handouts in one folder so you can move them together. A page sometimes needs a similar group for styling or later behaviour. Div is a general container for a group of page content. Span groups a small part of text. Neither tells the reader that its content is a menu, an article or a warning.
Div: a general content group
Write an opening div, put related content inside it, and close it with /div. A div can contain headings, paragraphs and other suitable flow content. It has no required attributes. Class can name a reusable group, such as notice. That name is chosen by the author; notice has no built-in browser effect.
Do not add a div around every line by habit. If the content is a paragraph, use p. For a list, use a list. The next chapter introduces more descriptive page-region elements.
Span: a small part within text
A span can identify a room code inside a sentence or mark words in a different language using lang. It can contain suitable text-level content. It is not a container for a paragraph or page section. Write both opening and closing tags clearly.
Choose a more meaningful element when one fits. Strong says words are important. Em marks stress emphasis. A span merely gives a group; making it bold with CSS does not give it the meaning of strong.
Block and inline describe default layout
In ordinary browser layout, a div starts a block on a new line and uses available width. A span flows with nearby words and can wrap across lines. Paragraphs commonly have browser margins; a plain div does not automatically add the same paragraph spacing. These are default presentation behaviours, not permanent promises.
CSS can change display. It does not change meaning or which children HTML allows. Setting a div to display:inline does not make it valid inside p. HTML content rules decide nesting; appearance alone cannot tell you what is allowed.
Class and id have different jobs
Class is reusable: several notices can share class=notice. Id identifies one element uniquely in the document and can serve as a link destination or label connection. Class alone does not add colour, borders or behaviour. A stylesheet must define presentation.
In the second program, .room-code is a CSS class selector: the dot selects that class. Color sets text colour and font-weight sets weight. The .room-note rule adds a border and padding, meaning space inside the border. These small rules make the groups visible; detailed CSS comes later.
Repair nesting, not just appearance
Do not write a div inside p. A browser can close the paragraph early while reading invalid source, so the resulting structure differs from your indentation. Put separate paragraphs inside the div, or use a span for a small part of one paragraph. A span should not wrap a div either.
Think of tags as labelled boxes with rules about what fits inside. Indentation helps humans read those boxes but cannot repair an invalid parent-child relationship.
Follow the idea
Meaning first, then grouping
1Choose a meaningful element if one fits
2Div: a general content group
3Span: a small text group
4CSS: control presentation separately
CSS display does not change HTML nesting rules.
Complete program: grouped-notices.html
Save this as grouped-notices.html. Open it in your browser.
grouped-notices.html
<!doctype html><htmllang="en"><head><metacharset="UTF-8" /><metaname="viewport"content="width=device-width, initial-scale=1" /><title>Group Related Notices</title></head><body><h1>Two study notices</h1><divclass="notice"><h2>Bring your notebook</h2><p>Write one question before the session.</p></div><divclass="notice"><h2>Choose a practice partner</h2><p>Take turns explaining a short example.</p></div></body></html>
Browser previewDocument title: Group Related Notices
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: one main heading, two smaller notice headings and two paragraphs. The div groups do not automatically create cards, backgrounds or borders. They share a class for future styling.
Complete program: inline-labels.html
Save this as inline-labels.html. Open it in your browser.
inline-labels.html
<!doctype html><htmllang="en"><head><metacharset="UTF-8" /><metaname="viewport"content="width=device-width, initial-scale=1" /><title>Group a Small Text Part</title><style>
.room-code {
color: #174a94;
font-weight: bold;
}
.room-note {
border: 1px solid #777;
padding: 0.75rem;
}
</style></head><body><h1>Read a room label</h1><p>Meet in <spanclass="room-code">B-204</span> after class.</p><p><strong>Bring your own notes.</strong> The room code is an identifier, not an importance
warning.
</p><divclass="room-note"><p>The desk label is <spanlang="hi">कक्ष</span>, meaning room.</p></div></body></html>
Browser previewDocument title: Group a Small Text Part
This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.
Expected result: a bold blue room code within its sentence, a separate important reminder and a bordered note. The Hindi word has lang=hi so tools can identify its language.
Problem and solution
Problem: a student wants one blue room code and puts a div inside a paragraph.
Solution: keep one p and wrap only B-204 in a span with a class. Use CSS for colour. The sentence stays one paragraph and the markup expresses the small group.
Common mistakes and useful tips
Do not use span for a page section.
Do not assume div adds paragraph margins.
Class names do not create a built-in design.
Do not duplicate ids when copying groups.
Choose meaningful HTML before generic wrappers.
CSS display changes cannot bypass nesting rules.
Practice questions
Which element groups a room code within a sentence?
Does class=notice create a card by itself?
Can two divs share one class?
Is a div valid inside p after display:inline?
Which better expresses an important warning: strong or a bold span?
Remove the two CSS rules. What remains?
Check your practice answers
Span, when no more specific text meaning is intended.
No. CSS defines its appearance.
Yes. Classes can repeat; ids must be unique.
No. CSS does not change HTML content rules.
Strong expresses importance. A styled span is a generic group.
Content, grouping, language and strong importance remain. The blue code styling and note border disappear.
Interview questions
How do div and span differ?
Both are generic containers. Div groups suitable flow content and is block-level by default. Span groups phrasing content and normally flows within text.
Why avoid unnecessary divs?
Extra wrappers make source harder to read. Use them when a group is needed and no more suitable element fits.
Does a class change meaning?
No. A class called warning does not create warning meaning or automatic styling. Choose markup and presentation separately.
Are block and inline the complete nesting rules?
No. They describe layout behaviour. HTML content categories and each element's content model determine allowed children.
Quick revision
Div groups content; span groups text-level content.