HTML and CSS lesson 14: The box model: padding, border and margin
US version. US grades and US spelling. · UK version of this lesson
Every element is a box: content, then padding, then border, then margin. Normally width is just the content; box-sizing: border-box makes it the whole box. A complete 40-minute HTML and CSS lesson for beginners (Grades 6–7), from Unit 5: Boxes and layout. You build real web pages right here in your browser – nothing to install.
- Time
- 40 minutes + 20 min homework
- Grade
- Grades 6–7 (ages 11–12)
- You need
- A computer or tablet, paper and a pencil
- Graded
- Instantly, with explanations
For parents: how to use this lesson
You don't need to know HTML yourself. The lesson explains each step on screen, the way Mr Babbage, our Head of Computing, likes it: a worked example first, then a go for your child. When your child builds a page, the browser draws it straight away and Classfern grades it with a checklist.
- Read what your child will learn together.
- Press Start the lesson below. Sit with your child for the teaching and the worked example (about 15 minutes): ask them to point at each tag and say what it does.
- Let them do the practice on their own. If a page doesn't look right, the checklist says what is missing – let them find the mistake before you help.
- Finish with the exit questions and the talk question: “Why might designers like `box-sizing: border-box`?”
- Later that day or the next day: the homework (20 minutes), then a practice test.
Watch out for
widthincludes padding. Only withbox-sizing: border-box.- Margin is part of the box's width. Margin is space outside the border.
Miss Lovelace's tip for home: ask your child to change one thing on their page – a word, a color, a size – and predict what will happen before they look. Predicting first is how programmers learn fastest.
What your child will learn
Learning goal: name the four parts of the box model.
Success looks like
- I can name the four parts of the box model.
- I can work out how wide a box is on screen.
- I can use padding, border, margin and box-sizing.
Key words
box model · content · padding · border · margin · box-sizing · shorthand
What to have ready
- A device with a web browser (pages are built and checked right in the browser – nothing to install)
- Paper for planning and sketching
- A keyboard helps for typing code (a tablet works too)
The big ideas (for the adult)
Every element is a rectangle with four layers, from the inside out: the content, the padding (space inside the border), the border, and the margin (space outside the border, pushing other boxes away).
Normally width sets only the content. Padding and border are ADDED on both sides: width: 200px; padding: 10px; border: 5px solid; is 200 + 20 + 10 = 230px wide on screen. Margin is outside, so it doesn't count. With box-sizing: border-box;, width means the whole box, which is easier to plan.
A border needs a style: border: 3px solid red;. Without solid (or dashed, dotted…) there is no border at all. Shorthands save typing: padding: 10px 20px; is 10px top and bottom, 20px left and right.
Worked example: a 230px box
CSS:
.box { width: 200px; padding: 10px; border: 5px solid navy; margin: 20px; }HTML:
<div class="box">A box</div>
The lesson, step by step
| Part | Time | How it is checked |
|---|---|---|
| Starter | 5 min | Graded by Classfern |
| Learn | 8 min | Read, watch and talk |
| See it | 7 min | Graded by Classfern |
| Practice | 15 min | Graded by Classfern |
| Check and reflect | 5 min | Graded by Classfern + self-check |
The lesson
It runs here on the page. No sign-up and no names – nothing you type is sent anywhere. Each visit picks a new mix of questions.
Homework
20 minutes: predict what the browser will do, build or fix a short page, and check the key ideas – then a short reflection. New questions every time, all graded instantly.
Practice tests
- The box model and flexbox test12 questions · graded instantly
- Mixed HTML and CSS test20 questions · graded instantly
- Mixed HTML and CSS test20 questions · graded instantly
All computer science practice tests · All 20 HTML and CSS lessons
Curriculum
CSTA K–12 Computer Science Standards (2017), grades 6–8: 2-AP-13 – Decompose problems to help design, implement and review programs.
The standard codes are CSTA's; the descriptions are our own words.
Our lessons and questions are our own. They are not written or endorsed by any government department, testing agency or exam board. HTML and CSS are open web standards; Classfern is not affiliated with the organisations that publish them.