Web Development Pro | Lesson 012 | The Box Model and Reliable Sizing
Web Development Pro · Lesson 12 of 60
Module 2: CSS and responsive design
In this lesson
Prerequisites and learning outcomes
Complete lesson 11 first. This lesson builds on content, padding, border, margin, and units. By the end, you can measure a component and prevent common overflow with border-box and relative sizing. Plan 10–15 minutes for the core explanation and additional time for the worked practice.
Every visible element occupies a box
A workshop card seems to be 300 pixels wide, yet two cards overflow a 600-pixel container. The missing detail is often the box model. A box has content, padding, border, and margin. Width does not always include all those layers. Understanding which measurement you set is more reliable than repeatedly reducing numbers until the page looks acceptable.
Content is the area used by text and other contents. Padding creates space inside the border. Border surrounds the padding and content. Margin is outside the border and separates the element from neighbors. Backgrounds normally paint through the padding area, while margins are transparent. Each layer has a different purpose; do not use empty paragraphs as substitutes for spacing.
Calculate before guessing
With the default content-box sizing, width: 300px sets the content width. Add 20px of padding on each side and a 2px border on each side, and the border-box width becomes 344px. Margins add further outside space when relevant. This simple calculation explains why “two 300px cards” may not fit in a 600px row.
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
main {
width: min(100% - 2rem, 60rem);
margin-inline: auto;
}
.card {
width: 100%;
padding: 1.25rem;
border: 2px solid #28536b;
margin-block: 1rem;
}
img { max-width: 100%; height: auto; }
With border-box, the specified width includes padding and border. A 300px border-box card with the same padding and border has 256px left for content. Margin remains outside; border-box does not absorb it. The universal rule includes pseudo-elements so generated decorative boxes follow the same sizing convention.
Choose units by relationship
A CSS pixel is a reference unit, not necessarily one physical display pixel. Rem is relative to the root element's font size. Em is relative to the element's font size for many properties, while font-size expressed in em is relative to the parent's font size. Percentages depend on the property's definition and its containing context; they are not all percentages of the viewport.
Use rem for many text and spacing relationships because it can follow the user's root text size. Use percentages and flexible layout for available space. Fixed pixels can be appropriate for details such as borders, but fixed heights on text containers are risky. A translated heading or larger font can overflow a height that only fitted your original sentence.
The main width expression chooses the smaller of the available width minus two rem and sixty rem. This creates outer breathing room on narrow screens and a maximum reading area on wide screens. Margin-inline: auto centers the block in the inline direction. Logical properties such as margin-inline describe writing directions more directly than hardcoding left and right in every rule.
Recognize common overflow causes
An image can overflow if its intrinsic width exceeds the container and no responsive rule constrains it. A long unbroken URL can also force a box wider than expected. Later flex and grid lessons address automatic minimum sizes; for text, overflow-wrap: anywhere can be useful where arbitrary long strings are possible. Apply it thoughtfully rather than hiding the overflow and losing information.
Avoid using overflow: hidden as a general cure for unexpected horizontal scrolling. It can conceal content, focus indicators, or menus without fixing the underlying sizing error. Inspect the oversized element and determine whether its width, padding, minimum size, or unbreakable content is responsible. A good fix preserves the needed content.
Vertical margins between ordinary block elements can collapse in certain situations, so two neighboring margins are not always simply added. Padding and flex/grid gaps follow different rules. You do not need every margin-collapse edge case yet, but recognize it when a measured vertical gap differs from your arithmetic. Use layout tools deliberately rather than treating all spacing mechanisms as interchangeable.
Inspect the box you actually rendered
Browser developer tools usually display a box-model diagram for a selected element. Compare content size, padding, border, and margin with your prediction. Check the computed box-sizing value as well: a reset might not have loaded, or a later rule could override it. The layout is the result of all applicable rules, not only the declaration you last edited.
The local tests verify the arithmetic examples and inspect the supplied rules, but they cannot measure rendered boxes in this restricted environment. Manual verification should include a narrow viewport and enlarged text. Keep a record of expected versus observed dimensions so you can explain a failure instead of only noting that it “looks off.”
Worked practice
Worked exercise 1: Compute a content-box width
Task: A content-box element has width 240px, 12px padding per side, and 1px borders. Worked answer: the border-box width is 240 + 24 + 2 = 266px. If each horizontal margin is 8px, the total outside space considered in a simple row becomes 282px.
Worked exercise 2: Compute a border-box content width
Task: The same element uses border-box with width 240px. Worked answer: its content width is 240 − 24 − 2 = 214px. The outside margins remain separate. Border-box changes what width includes; it does not remove the padding or border.
Worked exercise 3: Fix a clipped paragraph
Task: A card has height: 120px and clips text after zooming. Worked answer: remove the fixed height and let content determine the block size, or use an appropriate minimum height if a design needs a baseline. Retest with longer text rather than shrinking the font to hide the problem.
Knowledge check
Question 1
Under content-box, what does width normally measure? A) Content only. B) Content plus margin. C) Border plus margin. D) Padding only.
Check question 1: answer and explanation
Answer: A. Padding and border are added outside the specified content width.
Question 2
Does border-box include margin in width? A) Always. B) Only for percentages. C) No. D) Only for rem.
Check question 2: answer and explanation
Answer: C. Margin remains outside the border box regardless of this sizing choice.
Question 3
What is rem based on? A) The viewport width. B) The root font size. C) The immediate parent’s font size for every property. D) The element’s own font size for every property.
Check question 3: answer and explanation
Answer: B. Root-relative sizing differs from the contextual relationships used by em and percentages.
Question 4
Why is a fixed text-container height risky? A) It prevents the user from enlarging the text. B) It forces every paragraph onto a single line. C) It guarantees the same line count across different fonts. D) Larger or longer content may not fit.
Check question 4: answer and explanation
Answer: D. Text expansion is normal and should be accommodated.
Question 5
What should precede hiding horizontal overflow? A) Identifying the element and sizing cause. B) Removing all margins globally. C) Reducing every font. D) Deleting the widest content.
Check question 5: answer and explanation
Answer: A. Diagnosis preserves information and leads to a targeted fix.