MGU Computer Science Series — Web Development: HTML, CSS, and JavaScript
Part III: CSS — Style and Layout
Content · Padding · Border · Margin · Box-Sizing
Chapter 14 of 30
14
MGU Computer Science Series  ·  Web Development: HTML, CSS, and JavaScript  ·  Library Textbook Edition

Chapter 14:
The Box Model

How content, padding, borders, and margins work together to define the size and position of every element on a web page

■ Part III of VII ■

■ 10 Sections  •  Chapter 14 of 30 ■

Builds directly on Chapters 11–13 — selectors, properties, and CSS rules

Chapter Introduction and Learning Objectives

Every visible element on a web page occupies space in a predictable, structured way. CSS describes that space using a model called the box model. This chapter explains how content, padding, borders, and margins work together to define an element's size and position. Students who understand the box model can predict layout behavior, debug spacing problems, and control page design with precision rather than guesswork.

Open any web page, open developer tools, hover over an element, and the browser highlights it with colored overlays — a region in the center surrounded by three more bands moving outward. Those four bands are the box model, and they apply to every element on every web page you will ever build. This chapter takes that idea apart layer by layer, then shows how to size, debug, and inspect boxes with confidence.

Opening Question

A card on a web page is declared width: 300px, but the browser renders it at 352 pixels wide. A button's text looks cramped against its edges. Two headings that should sit 40 pixels apart on the page are actually only 24 pixels apart. What single idea explains all three of these surprises — and how can a developer prevent them before they happen? By the end of this chapter, you will be able to answer that question precisely, and you will know exactly where to look in your browser to confirm it.

10Sections
7Objectives
16Key Terms
30Total Chapters

Learning Objectives

By the end of this chapter, you should be able to:

  1. 1Explain why every HTML element behaves like a box in the browser, and name the four nested regions of the box model.
  2. 2Explain how content area, padding, border, and margin each contribute to an element's total size.
  3. 3Explain how width and height interact with the box model, including percentage, max-width, and min-width values.
  4. 4Explain why box-sizing: border-box simplifies layout calculations, and apply it using the universal selector reset.
  5. 5Explain how display: block, display: inline, and display: inline-block change how elements occupy space.
  6. 6Use the overflow property to control content that exceeds its container.
  7. 7Debug layout problems using the browser's box model inspector, including margin collapsing and unexpected default spacing.

Sections at a Glance

Sections 14.1 – 14.5
The Four Layers: Content, Padding, Border, and Margin
The Box Model · Content Area · Padding · Borders · Margins

Every Element Is a Box

Open any web page, open developer tools, hover over an element, and the browser highlights it with colored overlays: a region in the center, surrounded by three more bands moving outward. Those colors represent the four layers of the CSS box model. Every element on every web page is rendered using this same structure.

The CSS box model describes how much space an element takes up and where its edges are. It is not a design choice you make sometimes. It applies to every single element — paragraphs, headings, images, buttons, navigation links, divs, spans, and everything else. Understanding the box model means understanding how the browser builds a page.

Here is the fundamental idea: each element is composed of four nested regions.

+------------------------------------------+ | MARGIN | | +------------------------------------+ | | | BORDER | | | | +------------------------------+ | | | | | PADDING | | | | | | +--------------------------+| | | | | | | CONTENT AREA || | | | | | +--------------------------+| | | | | +------------------------------+ | | | +------------------------------------+ | +------------------------------------------+

Working from the inside out:

Layer 1 — Innermost
Content Area
Where the text, image, or other content appears. This is what width and height measure by default.
Layer 2
Padding
Transparent space between the content and the border. Padding is inside the element and shares the element's background color.
Layer 3
Border
A visible or invisible edge that wraps the padding and content. It has its own width, style, and color.
Layer 4 — Outermost
Margin
Transparent space outside the border that separates this element from neighboring elements. Margin is always transparent.

These four layers are the box model. Every layout calculation in CSS — every width, every gap between elements, every overflowing card — depends on them. The rest of this chapter examines each layer in turn, then shows how they combine to determine an element's true size.

The Content Area

The content area holds the actual content of an element: text, an image, a video, or child elements. When you set width and height on an element using the default box-sizing value (explained fully in Section 14.7), those measurements apply to the content area only — they do not include padding, border, or margin.

p { width: 400px; height: 100px; }

