SCS 209 • UNIT 3 • Computing
Block-Level And Inline Containers
Topics covered: div as a block-level container for content, Uses of div for page headers and footers, Uses of div for columns of content and sidebars, Uses of div for highlighted boxes within text flow, Uses of div for specific page areas such as ad spots, Uses of div for image galleries, span as an inline container for content, Block-level elements starting on a new line, Inline elements not creating a new line, Block-level elements containing inline elements, Inline elements not containing block-level elements, class and id attributes on div and span for CSS styling and positioning, Combining essential HTML tags in a single web page example
Syllabus Study Notes (Core Overview)
Free Overview Preview1. Core Concept Overview & Intuition
The Structural Problem in Web Documents
An HTML document is not merely a sequence of words and pictures; it is a hierarchy of containers. Every visible piece of content must be placed inside some element, and the choice of container determines how the browser lays that content out on the page. Two containers are fundamental to this layout process: the div element and the span element. The div element is a block-level container: it occupies the full available width of its parent and forces a line break before and after itself. The span element is an inline container: it occupies only as much width as its content requires and flows within the surrounding text without breaking the line.
The distinction is not cosmetic. It governs the box model of the document: block-level boxes stack vertically, while inline boxes run horizontally along the text baseline. A div is therefore the natural tool for carving a page into large regions — headers, footers, columns, sidebars, advertising slots, and image galleries — whereas a span is the natural tool for marking up a fragment of text inside a paragraph so that a particular word or phrase can be styled independently.
Why Containers Matter for Layout
Before CSS-based layout became standard, page regions were often built with tables. The modern approach uses div elements as generic structural containers and then applies CSS rules to position and style them. The source material states this principle directly: by adding class and/or id attributes to div elements, one can use CSS to style and position the divs, and this is the basis for creating CSS-based page layouts. The div itself carries no inherent visual meaning; it is a semantic blank slate whose appearance is entirely determined by the CSS attached to its class or id.
The same logic applies to span, except that the styling is applied to an inline fragment rather than a block region. A span can be given a class so that, for example, a product name inside a sentence is rendered in a distinct colour or weight without disturbing the flow of the paragraph.
The Nesting Hierarchy
The relationship between block-level and inline elements is governed by a containment rule that mirrors the structure of written language. A block-level element may contain inline elements, because a paragraph (a block) naturally contains words and phrases (inline content). An inline element may not contain a block-level element, because a phrase cannot logically enclose a paragraph. This asymmetry is the single most important structural constraint in the topic, and it explains why div is used for page regions while span is used for text fragments.
The intuition is that of a document outline: sections contain paragraphs, paragraphs contain sentences, and sentences contain words. A div corresponds to a section; a span corresponds to a marked word or phrase. Just as a word cannot contain a section, a span cannot contain a div.
The Role of Class and ID Attributes
Both div and span accept two identifying attributes. The class attribute assigns an element to one or more named categories, allowing a single CSS rule to style many elements at once; the same class may be reused across the document. The id attribute assigns a unique identifier to a single element, allowing a CSS rule (or a positioning rule) to target that one element specifically. In practice, class is used for repeated structural roles such as "column", "sidebar", or "highlight", while id is used for unique page regions such as the page header or a specific advertising slot.
2. Mathematical Formalization & Core Definitions
> Definition (Block-Level Element). A block-level element is an HTML element whose generated box occupies the full width of its containing block and which begins on a new line, forcing any following content onto a subsequent line. The div element is the generic block-level container.
> Definition (Inline Element). An inline element is an HTML element whose generated box occupies only the width required by its content and which does not begin on a new line; it flows within the surrounding inline content. The span element is the generic inline container.
> Definition (Container). A container is an element whose purpose is to enclose other content — text, other elements, or both — so that the enclosed content can be treated as a single structural unit for styling and positioning.
> Definition (Class Attribute). The class attribute assigns one or more category names to an element. Multiple elements may share the same class value, so a single CSS rule can apply to all of them.
> Definition (ID Attribute). The id attribute assigns a unique identifier to an element. Within a single document, no two elements may share the same id value, so a CSS rule targeting that identifier applies to exactly one element.
> Definition (Nesting). Nesting is the placement of one element entirely inside another. The outer element is the parent; the inner element is the child. Nesting defines the containment hierarchy of the document.
> Rule (Block-Contains-Inline). A block-level element may contain inline elements. A div may therefore enclose span elements, text, and other inline content.
> Rule (Inline-Cannot-Contain-Block). An inline element may not contain a block-level element. A span may therefore not enclose a div.
The containment rules can be summarised as a permitted-pair relation between the two element types:
| Parent type | Child type | Permitted |
|---|---|---|
Block (div) |
Block (div) |
Yes |
Block (div) |
Inline (span) |
Yes |
Inline (span) |
Inline (span) |
Yes |
Inline (span) |
Block (div) |
No |
The table encodes the asymmetry that defines the topic: block containers are permissive, inline containers are restrictive. The restriction exists because an inline box is laid out along the text baseline, and a block box would demand a full-width vertical region that the inline formatting context cannot supply.
Unlock Complete Study Notes & Exam Traps
The full guide includes formal proofs, worked exemplar calculations, high-yield exam traps, and examiner marking schemes. Continue studying this topic in Mentify Prep Studio.