thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

03.05 · CSS properties / Practice · At your pace

Measure the CSS box model

Separate content, padding, border and margin before choosing dimensions.

Activity 5 of 7 in CSS properties

Rob builds content, padding and border layers, then marks margin outside the border.
Rob’s comic strip · CSS properties

Explore the layers before you code

Start with padding, then add a border and margin. Compare the CSS values, colored diagram and width calculation. Choose Sizing to see what changes when the same width uses border-box.

EXPLORE THE BOX MODEL

One box. Four layers.

Change one value and connect the CSS to the space it creates.

Padding creates room inside the border, around your content. Move the padding control and watch the inside space grow.

Padding

The declared height stays at 96px. Values are CSS pixels.

ContentPaddingBorderMargin
A box with content, padding, border and marginAdjust the controls to compare the four layers.Your content

Diagram scales to fit. Colors identify the layers; they are not background-color declarations. Measurements remain actual CSS values.

Content
180 × 96px
Border box
204 × 120px
With margins*
204 × 120px
styles.cssLive example
.card {  box-sizing: content-box;  width: 180px;  height: 96px;  padding: 12px;  border-width: 0px;  border-style: solid;  margin: 0px;}
How the width adds up

180 content + 12 left padding + 12 right padding = 204px border box.

Margins are outside the border box.

*With margins shows the arithmetic space allocation for this isolated box. Vertical margins between blocks can collapse, so this is not a promise that all page gaps add together.

Explore freely. This demo does not change your saved exercise code or mark the lesson complete.

Understand the change

The content box holds text or an image. Padding is inside the border; margin is outside. With content-box, a 200px width plus 20px padding and 4px border on each side totals 248px. With border-box, those parts fit inside the declared 200px.

A border-box example
styles.csscss · example
.card {
  box-sizing: border-box;
  width: 200px;
  padding: 12px;
  border: 4px solid navy;
}

What to try and where to look

Open the practice workspace and follow its checklist in the named files. Keep the supplied content and compare your result in the preview before checking your work.

YOUR TURN

Measure the CSS box model

Work through the checklist in styles.css. Run the preview, then check your work.

To pass this challenge
  • In styles.css, add a rule with selector body outside all media blocks, directly below the STYLE BODY comment.
  • Set its font-family property to Arial, sans-serif.
  • Set its margin property to 24px.
  • Set its line-height property to 1.5.
  • In styles.css, add a rule with selector .card outside all media blocks, directly below the STYLE CLASS CARD comment.
  • Set its box-sizing property to border-box.
  • Set its width property to 200px.
  • Set its padding property to 20px.
  • Set its border property to 4px solid navy.
  • Set its margin property to 16px.
  • Keep the supplied page content, classes, IDs and link destinations unless a checklist item explicitly changes them.

Edit styles.css and watch the preview update after you pause typing. Open index.html to inspect its stylesheet link. Run or Check saves immediately. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Start with the target

The content box holds text or an image. Padding is inside the border; margin is outside. With content-box, a 200px width plus 20px padding and 4px border on each side totals 248px. With border-box, those parts fit inside the declared 200px.

Make one change at a time

Find the selector for the element named in the requirement. Add one declaration, run the preview and inspect the result. Check the requirement list before moving on.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

ONE STEP FORWARD

Explain what changed in the preview before continuing. Save your work or share a snapshot if you need help.