With the default box model, this paragraph's content area is 400 pixels wide and 100 pixels tall. If you later add padding or a border, the total visible size of the element will become larger than 400 by 100 pixels.

This is a common source of confusion for beginners. A box that is declared width: 400px does not necessarily occupy 400 pixels of page space. It occupies 400 pixels of content space, plus whatever padding and border are added.

Content Area and Background Color

The background color of an element fills both the content area and the padding area. It does not fill the margin.

div { background-color: lightblue; padding: 20px; margin: 30px; }

In this example, the light blue background color covers the content and the 20-pixel padding around it. The 30-pixel margin remains transparent, showing whatever is behind the element — usually the page background.

When Content Area Size Is Not Set

Study Note

If you do not set a width, block elements expand to fill their parent container. If you do not set a height, block elements grow to contain their content. This is normal and usually desirable behavior. You do not need to set explicit dimensions on every element.

Padding

Padding is the space between an element's content and its border. It is inside the element. It shares the element's background color. It creates breathing room between text and the element's edges.

p { background-color: lightyellow; padding: 20px; }

This paragraph has a light yellow background that extends 20 pixels beyond the text in every direction — top, right, bottom, and left.

Padding Shorthand

CSS padding can be written as a shorthand that applies to all four sides, or you can target each side independently.

/* All four sides */ padding: 20px; /* Top and bottom, then left and right */ padding: 10px 20px; /* Top, then left and right, then bottom */ padding: 10px 20px 15px; /* Top, right, bottom, left (clockwise) */ padding: 10px 20px 15px 5px;

You can also write each side as a separate property:

padding-top: 10px; padding-right: 20px; padding-bottom: 10px; padding-left: 20px;

Separate properties are useful when you need to override one side without affecting the others.

Padding and Total Element Size

A Key Insight

Because padding is inside the element under the default box-sizing value, adding padding increases the total space the element occupies.

div { width: 200px; padding: 20px; }

The content area is 200 pixels wide, but the element's total rendered width is 200 + 20 + 20 = 240 pixels. This is why layout calculations can become surprising. Section 14.7 introduces box-sizing: border-box, which changes this behavior in a way most developers prefer.

Using Padding Well

Padding is most commonly used to:

  • Add space inside buttons so the text is not crammed against the edges
  • Give text containers breathing room so text is not flush with a background color or border
  • Create visual size for small interactive elements like links and menu items
  • Separate content from a card's edges in a card layout

Borders

The border is a line drawn around the padding and content. It can be visible or invisible. It has three properties: width, style, and color.

div { border-width: 2px; border-style: solid; border-color: navy; }

These three properties can also be written in one shorthand declaration:

div { border: 2px solid navy; }

The shorthand order is: width, style, color. The style value is required — without it, the border will not appear even if width and color are set.

Border Style Values

border-style: solid; /* A continuous line */ border-style: dashed; /* A dashed line */ border-style: dotted; /* A dotted line */ border-style: double; /* Two lines */ border-style: none; /* No border (default) */

The most commonly used styles in production web development are solid and none.

Targeting Individual Sides

Like padding, borders can target individual sides:

/* Shorthand for a single side */ border-top: 3px solid orange; border-right: 1px dashed gray; border-bottom: 2px solid black; border-left: none; /* Separate properties */ border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: black;

Individual side targeting is useful for design patterns like bottom-only borders on headings, left-border accent lines on blockquotes, or removing borders on specific sides of navigation items.

Border Radius

border-radius rounds the corners of an element's border box.

button { border: 2px solid navy; border-radius: 6px; /* Slightly rounded corners */ } .circle { width: 100px; height: 100px; border-radius: 50%; /* Fully circular */ }

A border-radius of 50% on an element with equal width and height creates a circle. This is how profile images and icon buttons are styled as circles.

Borders and Total Element Size

Running the Numbers

Like padding, border adds to the total rendered size of an element under the default box-sizing setting.

div { width: 200px; padding: 20px; border: 4px solid black; }

Total width = 200 (content) + 20 (padding left) + 20 (padding right) + 4 (border left) + 4 (border right) = 248 pixels.

Section 14.7 shows how box-sizing: border-box handles this differently.

Margins

Margin is the transparent space outside an element's border. It separates the element from its neighbors. Because it is outside the border, it does not share the element's background color — it is always transparent.

