Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate18 minLESSON 08 / 16

The CSS box model

Change a control, or edit the CSS. The preview uses that same CSS.

box-sizing

Numeric changes ease so you can watch them.

You changed padding and box-sizing on one declared width. The border edge grew under content-box because padding and border were added outside that width, and it stayed on the declared width under border-box.

+Go deeper

Four layers

From the outside in, a box is margin, border, padding, then content. Backgrounds paint the content and padding, and by default they also sit under the border. Margin is outside the border and stays transparent. This is the .

Two meanings of width

From the inside out the layers are content, padding, border, and margin. Padding is space inside the border. Margin is space outside it, between this box and its neighbors. The initial value of box-sizing is content-box. Width and height then measure the content only, so padding and border are added outside. A declared width of 200px with 20px of padding on each side takes 240px before the border is even counted. border-box makes the declared width include content, padding, and border, so that 200px box stays 200px and the content shrinks to make room. Margin stays outside both models. Many resets set border-box on every element because the declared size is then the size you meant. A later rule can still lose on the if a heavier selector sets the other model. The playground uses 140px and 36px of padding so the same idea is large enough to see.

content-box and border-box
box-sizingWhat width measuresWhere padding and border go
content-boxThe content box onlyAdded outside the declared size
border-boxContent, padding, and borderIncluded inside the declared size

Margin stays outside

Switching to border-box does not pull margin inside the width. Two blocks with width 100 percent and a horizontal margin still overflow. Vertical margins of adjacent blocks in normal flow can collapse into the larger of the two, so 16 plus 16 often becomes 16.

Percent needs a reference

A percentage width is a percentage of the containing block width. A percentage height often computes to auto unless the parent has a specified height. The percentage is not broken. The reference length is missing.

Read the box in the tools

The Elements panel draws margin, border, padding, and content with used pixel values. If a width rule is crossed out, it lost the cascade from the previous lesson. The playground readout is the small version of that diagram: declared width against the border edge.

04

Guided challenge

The CSS box model

The style attribute already says content-box. The script still logs border-box. Read the first declaration from the attribute instead of printing a fixed word.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

content-box
140px
36px
Notes

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Edit the HTML and CSS. The JavaScript tab can read the result. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01Under content-box, what does width measure?
    02What does border-box change?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson