Boss lesson · Unit 02
The box model
Work out the real size of a box from its CSS
What you practise
- Content, padding, border and margin
box-sizing- Widths worked as arithmetic
- Collapsing margins
px,rem,%and viewport units
Try a question from this lesson
Pick one
How wide is this card on screen, from the outside of its left border to the outside of its right border?
.card {
width: 200px;
padding: 20px;
border: 5px solid;
}Hint
Padding and border each appear on both sides.
Correct answer
250px
Why
By default width is the width of the content only. Padding and border are added outside it, on both sides: 200 + 20 + 20 + 5 + 5 = 250. The box is wider than the number you wrote.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Four layers
Every element is a box of four layers. Content is the text or image. Padding is space inside the border, and it shows the background. Margin is empty space outside the border.
By default width and height size the content layer only.
+---------- margin ----------+
| +------- border -------+ |
| | +--- padding ---+ | |
| | | content | | |
| | +---------------+ | |
| +----------------------+ |
+----------------------------+Concept
Units
px is a fixed size. % is a share of the parent: for width, of the parent's content width.
rem is a multiple of the root font size, 16px unless the page or the user changes it. em is a multiple of the element's own font size.
1vw is 1% of the viewport's width and 1vh is 1% of its height.
.a { width: 320px; }
.b { width: 50%; }
.c { padding: 1.5rem; }
.d { height: 100vh; }10 more exercises in the app
- 5× type the answer
- 1× fill the blanks
- 3× multiple choice
- 1× true or false
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.