p { margin: 20px; }

This paragraph has 20 pixels of transparent space on all four sides, separating it from neighboring elements.

Margin Shorthand

Margin uses the same shorthand pattern as padding:

/* All four sides */ margin: 20px; /* Top/bottom, then left/right */ margin: 10px 40px; /* Top, left/right, bottom */ margin: 10px 40px 20px; /* Top, right, bottom, left */ margin: 10px 40px 20px 0;

Individual margin properties:

margin-top: 10px; margin-right: 40px; margin-bottom: 20px; margin-left: 0;

Centering Elements Horizontally with Margin

One of the most useful applications of margin is centering a block element horizontally inside its parent.

.container { width: 700px; margin-left: auto; margin-right: auto; }

When both margin-left and margin-right are set to auto, the browser distributes the remaining space equally on each side, centering the element. This technique only works when the element has a defined width that is smaller than its parent.

The shorthand for this pattern is:

.container { width: 700px; margin: 0 auto; }

This sets top and bottom margin to zero, and left and right margin to auto.

Margin Collapsing

Common Pitfall

Margin collapsing is one of the most confusing behaviors in CSS for beginners. When two block elements are stacked vertically and both have vertical margins, those margins collapse into a single margin equal to the larger of the two — they do not add together.

h2 { margin-bottom: 24px; } p { margin-top: 16px; }

You might expect the space between a heading and the following paragraph to be 24 + 16 = 40 pixels. Instead, it collapses to the larger value: 24 pixels.

Margin collapsing only occurs between vertical (top/bottom) margins of block elements in normal document flow. Horizontal margins do not collapse. Margins inside flex containers and grid containers do not collapse.

Understanding margin collapsing explains many unexpected spacing results beginners encounter when trying to add space between elements.

Using Margin to Remove Browser Defaults

Browsers apply default margins to many HTML elements. Headings, paragraphs, lists, and other elements have built-in top and bottom margins that create readable spacing in plain HTML documents. These defaults are often the first thing a stylesheet resets or adjusts.

/* Remove all default margin */ h1 { margin: 0; } /* Override bottom margin only */ p { margin-bottom: 1rem; margin-top: 0; }
Sections 14.6 – 14.7
Sizing the Box: Width, Height, and box-sizing
Width · Height · max-width · box-sizing · border-box

Width and Height

width and height set the dimensions of an element's content area, under the default box-sizing setting.

.card { width: 300px; height: 200px; }

Percentage Widths

A percentage width is relative to the parent element's width.

.column { width: 50%; }

If the parent is 800 pixels wide, this column will be 400 pixels wide. If the parent is 600 pixels wide, the column will be 300 pixels wide. Percentage widths enable fluid layouts that respond to different container sizes.

max-width

max-width sets a maximum size limit. The element will grow to fill available space, but never exceed the maximum.

.article-body { width: 100%; max-width: 680px; }
Common Pattern

This is a common pattern for readable body text. On narrow screens the article fills the full width; on wide screens it stops growing at 680 pixels, preventing uncomfortably long lines.

min-width and min-height

min-width and min-height prevent an element from becoming smaller than a specified size.

button { min-width: 120px; padding: 10px 20px; }

This button will always be at least 120 pixels wide, even if its text content is shorter. This prevents small text from creating a disproportionately narrow button.

Height and Content Overflow

Risk

Setting a fixed height is often risky because content can exceed it.

.card { height: 200px; /* What if the content is taller? */ }

If the card's content requires more than 200 pixels, the content may overflow its container in unexpected ways. Section 14.9 covers how to handle overflow. In many layouts, it is better to set min-height and let the element grow with its content, or to avoid setting height altogether.

Height as a Percentage

Common Pitfall

Percentage heights are more complicated than percentage widths. A percentage height only works if the parent element has an explicitly defined height. This catches many beginners by surprise.

/* This will NOT work unless the parent has an explicit height */ .child { height: 100%; }

For full-height layouts, modern CSS uses viewport units (covered in Chapter 17) or Flexbox and Grid (covered in Chapters 15 and 16) rather than percentage heights on nested elements.

box-sizing

By default, width and height apply only to the content area. Adding padding or borders makes the total element size larger than what you declared. This is called the content-box model, because your width and height describe the content box only.

