28. Fluid layouts
Module 6 · About 20 minutes · Review the earlier HTML, CSS and DOM foundations relevant to this task.
Open Interactive LessonDiscover
How can fluid layouts solve a concrete interface problem, and what actual evidence proves the result?
- Explain the important structure, presentation or state relationship in fluid layouts
- The preferred viewport-based size is constrained by minimum and maximum rem values.
- Predict a changed input, repair the supplied fault and verify a declared outcome
Predict
Record the expected structure or visual change before rendering. Name one thing that should remain unchanged.
Understand
rem follows the root font size, em depends on the relevant element context, percentages depend on the property and containing block, and viewport units follow a viewport dimension. clamp sets a minimum, preferred value and maximum. A fluid formula should retain sensible limits and work with user text scaling. Do not use viewport sizing alone to make body text unreadably small.
Important source lines
- index.html:1 <h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p>
- The highest-level heading for a page topic.
- styles.css:1 .fluid { font-size:clamp(2rem, 5vw, 4rem); line-height:1.1; }
- Sets the text size, subject to zoom and inherited context. Context: block
All five examples belong to this lesson’s operation. The broken case is explicitly labeled and cannot earn repaired-work credit. The source regression checks DOM contracts; CSS source, browser geometry and complete accessibility need their separately stated evidence.
Example 1: Working fluid layouts
The preferred viewport-based size is constrained by minimum and maximum rem values.
index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p>
styles.css
.fluid { font-size:clamp(2rem, 5vw, 4rem); line-height:1.1; }script.js
Expected: The preferred viewport-based size is constrained by minimum and maximum rem values.
Edit and Render This ExampleExample 2: Long-content and literal-text boundary
Keep the same core operation with an additional realistic long-text record. Inspect wrapping, preserved controls and safe literal characters. This is a content-stress case; actual layout and meaning require visual review.
index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p> <section aria-label="Additional content case"><p id="content-boundary">Lesson 28: A long account or course title must remain readable with spaces and literal <angle brackets>. A long account or course title must remain readable with spaces and literal <angle brackets>. </p></section>
styles.css
.fluid { font-size:clamp(2rem, 5vw, 4rem); line-height:1.1; }
#content-boundary { overflow-wrap:anywhere; max-width:60ch; }script.js
Expected: The preferred viewport-based size is constrained by minimum and maximum rem values.
Edit and Render This ExampleExample 3: Observe actual DOM mutations
Install a real MutationObserver before the same operation. Its live counter records actual child-list, attribute and character-data changes; it is not a browser-internal paint profiler.
index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p> <p>Actual observed mutation records: <output id="mutation-count">0</output></p>
styles.css
.fluid { font-size:clamp(2rem, 5vw, 4rem); line-height:1.1; }script.js
const mutationOutput = document.querySelector("#mutation-count");
let mutationCount = 0;
const mutationObserver = new MutationObserver(records => { const relevant = records.filter(record => record.target !== mutationOutput && !mutationOutput.contains(record.target)); if (!relevant.length) return; mutationCount += relevant.length; mutationOutput.textContent = String(mutationCount); });
mutationObserver.observe(document.body, {subtree:true, childList:true, attributes:true, characterData:true});
Expected: The preferred viewport-based size is constrained by minimum and maximum rem values.
Edit and Render This ExampleExample 4: Predict and inspect the intentional fault
This source deliberately replaces clamp(2rem, 5vw, 4rem) with 5vw. Reproduce the mismatch, identify its cause and compare the corrected source.
index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p>
styles.css
.fluid { font-size:5vw; line-height:1.1; }script.js
Expected: The preferred viewport-based size is constrained by minimum and maximum rem values.
Edit and Render This ExampleExample 5: Executable regression and independent expectations
Use the actual authored initial checks and fresh interaction assertions printed below. The in-page verifier performs real DOM operations and rejects mismatches; it does not infer correctness from source keywords.
index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p> <button id="run-regression" type="button">Run declared regression</button><pre id="regression-result">Not tested</pre>
styles.css
.fluid { font-size:clamp(2rem, 5vw, 4rem); line-height:1.1; }script.js
document.querySelector("#run-regression").addEventListener("click", async () => {
const spec = {"rules": [{"type": "rule", "selector": ".fluid", "property": "font-size", "value": "clamp(2rem, 5vw, 4rem)", "label": ".fluid declares font-size: clamp(2rem, 5vw, 4rem)"}], "case": null};
const failures = [];
const choose = selector => document.querySelector(selector);
for (const check of spec.rules) {
const element = choose(check.selector);
if (check.type === "exists" && !element) failures.push(check.label);
if (check.type === "count" && document.querySelectorAll(check.selector).length < check.value) failures.push(check.label);
if (check.type === "text" && !element?.textContent.includes(check.value)) failures.push(check.label);
if (check.type === "attr" && element?.getAttribute(check.property) !== check.value) failures.push(check.label);
if (check.type === "label" && !element?.labels?.length) failures.push(check.label);
}
if (spec.case) {
for (const action of spec.case.actions) {
const element = choose(action.selector);
if (!element) { failures.push("Missing action target " + action.selector); continue; }
if (action.type === "click") element.click();
if (action.type === "input" || action.type === "change") { element.value = action.value; element.dispatchEvent(new Event(action.type, {bubbles:true})); }
if (action.type === "key") element.dispatchEvent(new KeyboardEvent("keydown", {key:action.key,bubbles:true,cancelable:true}));
if (action.type === "submit") element.dispatchEvent(new Event("submit", {bubbles:true,cancelable:true}));
}
await new Promise(resolve => setTimeout(resolve, 60));
for (const check of spec.case.assertions) {
const element = choose(check.selector);
const actual = check.type === "count" ? document.querySelectorAll(check.selector).length : check.type === "attribute" ? element?.getAttribute(check.property) : check.type === "property" ? check.property === "focused" ? document.activeElement === element : element?.[check.property] : element?.textContent;
const pass = check.exact === false ? String(actual).includes(check.value) : actual === check.value;
if (!pass) failures.push(check.selector + ": expected " + JSON.stringify(check.value) + ", actual " + JSON.stringify(actual));
}
}
document.querySelector("#regression-result").textContent = failures.length ? "FAIL: " + failures.join("; ") : "PASS: declared DOM contract; review CSS/layout separately";
});Expected: The preferred viewport-based size is constrained by minimum and maximum rem values.
Edit and Render This ExampleBuild and test
The preferred viewport-based size is constrained by minimum and maximum rem values. Repair the prepared fault while preserving unrelated content. For an interactive lesson run the selected fresh interaction case, not only initial-structure checks.
Starter index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p>
Starter styles.css
.fluid { font-size:5vw; line-height:1.1; }Objective contract
[
{
"type": "rule",
"selector": ".fluid",
"property": "font-size",
"value": "clamp(2rem, 5vw, 4rem)",
"label": ".fluid declares font-size: clamp(2rem, 5vw, 4rem)"
}
]Hint 1
Compare the exact expected result with the first actual mismatch, using the parsed DOM and the selected event case.
Hint 2
Find 5vw in the exercise and compare its role with clamp(2rem, 5vw, 4rem).
Hint 3
Restore clamp(2rem, 5vw, 4rem) or an equivalent correct implementation. Run the fresh case again and explain the changed relationship.
Complete reference solution
index.html
<h1 class="fluid">A heading that adapts</h1><p>Resize the preview and inspect its computed font size.</p>
styles.css
.fluid { font-size:clamp(2rem, 5vw, 4rem); line-height:1.1; }Explain, reflect and extend
Which actual node, state transition or computed value proves this result? Name an independent input and expected output, then explain one remaining manual or compatibility check.
Apply the same relationship to a new realistic content case. Predict the normal, boundary and invalid outcomes first. Preserve keyboard access and narrow-screen usability, then have another learner reproduce the result.
Troubleshooting and professional practice
Confirm the active file, enable JavaScript and run a fresh preview. A changed source is not installed until the preview reruns. Use the DOM tree to locate missing targets, Console for actual errors, and the selected interaction test for exact behavior. CSS geometry requires a real browser. DOM and stylesheet assertions are focused checks. Real layout, geometry, native interactions, zoom and assistive technology require supporting-browser review.
Knowledge check
Explain why the stated result follows from the source. Predict a second input or width and test it independently. Automated contracts and written explanations are reviewed separately.
Optional AI prompts
- Explain this lesson’s source line by line using the actual objective above.
- Help implement one small extension and define normal, boundary and failure tests.
- Review/debug the source against actual preview observations; verify standards and never claim rendering without evidence.
Continue to Lesson 29: Responsive typography
Detailed learning and editor help · Standards reference
Definitions and use case
Use this technique in a reusable, responsive interface whose labels, state and behavior stay consistent. The preferred viewport-based size is constrained by minimum and maximum rem values.
- Fluid layouts
- rem follows the root font size, em depends on the relevant element context, percentages depend on the property and containing block, and viewport units follow a viewport dimension.
- DOM contract
- A fixed expected structure, attribute, property or event result under declared inputs.
- Browser observation
- An actual computed style, geometry reading or supported callback from the controlled preview; a DOM-only test does not reproduce layout.
Interaction experiments and exact contracts
Run the current source in a fresh preview. Choose an interaction case, predict, then Check My Work. These automated actions are synthetic events; repeat important tasks with a real keyboard and pointer.
Example 1: source explanation and interaction checks
index.html:1 The highest-level heading for a page topic.
styles.css:1 Sets the text size, subject to zoom and inherited context. Context: block
Example 2: source explanation and interaction checks
index.html:1 The highest-level heading for a page topic.
index.html:2 A thematic grouping, usually identified by a heading.
styles.css:1 Sets the text size, subject to zoom and inherited context. Context: block
styles.css:2 Permits long unbroken text to wrap to avoid overflow. Context: constrained scrolling box
Example 3: source explanation and interaction checks
index.html:1 The highest-level heading for a page topic.
index.html:2 A paragraph of prose.
styles.css:1 Sets the text size, subject to zoom and inherited context. Context: block
script.js:1 Resolve the first matching live DOM element. A missing match is null and requires handling.
script.js:2 Declare mutable local state; update it before projecting the state into the DOM.
script.js:3 Read or replace actual text without parsing user data as HTML. Select only the intended text container.
script.js:4 Detect and use the browser API only where available; actual geometry is not fabricated offline.
Example 4: source explanation and interaction checks
index.html:1 The highest-level heading for a page topic.
styles.css:1 Sets the text size, subject to zoom and inherited context. Context: block
Example 5: source explanation and interaction checks
index.html:1 The highest-level heading for a page topic.
index.html:2 A native action control with keyboard behavior.
styles.css:1 Sets the text size, subject to zoom and inherited context. Context: block
script.js:2 Register a real event callback. The function runs after the named event; registration alone does not perform the action.
script.js:3 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:4 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:5 Resolve the first matching live DOM element. A missing match is null and requires handling.
script.js:6 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:7 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:8 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:9 Resolve the first matching live DOM element. A missing match is null and requires handling.
script.js:10 Read or replace actual text without parsing user data as HTML. Select only the intended text container.
script.js:11 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:12 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:13 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:14 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:15 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:16 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:17 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:18 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:19 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:20 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:21 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:22 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:23 Queue asynchronous work after a minimum delay; no exact timing is promised.
script.js:24 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:25 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:26 Read or replace actual text without parsing user data as HTML. Select only the intended text container.
script.js:27 Bind the named value for the following operations; const does not freeze referenced objects.
script.js:28 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:29 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:30 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
script.js:31 Read or replace actual text without parsing user data as HTML. Select only the intended text container.
script.js:32 Follow this control flow using the declared state and event. Compare actual DOM results against the fixed expected contract.
Run the executable regression on a fresh page
{
"title": "Run the executable regression on a fresh page",
"actions": [
{
"type": "click",
"selector": "#run-regression"
}
],
"assertions": [
{
"type": "text",
"selector": "#regression-result",
"value": "PASS: declared DOM contract",
"exact": false
}
]
}Common mistakes and official references
- Changing a nearby visual style while leaving the actual semantic or event defect intact.
- Comparing a changed state with an initial-state expected result without resetting.
- Treating DOM-only checks as complete accessibility, geometry or production performance certification.