All topics › Unit 3 · Cascading Style Sheets
Topic 28 · 28 of 43
The Box Model
All in one: every box-model property, side by side
What this example compares
- The four layers: content vs
paddingvsbordervsmargin, added one at a time - The same 26px, two jobs:
padding: 26px(coloured) vsmargin: 26px(see-through) - Border styles:
solidvsdashedvsdottedvsdoublevsnone, andborder: 6px #E07B1Ewith the style missing - Which sides:
bordervsborder-left,-top,-right,-bottomvsborder-width+border-style+border-color, in any order - Round corners:
border-radius0vs8pxvs999pxvs50%on a square and on a wide box vs four values - Shadows:
box-shadownone vs small vs big vs hard vs spread, andtext-shadow - A line that takes space or not:
bordervsoutline, and the focus outline vsoutline: none - How many values:
padding: 10pxvs10px 20pxvs10px 20px 30pxvs5px 10px 15px 20px(TRouBLe), andborder-width: 2px 10px - One side by name:
padding-topvsmargin-leftvsborder-bottom-widthvs the shorthand - How wide is it really:
box-sizing: content-box(250px) vsborder-box(200px), and* { box-sizing: border-box; } - The gap between two boxes: margins collapse vs the larger wins vs no collapse in a grid vs sideways margins vs a negative margin
- Centring a box:
margin: 0 autowith a width vs without one vs on a<span>vs withdisplay: blockvstext-align: centervs trying to centre vertically - Sizes:
widthvsmax-widthvsmin-widthvsheightvsmin-heightvsheight: auto - All of it in one real card: the project's course cards, every piece named
Point at any word in the code to see what it means (tap on a phone); click a word to keep its meaning on screen. Read the comment above each numbered panel, press Run, then Edit & experiment to try the TRY lines yourself.
Loading the example…
What you should see
You should see fourteen panels (scroll inside the result to reach them all). Panel 1 shows the same words getting a cream padding, then an orange border, then grey all round from the margin. Panel 3 shows five border styles and one box with no border at all because its style is missing. Panel 5 shows a square, a soft card, a pill, a circle, an oval and a leaf shape. In panel 7 chip C moves to the right only in the border row, not in the outline row. Panel 10 shows one box reaching the end of its 250px red line and one stopping 50px short. In panel 11 the gap between the two strips is one strip high in the first card and two strips high in the grid card. Panel 13 shows a bar that runs off the pretend phone and words spilling out of the fixed-height box, and panel 14 shows the two course cards from the project. There is no JavaScript on this page, so the Console prints nothing.
Quick check: in panel 12, why does the box in card 2 not move to the centre, even though it has margin: 0 auto exactly like card 1?
Card 2 has no width, so the box already fills the whole line and there is no leftover space for the two auto margins to share. Give it a width (or max-width) smaller than its parent, as card 1 does, and it moves to the centre.