Most web developers find content-box behavior counterintuitive. When you say "this element should be 300 pixels wide," you usually mean the entire visible element should be 300 pixels, including its padding and border.

CSS provides a property that changes this: box-sizing.

box-sizing: content-box; /* Default — width/height apply to content only */ box-sizing: border-box; /* Width/height include padding and border */

With border-box:

.card { box-sizing: border-box; width: 300px; padding: 20px; border: 2px solid gray; }
A Key Insight

The total rendered width of this card is exactly 300 pixels. The padding and border are included within that 300. The content area becomes 300 − 20 − 20 − 2 − 2 = 256 pixels, calculated automatically by the browser.

Applying border-box Globally

Almost every modern web project applies border-box to all elements globally using a universal selector reset. This is now a standard practice.

Standard Practice
*, *::before, *::after { box-sizing: border-box; }

The * selector targets every element. *::before and *::after target pseudo-elements, which behave like elements but are not in the HTML. This one rule at the top of a stylesheet makes width and height mean what most people intuitively expect them to mean.

After this reset, when you set width: 300px, the element is 300 pixels wide, full stop — padding and borders included.

Worked Example: Comparing content-box and border-box

Worked Example 14.7.1
Two cards, identical CSS, one setting changed
  • SetupTwo cards, each set to width: 300px, padding: 24px, and border: 2px solid black.
  • content-boxContent area: 300px. Total width: 300 + 24 + 24 + 2 + 2 = 352px.
  • border-boxTotal width: 300px (the declared width). Content area: 300 − 24 − 24 − 2 − 2 = 248px.
  • Why it mattersIf you have two cards side by side in a 700-pixel container, content-box cards at width: 300px would occupy 704 pixels total — overflowing the container. With border-box, they fit comfortably. This is exactly why the global border-box reset is so widely used: it makes layout math straightforward.
Sections 14.8 – 14.10
Display, Overflow, and Debugging
Block vs. Inline · Overflow Control · DevTools Inspection

Block and Inline Elements

Not all elements behave the same way in the document flow. CSS recognizes two primary display types that affect how elements participate in the box model.

Block Elements

Block elements:

  • Start on a new line
  • Stretch to fill the full width of their parent container by default
  • Respect width, height, margin, and padding on all four sides
  • Stack vertically

Common block elements: <div>, <p>, <h1>–<h6>, <section>, <article>, <header>, <footer>, <nav>, <ul>, <ol>, <li>, <form>, <blockquote>.

p { display: block; /* Default for paragraphs — usually not necessary to write */ width: 400px; margin: 16px 0; padding: 12px; }

Inline Elements

Inline elements:

  • Flow within text, like words in a sentence
  • Only occupy as much width as their content requires
  • Do not start on a new line
  • Ignore width and height settings
  • Respect left and right padding and margin, but top and bottom padding and margin can behave unexpectedly and may not push other block elements away

Common inline elements: <span>, <a>, <strong>, <em>, <img> (technically inline-block, discussed below), <code>, <abbr>.

span { display: inline; /* Default for spans — usually not necessary to write */ color: navy; font-weight: bold; }
Common Pitfall

Setting width: 200px on an inline element will have no visible effect in most browsers.

Inline-Block

display: inline-block is a hybrid. Elements with this value:

  • Flow inline with surrounding content — they do not force a new line
  • But respect width, height, margin, and padding on all four sides, like block elements
.badge { display: inline-block; padding: 4px 10px; border-radius: 12px; background-color: navy; color: white; font-size: 0.85rem; }

This badge sits inline with text, but has full box model control. Inline-block was heavily used in older CSS layout techniques. Today, Flexbox and Grid are usually preferred for layout, but inline-block remains useful for small inline elements that need padding and sizing.

Changing Display

Any element's display value can be overridden in CSS.

/* Make a link display as a block (fills its container, useful for navigation) */ nav a { display: block; padding: 10px 16px; } /* Make a list display inline for a horizontal menu */ nav li { display: inline; }

Changing display type is common in navigation menus, button groups, and component styling.

The display Property and Layout Systems

In addition to block, inline, and inline-block, the display property also enables the Flexbox and Grid layout systems:

display: flex; /* Creates a flex container (Chapter 15) */ display: grid; /* Creates a grid container (Chapter 16) */
Looking Ahead

These values are covered in detail in the next two chapters. The box model you have just learned does not disappear when you start using Flexbox or Grid — it is the foundation every flex item and grid item is still built on.

