Lesson 1
Padding, border and margin
Every element is a box. Here’s what it’s made of.
Every element is a box
The browser draws every element as a rectangle with four layers, from the inside out:
- content: the text or picture itself
- padding: space inside the box, between the content and the border
- border: a line around the box
- margin: space outside the box, pushing other boxes away
Padding takes the box’s background color; margin is always see-through.
.card {
background-color: #E1EAFE;
padding: 16px;
border: 3px solid #2965F1;
margin: 20px;
}
One side, or all four
padding: 10px;is the same on all four sides
padding: 10px 20px;is 10px top and bottom, 20px left and right
padding-left: 30px;sets just one side (there’s-top,-right,-bottomtoo)
Margin works exactly the same way. And border takes three things: a width, a style (solid, dashed, dotted) and a color. border-radius: 12px; rounds the corners.
Lesson 2
Sizes and units
width, height, px, %, rem and box-sizing.
How big is a box?
width and height set the size of a box. The units matter:
px: pixels, a fixed size
%: a share of the box it’s inside:width: 50%is half its parent
rem: a multiple of the page’s font size (usually 16px), so2remis 32px. Good for text and spacing, because it grows when someone makes their text bigger
max-width: 600px; lets a box shrink on small screens but never grow wider than 600px.
.half {
width: 50%;
}
.box {
max-width: 300px;
padding: 1rem;
}
box-sizing: border-box
By default, width is only the content. Add padding: 20px and a 5px border to a width: 200px box, and it’s really 250px wide. Surprise!
box-sizing: border-box; makes width include the padding and border, so a 200px box stays 200px. Most developers put this at the top of every stylesheet:
* {
box-sizing: border-box;
}
Tip: * is the universal selector: it picks every element.
Lesson 3
Block, inline and none
How boxes flow, and how to hide one.
Two kinds of box
- Block elements (
<p>,<h1>,<div>,<li>) start on a new line and stretch across the whole width
- Inline elements (
<a>,<strong>,<em>,<span>) flow along inside a line of text, and ignorewidthandheight
You can switch: display: block;, display: inline;, or display: inline-block; (flows like text, but takes width, height and padding properly, which is great for buttons).
display: none; hides an element completely, as if it wasn’t there.
a.button {
display: inline-block;
padding: 8px 16px;
background-color: #2965F1;
color: white;
}
.secret {
display: none;
}
Recap
What Level 3 covers, in a few lines.
- Every element is a box. From the inside out: the content, then padding (space inside the border), the border, and margin (space outside, pushing other boxes away).
padding: 10px 20px;is 10px top and bottom, 20px left and right.border: 2px solid navy;is width, style and color.- With
box-sizing: border-box,widthincludes the padding and border, which makes sizes much easier to plan. - Units:
pxare fixed;%is a share of the parent;remis a multiple of the page’s font size. display: blockboxes start on a new line and fill the width.inlineones flow inside text.nonehides an element completely.
Now try it in CSS School
Write real code and watch your page change as you type, with a check after every step and a test at the end of each level. Free.