Throughout this module you have used a lot of elements: headings, paragraphs, lists, quotations, strong, em, mark… You may have noticed that some of them always start on a new line and take up the full width (an h2, a p, a ul), while others fit into the line of text without breaking it (a strong, an abbr). This difference comes from HTML's flow model, and understanding it is essential for writing valid HTML and, later on, for building layouts with CSS. In this lesson you will learn what block and inline elements are and which nesting rules follow from them, and you will meet the only two HTML elements with no meaning of their own: div, a generic block container, and span, a generic inline container. With them, and with the class and id attributes, you will get The Blue Cup's home page ready for styling.
Contents
- The flow model: block versus inline
- Classifying the elements you already know
- Nesting rules
div: the generic block containerspan: the generic inline container- Grouping with
classandid - A visual preview: seeing the boxes
- The problem of "divitis"
- Project: organising
index.htmlinto blocks
The flow model: block versus inline
The browser places elements on the page following the normal document flow: from top to bottom and, within each line, in the reading direction (from left to right in English). In that flow, each element behaves by default in one of two ways:
| Feature | Block element | Inline element |
|---|---|---|
| Where does it start? | Always on a new line | On the same line, following the text |
| How wide is it? | The full available width of its container | Only the width of its content |
| What comes after it? | The next element starts below | The text continues on the same line |
| What does it usually contain? | Other blocks or text | Text and other inline elements |
| Analogy | A box stacked on a shelf | An underlined word within a sentence |
<p>This paragraph is a block that contains an <strong>inline fragment</strong> and carries on.</p>
<p>This is another block: it starts below the previous one.</p>The first p takes up the full width of the window (even though its text is short); the strong takes up only as much space as "inline fragment" needs, and the text continues after it. The second p starts on a new line.
You can check this with DevTools: in the Elements panel, hover over a p and the browser will highlight a box that reaches the right-hand edge; hover over a strong and the box will fit tightly around its words.
A clarification: the modern HTML specification no longer talks about "block" and "inline" but about content categories (flow content, phrasing content…). And the visual behaviour is actually decided by the CSS
displayproperty, which can be changed (you will see this in Module 8). Even so, the block/inline distinction is still the most practical way to understand how each element behaves by default and what can go inside what.
graph TD
B["body"] --> P1["p (block)"]
B --> P2["p (block)"]
P1 --> T1["text"]
P1 --> S["strong (inline)"]
P1 --> T2["text"]
P2 --> T3["text"]
Classifying the elements you already know
| Block | Inline |
|---|---|
h1, h2, h3, h4, h5, h6 |
strong, b, em, i, u |
p |
mark, small |
hr |
s |
ul, ol, li |
sub, sup |
dl, dt, dd |
abbr |
blockquote |
code, kbd, samp, var |
pre |
q, cite |
div (in this lesson) |
br, a, span (in this lesson) |
Two special cases:
delandinsare transparent: they behave like the context they are in. You will normally use them inline, inside a paragraph, as you did innews.html.a(the link) is inline, but HTML5 allows it to wrap entire blocks in certain cases. You will see this in Creating Hyperlinks (Module 3).
Nesting rules
The basic rules for nesting elements correctly follow from this model:
- An inline element can go inside a block. This is the usual case:
<p>… <em>…</em> …</p>. - An inline element can contain other inline elements:
<strong>Notice: <abbr title="…">VAT</abbr></strong>. - An inline element must not contain blocks.
<strong><p>…</p></strong>is incorrect. - Some blocks only accept inline content. This is the case for
p,h1–h6,dtandpre: they cannot contain other blocks. - Other blocks accept blocks inside them:
li,dd,blockquoteanddivcan contain paragraphs, lists and other blocks. - Some elements require specific children:
ul/olonly acceptli, anddlonlydt,dd(or adivgrouping them).
<!-- Incorrect: a block (h2) inside an inline element (em) -->
<em><h2>Our bakery</h2></em>
<!-- Correct: the inline element inside the block -->
<h2><em>Our</em> bakery</h2>
<!-- Incorrect: a list inside a paragraph -->
<p>We have:
<ul><li>Coffee</li></ul>
</p>
<!-- Correct: the paragraph and the list are sibling blocks -->
<p>We have:</p>
<ul><li>Coffee</li></ul>In the case of the list inside the paragraph, the browser won't warn you: it will automatically close the p before the ul and leave an orphaned </p>, which it will turn into an empty paragraph. You will see this in the Elements panel of DevTools. The validator (Module 10), on the other hand, will flag it as an error.
div: the generic block container
div (division) is a block element with no meaning at all. Its only purpose is to group other elements so they can be treated as a unit, usually to style them or lay them out with CSS.
<div>
<h2>Offer of the week</h2>
<p>White coffee and croissant for €3.50.</p>
<p><small>Valid Monday to Friday.</small></p>
</div>- Without CSS, a
divchanges nothing visually: it doesn't add margins or borders. It simply starts on a new line, like every block. - To a screen reader or a search engine, a
divcommunicates nothing: it's as if it weren't there. - It can contain any block or inline content, and be nested inside other
divelements.
So what is it for? So that later on you can say "this whole group has a coloured background" or "these three groups go in columns". Without a common container, that would be impossible.
span: the generic inline container
span is the inline equivalent of div: an element with no meaning that wraps a fragment of text within a line so that it can be identified.
- Without CSS, it doesn't change anything either: the text looks exactly the same.
- It's used when you want to mark up a fragment and no semantic element fits. Here, the name of the café isn't more important (
strong) and doesn't carry emphasis (em); we just want to be able to give it, for example, the brand colour.
One use of span that has value of its own, even without CSS, is to indicate a change of language with the global lang attribute when there is no difference in voice to justify i:
<p>On the blackboard at the entrance, for our Spanish-speaking visitors: <span lang="es">buen café, buen humor</span>.</p>div |
span |
|
|---|---|---|
| Type | Block | Inline |
| Groups | Sections, several elements | Words or fragments within a line |
| Meaning | None | None |
| Visual effect without CSS | Only the line break that comes with any block | None |
| Can contain | Blocks and inline elements | Only inline elements and text |
Golden rule: use div and span only when there isn't a more specific element. Before writing <span class="important">, ask yourself whether it should be a strong; before <div class="quote">, whether it should be a blockquote.
Grouping with class and id
A div or a span with no attributes isn't much use: you need to give them a name so that you can refer to them from CSS (and from JavaScript). For this, two global attributes are used, which you met in Tags, Elements and Attributes:
class |
id |
|
|---|---|---|
| Can it be repeated? | Yes: many elements can share the same class | No: each id is unique on the page |
| Several values? | Yes, separated by spaces: class="block featured" |
No: a single value |
| What for | Grouping elements of the same kind that will look the same | Identifying one specific element |
| Future use | CSS selectors (Module 8) | Anchors for linking to part of the page (Module 3), JavaScript |
- The two boxes share the class
block: later on, a single CSS rule will give them the same appearance (margins, spacing…). - The offer box also has the class
featured, which will make it possible to give it a special background. - Each one has a different
id, which identifies it uniquely.
Conventions for naming classes and id values, consistent with the ones you set for files in File Organisation and Paths:
- In lower case, with no spaces or accents, using hyphens to separate words:
old-price, notOldPriceorold price(a space would create two classes). - Names that describe what the content is, not how it looks:
noticeorfeaturedare better thanredorlarge-text. If tomorrow the notice becomes orange, the name will still be valid. classandidaren't exclusive todivandspan: you can put them on any element. Adding a class to an existingporuloften saves you from creating an unnecessarydiv.
A visual preview: seeing the boxes
Since div and span are invisible, at first it's hard to tell whether you have placed them correctly. As well as DevTools, you can temporarily use the style attribute, which applies CSS directly to an element:
<div class="block" id="offer" style="border: 2px dashed blue;">
<h2>Offer of the week</h2>
<p>Full breakfast for <span style="background: yellow;">€3.50</span>.</p>
</div>border: 2px dashed blue;draws a blue dashed border around thediv: you will see that the box takes up the full width of the window.background: yellow;colours the background of thespan: you will see that it only covers the text it wraps.
This is just a trick for visualising the block and inline model. Delete these style attributes after trying them out: the style attribute mixes presentation and content, and you will learn the correct way to apply styles (with an external stylesheet and class selectors) in Module 8, in Inline, Internal and External CSS.
The problem of "divitis"
Since div can be used for anything, it's tempting to use it for everything. This overuse is jokingly called divitis:
<!-- Divitis: everything is a div, nothing has meaning -->
<div class="title">The Blue Cup</div>
<div class="text">Welcome to your neighbourhood café and bakery.</div>
<div class="list">
<div class="item">Menu</div>
<div class="item">Bookings</div>
</div>Visually, with CSS, it might look right. But:
- A screen reader finds no headings and no lists: the page is a block of plain text with no structure.
- Search engines don't know which is the title.
- The code is longer and harder to read, with layers of nested
divelements.
<!-- Correct: each piece of content with its own element -->
<h1>The Blue Cup</h1>
<p>Welcome to your neighbourhood café and bakery.</p>
<ul>
<li>Menu</li>
<li>Bookings</li>
</ul>Symptoms of divitis:
- A
divwith the classtitle,paragraph,list,quote… when the corresponding element exists. - A
divwrapping a single element for no reason (put the class on that element). - Several levels of nested
divelements that don't group anything different.
What's more, many of the div elements you would use today to group the main parts of a page (the header, the menu, the main content, each news item, the footer) have specific semantic elements in HTML5: header, nav, main, section, article, aside and footer. You will study them in Module 6, HTML5 Semantic Elements, and they will replace a good many of the div elements in this lesson. The div will be left for what it really is: a grouping element with no meaning, for when no other element fits.
Project: organising index.html into blocks
You are going to group each part of the home page in a div with classes and identifiers, getting it ready for the styles in Module 8 and the internal links in Module 3. This is the complete index.html as it stands at the end of Module 2:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Home | The Blue Cup</title>
</head>
<body>
<!-- Welcome: home page -->
<div class="welcome">
<h1>The Blue Cup</h1>
<p>Welcome to <span class="brand">The Blue Cup</span>, your neighbourhood café and bakery.</p>
</div>
<div class="block" id="bakery">
<h2>Our bakery</h2>
<p>We roast speciality coffee and <em>every morning</em> we bake bread, croissants and sponge cakes in our own bakery.</p>
<p>We work with flour from nearby mills and long-fermented sourdough.</p>
<p class="notice"><strong>Notice:</strong> gluten, nuts and dairy products are handled in our bakery. Ask us about the allergens in each product.</p>
</div>
<div class="block featured" id="offer">
<h2>Offer of the week</h2>
<p><mark>Full breakfast</mark>: white coffee and croissant for <s class="old-price">€4.20</s> <strong class="price">€3.50</strong>.</p>
<p><small>Offer valid Monday to Friday while stocks last. Prices include <abbr title="Value Added Tax">VAT</abbr>.</small></p>
</div>
<div class="block" id="opening-hours">
<h2>Opening hours and location</h2>
<p>We are open every day from <strong>8:00 to 20:00</strong>.</p>
<p>12 Example Street<br>
Fictional Town<br>
Phone: +34 900 000 000</p>
</div>
<div class="block" id="reviews">
<h2>What our customers say</h2>
<div class="review">
<blockquote cite="https://reviews.example/the-blue-cup">
<p>The best croissant in the neighbourhood, and the coffee is always spot on. I don't have breakfast anywhere else now.</p>
</blockquote>
<p class="author">— Marta G., regular customer</p>
</div>
<div class="review">
<blockquote>
<p>I did the sourdough bread-making workshop and I'm definitely coming back. They explain everything very patiently.</p>
</blockquote>
<p class="author">— Andrew P., workshop participant</p>
</div>
<p>We are also recommended in the guide <cite>Neighbourhood Cafés 2026</cite>.</p>
</div>
<div class="block" id="contact">
<h2>Contact</h2>
<p>Email us at [email protected] and we'll get back to you as soon as possible.</p>
</div>
<!-- Temporary links to the other pages (to be improved in Modules 3 and 6) -->
<ul class="links">
<li><a href="menu.html">Menu</a></li>
<li><a href="bookings.html">Bookings</a></li>
<li><a href="gallery.html">Gallery</a></li>
<li><a href="news.html">News</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</body>
</html>Review the decisions:
- One
divper section, all with the classblock(they will look the same) and a uniqueiddescribing their content. In Special Links: Anchors, Email, Phone and Downloads, theidvalues will let you create links that jump straight to#opening-hoursor#offer. - The offer has two classes,
block featured, so that it can be visually distinguished without losing the shared style. - The welcome has its own class (
welcome), because it will probably have a different design (a hero area). - Each review is grouped with its attribution in a
div class="review", so that the quotation and the author form a unit that can be styled as a card. (In Module 6 you will see thatfigureandfigcaptionexpress this with meaning.) - The name of the café in the text is wrapped in
span class="brand": no semantic element describes a "brand name". - No
divis created around the list of links: addingclass="links"to theulitself is enough. This avoids divitis. - The prices and the notice are given classes (
price,old-price,notice) on their existing elements, without adding anyspan.
The block outline of the body now looks like this:
graph TD
BODY["body"] --> D1["div.welcome"]
BODY --> D2["div.block#bakery"]
BODY --> D3["div.block.featured#offer"]
BODY --> D4["div.block#opening-hours"]
BODY --> D5["div.block#reviews"]
BODY --> D6["div.block#contact"]
BODY --> UL["ul.links"]
D5 --> R1["div.review"]
D5 --> R2["div.review"]
(The notation div.block#opening-hours means "a div with the class block and the id opening-hours"; it's the same notation you will use in CSS selectors.)
Save and open the page: it will look exactly the same as before. That's what you should expect. To check that the boxes are right, temporarily add style="border: 2px dashed blue;" to a couple of div elements, look at the borders and then remove them. Or use DevTools: when you hover over each div in the Elements panel, you will see the whole section highlighted.
Common Mistakes and Tips
- Using
divorspanwhen a semantic element exists: a heading is a heading element, important text isstrong, a list isul/ol. - Putting blocks inside inline elements (
<span><p>…</p></span>) or insidep: the browser will restructure the DOM and the validator will flag it as an error. - Repeating an
idon the same page: it must be unique. If several elements share a style, use a class. - Writing classes with spaces thinking it's a single name:
class="old price"is two classes,oldandprice. - Class names based on appearance (
blue,big): they become outdated as soon as the design changes. - Wrapping a single element in a
divjust to give it a class: put the class directly on the element. - Leaving test
styleattributes in the final code: remove them; the styles will go incss/styles.css. - Tip: before typing
<divor<span, run through the elements you already know in your head. If one of them describes the content, use it;divandspanare the last resort.
Exercises
Exercise 1. Classify each element as block or inline and say whether the nesting is correct or not; correct the incorrect ones:
<li><p>Butter croissant</p></li><em><ul><li>Coffee</li></ul></em><h2>Opening hours <span class="note">(including public holidays)</span></h2><p>Ingredients: <div>flour, water, salt</div></p><blockquote><p>Highly recommended.</p></blockquote>
Exercise 2. This fragment suffers from divitis. Rewrite it with the appropriate elements, keeping classes only where they add something:
<div class="page">
<div class="main-title">Our menu</div>
<div class="text-container">
<div class="text">All our coffees are roasted in house.</div>
</div>
<div class="coffee-list">
<div>Espresso</div>
<div>Cortado</div>
</div>
<div class="important">Ask about our lactose-free milk.</div>
</div>Exercise 3. In menu.html, group each section (the h2 and its content) in a div with the class menu-section and a descriptive id (specialities, coffees, brewing, glossary). Also, wrap the text "roasted in house" in the first paragraph in a span with a suitable class so that it can be highlighted in the future. Should you also wrap the glossary's dl in its own div?
Solutions
Solution 1.
li(a block that accepts blocks) withp(a block): correct.em(inline) withul(a block) inside: incorrect. Correction:<ul><li><em>Coffee</em></li></ul>.h2(a block that only accepts inline content) withspan(inline): correct.pwith adivinside: incorrect,ponly accepts inline content. Correction:<p>Ingredients: <span>flour, water, salt</span></p>(or, better still, a list of ingredients after the paragraph).blockquote(accepts blocks) withp: correct.
Solution 2.
<h1>Our menu</h1>
<p>All our coffees are roasted in house.</p>
<ul class="coffee-list">
<li>Espresso</li>
<li>Cortado</li>
</ul>
<p><strong>Ask about our lactose-free milk.</strong></p>- The title is an
h1, the text apand the list aulwithlielements. - "Important" is expressed with
strong(if a special style is needed,class="notice"can be added to thep). - The
pageandtext-containerdivelements didn't group anything useful and are removed. (In Module 6, the main content of the page will go inmain.)
Solution 3.
<h1>Our menu</h1>
<p>Speciality coffee <span class="highlight">roasted in house</span> and pastries from our bakery.</p>
<p><small>Prices include <abbr title="Value Added Tax">VAT</abbr>.</small></p>
<div class="menu-section" id="specialities">
<h2>House specialities</h2>
<ul>
<!-- … the same li elements as before … -->
</ul>
</div>
<div class="menu-section" id="coffees">
<h2>Coffees</h2>
<!-- … nested list of coffees … -->
</div>
<div class="menu-section" id="brewing">
<h2>How we make our filter coffee</h2>
<!-- … ol with the steps … -->
</div>
<div class="menu-section" id="glossary">
<h2>Coffee glossary</h2>
<!-- … dl with the terms … -->
</div>There's no need to wrap the dl in another div: it's already inside div#glossary, and if you need to style it you can give it a class directly (<dl class="glossary">). Adding another div would be divitis. The span is appropriate because "roasted in house" isn't more important and doesn't carry emphasis: we just want to be able to highlight it visually.
Conclusion
You now understand HTML's flow model: block elements start on a new line and take up the full width, inline elements fit into the text, and from this come the nesting rules (no blocks inside inline elements, nor inside p or headings). You know div and span, the generic containers with no meaning, and you know how to name them with class (reusable) and id (unique) with the CSS of Module 8 in mind, without falling into divitis and remembering that the semantic elements of Module 6 will replace many of them. This brings the Text Formatting module to a close: The Blue Cup has a home page structured into sections with offers, notices and reviews, a menu with lists and a glossary, and a news page with quotations and a recipe. But the pages are still poorly connected to one another, and they have no images. In the next module, Links and Media in HTML, you will start with the lesson Creating Hyperlinks to join up all the pages on the site and then bring them to life with images, video and audio.
HTML Course
Module 1: Introduction to HTML
- What Is HTML?
- Setting Up Your Work Environment
- Basic Structure of an HTML Document
- Tags, Elements and Attributes
- Creating Your First HTML Page
- File Organisation and Paths
Module 2: Text Formatting in HTML
- Headings and Paragraphs
- Text Formatting Tags
- Lists: Ordered, Unordered and Description Lists
- Block Quotes and Preformatted Text
- Block and Inline Elements: div and span
Module 3: Links and Media in HTML
- Creating Hyperlinks
- Special Links: Anchors, Email, Phone and Downloads
- Adding Images
- Embedding Videos
- Using the Audio Element
Module 4: Tables in HTML
- Basic Table Structure
- Table Head, Body and Foot
- Merging Cells: colspan and rowspan
- Accessible Tables: caption, scope and headers
- Styling Tables
Module 5: Forms in HTML
- Creating a Basic Form
- Form Elements: input, textarea and select
- HTML5 Input Types
- Labels and Grouping: label, fieldset, legend and datalist
- Form Attributes and Validation
- Submitting Forms
Module 6: HTML5 Semantic Elements
- Introduction to Semantic HTML
- header, nav and footer
- article, section and aside
- main and figure
- Other Semantic Elements: time, address, details and dialog
Module 7: Advanced HTML Techniques
- HTML Entities
- Using iframes
- Data Attributes
- Metadata: meta, SEO and Open Graph
- Microdata and Structured Data
- Graphics in HTML: SVG and canvas
Module 8: Integrating HTML and CSS
- Linking CSS to HTML
- Inline, Internal and External CSS
- Basic CSS Selectors
- The Box Model
- Styling HTML Elements
- Introduction to Layout with Flexbox and Grid
Module 9: Responsive Web Design
Module 10: Best Practices and Accessibility
- HTML Best Practices
- Validating HTML Code
- Web Accessibility Basics
- Using ARIA Roles and Attributes
- Accessibility Testing
- Performance and Resource Loading