Overflow

When content is larger than its container, it overflows. The overflow property controls what happens.

overflow: visible; /* Default — content spills outside the box */ overflow: hidden; /* Content is clipped at the box edge */ overflow: scroll; /* Scrollbars are always shown */ overflow: auto; /* Scrollbars appear only when needed */

The Default: overflow visible

Without setting overflow, content simply flows out of its container if it exceeds the declared size. This is often visible as text or images spilling outside a div with a fixed height.

/* This content may overflow visually */ .card { height: 150px; background-color: lightyellow; }

If the text inside the card requires 300 pixels of height, the text will flow below the card's visible yellow background — it exists in the document but sits outside the card's box.

overflow hidden

overflow: hidden clips anything outside the container's box. Content that extends past the edges is simply not displayed.

.card { height: 150px; overflow: hidden; }

Common uses for overflow: hidden:

  • Image thumbnails where you want to hide the edges of an image that has been zoomed or cropped
  • Cards where you do not want text to spill
  • Containing floated child elements — a technique called clearfix, now largely replaced by Flexbox and Grid

overflow auto

overflow: auto adds scrollbars only when content exceeds the container's dimensions.

.log { height: 200px; overflow-y: auto; border: 1px solid gray; padding: 12px; }

This is useful for scrollable log panels, code editors, preview boxes, and any container that should be a fixed height but may contain variable-length content.

overflow-x and overflow-y

Overflow can be controlled independently on each axis.

overflow-x: hidden; /* Clip horizontal overflow */ overflow-y: auto; /* Scroll vertical overflow when needed */

overflow-x: hidden is often applied to the body or page wrapper to prevent horizontal scrollbars caused by full-width elements.

Debugging Layout with the Box Model Inspector

The browser's developer tools include a visual box model panel that shows the exact computed size of each region for any selected element. Learning to use this panel is one of the most practical debugging skills in web development.

Accessing the Box Model Panel

  1. Open developer tools (F12 or right-click → Inspect)
  2. In the Elements panel, click on an element in the HTML tree or on the page
  3. In the Styles or Computed panel, scroll down to find the box model diagram

The diagram shows the element's margin (usually in orange), border (usually in yellow/gold), padding (usually in green), and content dimensions (usually in blue), with pixel values labeled in each region.

How to Read the Box Model Diagram

The numbers in each region show the computed size of that layer. For example:

[Margin: 16px on all sides] [Border: 0] [Padding: 12px top/bottom, 20px left/right] [Content: 320 × 48]

This tells you the element's content area is 320 by 48 pixels, with 12 pixels of top/bottom padding, 20 pixels of left/right padding, no border, and 16 pixels of margin on all sides.

Common Debugging Scenarios

Problem 1
An element is wider than expected.
Check the computed content width, padding, and border values. If box-sizing: content-box is in effect, the total width is content + left padding + right padding + left border + right border. Adding the global border-box reset usually simplifies this.
Problem 2
There is unexpected space above or below an element.
Check the margin values. Browsers apply default top and bottom margins to headings, paragraphs, and other elements. Look for margin-top or margin-bottom being inherited from the browser stylesheet, visible in the Styles panel as a crossed-out or grayed-out rule labeled user agent stylesheet.
Problem 3
Two block elements are not separated by the expected space.
Remember margin collapsing: adjacent vertical margins collapse to the larger value, not their sum. Use the box model inspector to check the computed margin on each element.
Problem 4
Padding is not visible.
Verify the element's background color. If the background is transparent, you cannot see padding visually. The box model inspector will still show the padding value even when it is not visible against a white background.
Problem 5
An element has a fixed height but content is overflowing.
Add overflow: hidden or overflow: auto as appropriate. If the fixed height is causing problems, consider whether min-height would be a better choice.

Tracing a Layout Problem Step by Step

A Repeatable Method

1. Identify which element looks wrong on the page. 2. Inspect that element in developer tools. 3. Check the box model diagram for its actual computed values. 4. Compare the computed values against what your CSS declares. 5. Look for margin collapse, unexpected browser defaults, or a missing box-sizing setting. 6. Make a small change, save, and inspect again.

This method — inspect, compare, hypothesize, change, verify — is how professional web developers debug layout problems.

Chapter Summary

Key Terms

