Skip to lesson content
Web Technologies โ€บ Level 12
LEVEL 12 ยท CSS

CSS Box Model and Layout

Calculate component sizes, follow the page flow and keep every explanation readable.

๐Ÿ“ฆ Box model๐Ÿ“ Sizing๐ŸŽฏ Beginner โ†’ Intermediate๐Ÿงช Lab + MCQs
01

Learning Objectives

Box parts

Identify each layer and its purpose.

Sizing

Calculate a simple horizontal box and use size constraints.

Flow

Understand how ordinary blocks and inline content are arranged.

Diagnosis

Check overflow, positioning and containing space before adding overrides.

02

The Box Model โ€” Four Different Areas

Read from inside to outside
Content

The actual text or child content area.

Padding + border

Breathing room around content, then a visible or invisible boundary.

Margin

Outside spacing relative to neighboring content.

03

Content Dimensions โ€” State What width Means

.card {
  box-sizing: content-box;
  width: 200px;
  padding: 20px;
  border: 4px solid #173451;
}
04

Padding โ€” Space Inside the Boundary

.notice { padding: 12px 20px; }
.card { padding: 10px 16px 20px 24px; }
05

Borders and Outlines โ€” Different Purposes

.card { border: 2px solid #bdd5e6; border-radius: 12px; }
button:focus-visible { outline: 3px solid #126d96; outline-offset: 3px; }
06

Margins, Gaps and Collapsing

.stack { display: flex; flex-direction: column; gap: 16px; }
.stack > .card { margin: 0; }
07

Worked Calculation โ€” A Content-Box Card

Content width = 200px
Horizontal padding = 20px + 20px = 40px
Horizontal border = 4px + 4px = 8px
Border-box width = 200px + 40px + 8px = 248px
Border box plus horizontal margins = 248px + 10px + 10px = 268px
08

border-box โ€” Include Padding and Border in the Width

.card {
  box-sizing: border-box;
  width: 200px;
  padding: 20px;
  border: 4px solid #173451;
  margin: 10px;
}
/* Content width: 200 - 40 - 8 = 152px
   Border box + horizontal margins: 200 + 20 = 220px */
09

Width Constraints โ€” Flexible Does Not Mean Unbounded

.card {
  box-sizing: border-box;
  width: 100%;
  max-width: 40rem;
  margin-inline: auto;
  padding: 1.25rem;
}

Height, Wrapping and Content Growth

.explanation { min-height: 8rem; height: auto; }
.long-token { overflow-wrap: anywhere; }
10

Normal Flow โ€” Start with the Document Order

11

Block, Inline and Inline-Block

Block

A typical block participates as a block in normal flow. Width and height can apply under the relevant box rules.

Inline

  • Text-level content participates in line layout.
  • Ordinary non-replaced inline boxes do not treat width/height like a block box.

Inline-block

A box participates in the inline flow while providing a box that can accept dimensions.

Context

The element type and display value are related but different. A semantic element can have a changed display value.

.badge { display: inline-block; padding: 4px 10px; }
12

display โ€” Outer Flow and Inner Layout

.actions { display: flex; gap: 12px; }
.cards { display: grid; gap: 16px; }
13

Overflow โ€” Preserve Access to the Content

.code-scroller { max-width: 100%; overflow-x: auto; }
.code-scroller:focus-visible { outline: 3px solid #126d96; }

<!-- HTML -->
<div class="code-scroller" tabindex="0" aria-label="Scrollable code example">
  <pre>Long code example goes here.</pre>
</div>
14

Positioning โ€” Flow, Containing Blocks and Scrolling

static / relative

Static uses normal positioning. Relative offsets a box while its original place in flow remains reserved.

absolute

  • Takes the box out of normal flow and positions it using its containing block.
  • Other content does not automatically reserve its place.

fixed

Usually uses the viewport as its reference, but some ancestors can establish a different containing block.

sticky

Participates in flow, then is constrained by its scrolling context and containing area when an applicable inset is set.

.card { position: relative; }
.card .badge { position: absolute; top: 12px; right: 12px; }
.section-label { position: sticky; top: 1rem; }
15

Choosing a Layout โ€” Flow First, Flexbox or Grid When Needed

.actions { display: flex; flex-wrap: wrap; gap: 12px; }
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
16

Premium Visualizer โ€” Build the Box

Follow the content-box arithmetic, then compare the same declared width in border-box mode. Select a stage or use playback. Each stage states the meaning of the measured width so content size and outside space are not confused.

CSS BOX MODEL TRACE
Step 1 of 5
STEP 01

Start with the content width

This content-box example begins with a definite 200px content width.

Content width = 200px

What is happening?

The demonstration uses symmetric pixel values with no layout shrinking or min/max constraints.

17

Premium Interactive โ€” Box Size Lab

Default content-box example: 200px content, 248px border box and 268px including horizontal margins.

Calculated widths for this example

CSS supplied to the preview

18

Debugging Layout โ€” Find the Constraint

Measure the right area

Inspect content, padding, border and margin separately in the browser box-model tools.

Check constraints

Look at width, min/max sizes, box-sizing and automatic minimums.

Follow the flow

Find out whether the box is in normal flow, a flex/grid item or absolutely positioned.

Preserve information

Locate the overflow cause before clipping or hiding the content.

19

Interview Questions โ€” Flip to Explain

QUESTION

Does border-box include margin?

Click or press Enter to explain
ANSWER

No. The declared border-box width includes content, padding and border. Margin remains outside it.

QUESTION

Why can width: 100% overflow in content-box mode?

Click or press Enter to explain
ANSWER

The declared width describes content. Horizontal padding and borders can add to that width and exceed the intended container space.

QUESTION

Is padding the same as margin?

Click or press Enter to explain
ANSWER

No. Padding is inside the border around content; margin is outside the border relative to surrounding boxes.

QUESTION

Do two adjacent vertical margins always add?

Click or press Enter to explain
ANSWER

No. Some vertical margins collapse in normal block flow. The conditions matter; horizontal margins and flex/grid item margins do not follow that same collapsing rule.

QUESTION

Does absolute positioning reserve space for its box?

Click or press Enter to explain
ANSWER

No. It removes the box from normal flow, so other flow content does not automatically reserve its space.

QUESTION

Why might a flex item refuse to shrink?

Click or press Enter to explain
ANSWER

Its automatic minimum or long unbroken content may constrain it. Inspect the relevant item; a targeted min-width: 0 and suitable wrapping or scrolling may help.

QUESTION

Why is overflow: hidden a poor general fix for a lesson panel?

Click or press Enter to explain
ANSWER

It may clip the explanation. Find the incorrect sizing or content constraint and preserve a way to read all information.

QUESTION

What does a sticky element need?

Click or press Enter to explain
ANSWER

A relevant non-auto inset, an appropriate scrolling context and room within its containing constraints. Ancestor overflow can affect the behavior.

20

MCQ Practice โ€” Calculate and Explain

PRACTICE

1. Which sequence goes from inside to outside?

PRACTICE

2. A content-box width is 200px, padding is 20px per side and border is 4px per side. What is the border-box width?

PRACTICE

3. The same box has 10px margins per side. What is its simple horizontal border-plus-margin space?

PRACTICE

4. In border-box mode with width 200px, padding 20px per side and border 4px per side, what is the content width here?

PRACTICE

5. Which remains outside a border-box width?

PRACTICE

6. What does padding: 12px 20px mean?

PRACTICE

7. Which statement about margin collapsing is correct?

PRACTICE

8. What happens to an absolutely positioned boxโ€™s normal-flow reservation?

PRACTICE

9. Which approach preserves an oversized code example?

PRACTICE

10. Which hidden state normally preserves layout space?

PRACTICE

11. Why can a fixed-height explanation become unreadable?

PRACTICE

12. Which is a suitable next step when a column refuses to shrink?

21

Extra Practice โ€” Specific Dimensions and Behaviors

Content-box calculation

Use width 160px, padding 10px and border 2px per side. Expected border box: 184px; with 10px margins per side: 204px.

Border-box comparison

Keep those values but set border-box. Expected content width: 136px; border box: 160px; with horizontal margins: 180px.

Padding boundary

Set padding to 0px, then 20px with content-box width 200px and border 4px. Expected border widths: 208px then 248px.

Margin separation

  • Change only margin from 0px to 20px per side.
  • Expected: content and border widths are unchanged; horizontal outside spacing increases by 40px.

Long code

  • Put a long code line in a local overflow-x auto wrapper.
  • Expected: information remains available inside the wrapper without making the whole lesson wider.

Position comparison

  • Offset a relative badge, then compare an absolute badge inside a relative card.
  • Expected: relative retains its flow place; absolute needs deliberate space planning.

Text growth

Double an explanationโ€™s text length and increase font size. Expected: an auto-height panel grows rather than clips.

Reading order

  • Build a title, explanation, lab and summary in source order.
  • Expected: layout styling preserves a useful reading and keyboard sequence.
22

Quick Revision

Layers: Content โ†’ padding โ†’ border โ†’ margin.
Content-box: Add horizontal padding and border to a declared content width.
Border-box: Padding and border fit within the declared border-box width.
Margin: Outside spacing is not included in border-box width.
Padding: Internal breathing room also affects dimensions.
Flow: Ordinary content can grow and move following content naturally.
Growth: Prefer flexible text panels to arbitrary fixed heights.
Overflow: Keep a usable route to all content.
Position: Absolute and fixed positioning do not reserve ordinary flow space.
Diagnosis: Inspect constraints, context and actual box dimensions.
23

Glossary โ€” Flip to Learn

TERM

Content box

Click to see meaning
DEFINITION

Content box

The area allocated to the elementโ€™s content.

TERM

Padding

Click to see meaning
DEFINITION

Padding

Space between the content and the border.

TERM

Border box

Click to see meaning
DEFINITION

Border box

The area including content, padding and border.

TERM

Margin

Click to see meaning
DEFINITION

Margin

Spacing outside the border relative to surrounding layout.

TERM

box-sizing

Click to see meaning
DEFINITION

box-sizing

The property that changes what a declared width or height includes.

TERM

Normal flow

Click to see meaning
DEFINITION

Normal flow

The ordinary arrangement of content under the applicable formatting rules.

TERM

Containing block

Click to see meaning
DEFINITION

Containing block

The reference area used for certain sizes and positioning calculations.

TERM

Overflow

Click to see meaning
DEFINITION

Overflow

Content extending beyond the relevant box area.

TERM

Margin collapsing

Click to see meaning
DEFINITION

Margin collapsing

The combining of certain vertical margins under specific normal-flow conditions.

TERM

Stacking context

Click to see meaning
DEFINITION

Stacking context

A grouping within which stacking order is resolved as part of the pageโ€™s stacking rules.

24

Final Challenge โ€” A Readable Workshop Card

25

Level 12 Complete?

Before marking complete, compare both box-sizing modes, calculate the stated example, preserve a long code line without clipping, and explain a normal-flow versus positioned layout choice. Completion stores a local study milestone; it is not an assessment score.