box model
The CSS specification that describes how every HTML element occupies space on a page, consisting of a content area, padding, border, and margin.
content area
The innermost region of the box model, where an element's text, images, or child elements are rendered.
padding
The space between an element's content and its border. Padding is inside the element and shares its background color.
border
The line drawn around an element's padding and content. It has width, style, and color properties.
margin
The transparent space outside an element's border that separates it from neighboring elements. Margin does not share the element's background color.
border-radius
A CSS property that rounds the corners of an element's border box.
box-sizing
A CSS property that determines whether width and height apply to the content area alone (content-box, the default) or include padding and border (border-box).
content-box
The default box-sizing value. Width and height apply only to the content area.
border-box
A box-sizing value that includes padding and border in the declared width and height.
display: block
Makes an element behave as a block-level element: it starts on a new line and fills its parent's width.
display: inline
Makes an element flow within text without starting a new line. Width and height have no effect.
display: inline-block
A hybrid display value. Elements flow inline but respect width, height, and all four sides of margin and padding.
overflow
A CSS property that controls how content behaves when it is larger than its container.
margin collapsing
The CSS behavior where adjacent vertical margins between block elements collapse to the larger value instead of adding together.
max-width
A CSS property that sets the maximum width an element can grow to, while still allowing it to shrink on smaller screens.
universal selector reset
The CSS rule *, *::before, *::after { box-sizing: border-box; } that applies border-box sizing to every element.
PracticeCode Reading

Read each CSS rule carefully before answering the questions that follow it.

Exercise 14-A
.announcement { width: 500px; padding: 24px; border: 3px solid darkred; margin: 40px auto; background-color: lightyellow; }
  1. What is the content area width?
  2. What is the total rendered width of this element, assuming the default box-sizing: content-box?
  3. What does margin: 40px auto do to this element?
  4. Which layers share the lightyellow background color?
Exercise 14-B
*, *::before, *::after { box-sizing: border-box; } .card { width: 360px; padding: 30px; border: 4px solid steelblue; }
  1. After the global reset, what does width: 360px include?
  2. What is the content area width for .card?
  3. What is the total rendered width of .card?
Exercise 14-C
h2 { margin-bottom: 20px; } p { margin-top: 12px; margin-bottom: 0; }

A page has an <h2> immediately followed by a <p>.

  1. What is the space between the heading and the paragraph?
  2. Why is the answer not 32 pixels?
Exercise 14-D
.preview { height: 180px; overflow: hidden; background-color: white; border: 1px solid lightgray; padding: 16px; }
  1. What happens if the content inside .preview is taller than 180 pixels?
  2. What overflow value would add a scrollbar instead of hiding the extra content?
  3. Would changing overflow: hidden to overflow: visible make the extra content appear? Where would it appear?
PracticeCode Tracing
Trace 14-1 — Total Element Size

A stylesheet does not include a box-sizing reset. Calculate the total rendered width for each element.

Element A

.box-a { width: 300px; padding: 16px; border: 2px solid black; margin: 20px; }

Element B

.box-b { width: 300px; padding-left: 30px; padding-right: 10px; border-left: 5px solid navy; border-right: 0; margin: 20px; }

For each element: What is the total rendered width (content + padding + border, excluding margin)? What is the total horizontal space the element occupies including its margins?

Trace 14-2 — border-box Recalculation

Now apply the global reset:

*, *::before, *::after { box-sizing: border-box; }

Recalculate the content area width for each element in Trace 14-1 with border-box now in effect. The total rendered width is fixed at 300 pixels for each.

Trace 14-3 — Margin Collapsing
<div class="section-a">Section A</div> <div class="section-b">Section B</div> <div class="section-c">Section C</div>
.section-a { margin-bottom: 40px; } .section-b { margin-top: 20px; margin-bottom: 30px; } .section-c { margin-top: 50px; }
  1. What is the space between Section A and Section B?
  2. What is the space between Section B and Section C?
  3. In each case, explain why the answer is what it is.
PracticePredict the Result
Predict 14-1
.container { width: 800px; margin: 0 auto; background-color: lightyellow; padding: 40px; }

The page background is white. The container's parent is the <body>. What do you expect to see?

  1. A yellow box that is 800 pixels wide, centered on the page, with the yellow color extending to include the padding area
  2. A yellow box that is 720 pixels wide, centered on the page, with 40 pixels of transparent space around it
  3. A yellow box that is 880 pixels wide, centered on the page
  4. A yellow box that fills the entire viewport

What is the total rendered width assuming content-box?

Predict 14-2
<p>Visit our <a href="#">resource page</a> for more information.</p>
a { width: 200px; height: 80px; background-color: lightblue; padding: 10px; }
  1. Will the link appear as a 200 × 80 pixel blue box?
  2. Why or why not?
  3. What display value would you need to add to make width and height take effect?
PracticeDebugging Practice

Each scenario below describes a layout problem. Identify the likely cause and write a corrected CSS rule.

Debug 14-1

A student writes this CSS expecting two divs, each declared at width: 50%, to sit side by side inside a 600-pixel container. But together they seem too wide.

.column { display: inline-block; width: 50%; padding: 20px; border: 2px solid gray; }

What is the total rendered width of each .column with default box-sizing? Why do they not fit side by side? What is the fix?

Debug 14-2

A student wants a paragraph to have exactly 16 pixels of space above it. They write:

p { margin-top: 16px; }

But on the page, there appears to be more space than expected above the first paragraph — it looks like there might be 24 pixels or more. The heading above uses this CSS:

h2 { margin-bottom: 24px; }

Explain what is happening. What is the actual space between the heading and the paragraph?

Debug 14-3

A student creates a testimonial card with a fixed height:

.testimonial { height: 120px; background-color: lightyellow; border: 1px solid gold; padding: 16px; }

Longer testimonials are spilling below the card's yellow background and overlapping the next card. What property and value would clip the overflowing content? What property and value would add a scrollbar instead?

Debug 14-4

A student is confused because this element seems to ignore its width and height:

<p>Here is some text with a <span class="highlight">highlighted phrase</span> inside it.</p>
.highlight { width: 200px; height: 50px; background-color: yellow; }

The yellow background appears but does not seem to be 200 by 50 pixels. What is the problem, and what two solutions could fix it?

PracticeShort Build Tasks
Task 14-1 — Global Reset and Basic Card

Create a small HTML file with a single .card div containing a heading and two sentences of text. Write a stylesheet that:

  1. Applies the global box-sizing: border-box reset
  2. Sets the card to width: 380px, centered with margin: 40px auto
  3. Adds padding: 24px inside the card
  4. Adds a 2px solid border in a color of your choice
  5. Rounds the corners with border-radius: 8px
  6. Sets a light background color on the card

Inspect the card in developer tools and confirm the total rendered width is 380 pixels.

Task 14-2 — Inline Versus Block

Create an HTML file with the following elements: a <div> with a visible background, a <span> with a visible background inside a paragraph, and an <a> link styled with padding and a background color.

Observe the difference between how the div, span, and link occupy space. Then: change the link's display to inline-block and add width: 160px; change the link's display to block and observe how it behaves in the paragraph.

Write a brief explanation of what changed and why.

Task 14-3 — Overflow Comparison

Create a div with width: 300px, height: 150px, and a visible background color. Fill it with enough text to overflow. Then test all four values of overflow (visible, hidden, scroll, auto) one at a time. For each value, write one sentence describing what you observe.

Task 14-4 — Box Model Inspection

Open any existing web page or one of your own practice pages. Using browser developer tools:

  1. Inspect three different elements: a heading, a paragraph, and a navigation link or button
  2. For each element, record the content width, padding, border, and margin values shown in the box model diagram
  3. Check whether the page uses a box-sizing reset by looking in the Styles panel for a * or html rule

Write your observations in a comment at the top of a new CSS file.

CumulativeMixed Review

These questions connect Chapter 14 to earlier material.

Review 14-1 (Chapters 11–12)

A student adds a CSS rule that should change the background color of all .card elements. But some cards are not changing. They look at the rule and notice an override in a later stylesheet section that is setting background-color: white. Which CSS concept explains why the later rule is winning?

Review 14-2 (Chapter 13)

A paragraph has font-size: 18px, line-height: 1.6, and padding: 20px. In your own words, explain the difference between line-height and padding. What does each control?

Review 14-3 (Chapter 6)

A student uses <div> for every section of their page. Their layout works, but their page has no semantic landmarks. List three semantic HTML elements from Chapter 6 that could replace some of those <div> elements and explain what meaning each provides.

Review 14-4 (Chapter 13)

A .callout box needs: 16 pixels of padding on all sides, a bottom border only (2 pixels, solid, in a medium blue color), and no border on any other side. Write the CSS rules. Use both the padding shorthand and targeted border-side properties.

PracticeAccessibility Check

Spacing and layout choices affect the usability of pages, including for users with different needs.

Check 14-A — Sufficient Tap Target Size

Buttons and links that are too small are difficult to use, especially on touch screens. Inspect a navigation link or button on a practice page. Does it have enough padding to be comfortably tappable? A general guideline is that interactive elements should be at least 44 by 44 pixels in total rendered size. Measure the element's padding and content in developer tools to check.

Check 14-B — Overflow and Screen Readers

When overflow: hidden clips text, sighted users cannot read it — but screen readers may still be able to access it in some configurations. This can cause unexpected experiences. Describe a case where clipping content with overflow: hidden might confuse or mislead a screen reader user, and explain why it is better to simply not show content than to visually hide text that is semantically present.

Check 14-C — Line Length and Readability

Wide text columns reduce readability. Research suggests that comfortable reading line length is typically between 45 and 75 characters per line. Using max-width on a text container (often in ch units, where 1ch is the width of the "0" character) can enforce readable line lengths. Write a CSS rule using max-width to limit a paragraph container to approximately 65 characters wide.

ProjectMini-Project: Styled Content Cards
Mini-Project 14.1
A row of three content cards, built and inspected
  • BuildBuild a small HTML page containing a row of three content cards. Each card should include a heading (h3), two sentences of descriptive text, and a mock "Read more" link at the bottom.
  • StyleApply the global box-sizing: border-box reset. Style each card with padding, a border, and a border-radius. Set a fixed width on each card. Use margin to add space between cards — they will stack vertically for now; Chapter 15 covers making them sit side by side.
  • ContainUse overflow: hidden to prevent any card from growing beyond a defined max-height, if the content overflows.
  • VerifyOpen developer tools and inspect one card. Confirm the total rendered width matches your expectations given border-box sizing, padding/border/margin values appear correctly in the box model diagram, and no unexpected browser-default margins are affecting spacing.
  • DocumentAdd a short comment block at the top of your CSS file explaining what box-sizing: border-box does and why you applied it.
Chapter 14 Checklist
Use this to confirm your understanding before moving to Chapter 15
Concepts
  • I can name the four nested regions of the box model, from the inside out.
  • I can explain what width and height measure by default, and how padding and border change an element's total rendered size.
  • I can explain the difference between content-box and border-box, and why most projects apply a global border-box reset.
  • I can explain margin collapsing and predict the resulting space between two stacked block elements.
  • I can explain the difference between block, inline, and inline-block display.
  • I can describe what each value of overflow does: visible, hidden, scroll, and auto.
Applications
  • I can center a block element horizontally using margin: 0 auto.
  • I can write padding, border, and margin shorthand, and target individual sides when needed.
  • I can build a simple card with padding, a border, rounded corners, and a defined width.
Debugging Habits
  • I can open developer tools and read the box model diagram for any selected element.
  • I know to check for margin collapsing and browser-default margins when spacing looks wrong.
  • I know to check for a missing box-sizing reset when an element looks wider than declared.
Preview — Chapter 15
Flexbox Layout

Chapter 14 introduced the box model, the fundamental structure that governs how every element occupies space on a page. Now that you understand how individual boxes work, you are ready to learn how to arrange multiple boxes in relation to each other.

Chapter 15 introduces Flexbox, a one-dimensional layout system that controls how child elements are distributed along a row or column. Flexbox solves layout challenges that were difficult or impossible to handle cleanly with margins and inline-block elements alone — including evenly spaced navigation menus, centered card rows, equal-height columns, and flexible responsive sections. The box model is the foundation; Flexbox is the first major tool for building layouts on top of it.

MGU Library Connection
  • Web Development: HTML, CSS, and JavaScript — Appendix D: CSS Reference
  • Web Development: HTML, CSS, and JavaScript — Appendix E: CSS Layout Reference
  • Companion resource: CSS Box Model Chart
  • Companion resource: CSS Selector Chart
  • Next chapter: Chapter 15 — Flexbox Layout
  • MGU Digital Literacy — background reading on browser developer tools