Box model layers
Also called padding, margin, border, content.
From the inside out: content, padding, border, margin. The background fills the content and the padding; the margin is outside the box and transparent.
Careful: Padding and margin are not the same space. To move text away from a box's edge, use padding; to move boxes apart, use margin.
Taught in Every element is a box
Width of a box with the default sizing
Also called content-box, rendered width, total width.
- width across the borderpx
- the
width value, the contentpx - padding on each sidepx
- border width on each sidepx
Applies: The box has the default box-sizing: content-box and the same padding and border on the left and right.
Careful: With box-sizing: border-box, and the content is . Margin is never part of the width.
Taught in How wide is the box?
Border-box sizing
Also called box-sizing border-box, universal box-sizing.
width becomes the whole box across its border, and the padding and border fit inside it.
* {
box-sizing: border-box;
}
Careful: The content gets narrower, not the box wider. Without this rule, width sets the content only.
Taught in How wide is the box?
Margin collapse
Also called collapsing margins, gap between paragraphs.
When a block's bottom margin meets the top margin of the block below, the gap is the larger of the two, not their sum.
Careful: Only vertical margins of blocks in normal flow collapse. Left and right margins add up, and margins inside flex and grid containers never collapse.
Taught in Margins: gaps and centering
Centering a block
Also called margin auto, center a div horizontally.
The two auto margins split the free space, so the block sits in the middle, left to right.
.column {
max-width: 600px;
margin: 0 auto;
}
Careful: The block needs a width or max-width narrower than its container. text-align: center centers the text inside, not the box.
Taught in Margins: gaps and centering
Block, inline and inline-block
Also called display, inline box.
div, p, h1 and li are blocks; span, a and em are inline; button and input are inline-block.
| display | New line? | Takes width and height? |
|---|
block | yes | yes |
inline | no | no |
inline-block | no | yes |
none | no box | no box |
Careful: An inline box's top and bottom margins move nothing. To size a link or a span, change its display.
Taught in Block and inline
em and rem
Also called relative units, compounding em, root font size.
- the length that resultspx
- the number before
em or remno unit - for em, the element's own font size (the parent's, on
font-size); for rem, the root font size, 16 by defaultpx
Applies: Any length written in em or rem. For text, use rem: it follows the size a reader sets in the browser.
Careful: Ems compound when elements nest: 20 px, then 1.5em gives 30 px, then 45 px. A rem gives the same size at every level.
Taught in Lengths: px, %, em and rem
Contrast minimums
Also called WCAG contrast, 4.5 to 1, color contrast.
The ratio runs from 1:1 (the same color) to 21:1 (black on white) and is computed from the two colors. #767676 on white is 4.54:1; #777777 is 4.48:1.
| Text | Minimum ratio |
|---|
| Body text | 4.5:1 |
| Large text: 24 px, or about 18.7 px bold | 3:1 |
Careful: Nothing is rounded: 4.48:1 fails. The ratio depends on the background too, and color alone must never carry meaning.
Taught in Color and contrast
Hex, rgb and hsl
Also called hex color, color notation.
#ffcc00 is red ff (255), green cc (204), blue 00; the same yolk yellow is rgb(255, 204, 0) and hsl(48, 100%, 50%), a hue angle, a saturation and a lightness.
Careful: The pairs go red, green, blue. In hsl() the third number is the lightness: lower it for a darker shade of the same hue.
Taught in Color and contrast
Font stack and line height
Also called font-family fallback, unitless line-height.
The browser uses the first font in the list that the device has; the generic family at the end is always there. A unitless line height is passed down as a factor of each element's own size.
body {
font-family: Georgia, serif;
line-height: 1.5;
}
Careful: line-height: 24px passes a fixed 24 px to every element, so a 32 px heading gets cramped lines.
Taught in Readable text
Images that fit
Also called responsive image, max-width 100%.
The image shrinks to the width of its container and keeps its shape, but never stretches past its own size.
img {
max-width: 100%;
height: auto;
}
Careful: width: 100% stretches small images too. A fixed width wider than the screen makes the page scroll sideways.
Taught in Keeping content in its box
Percentage padding and margin
Also called padding-top percent, aspect box.
- the padding or marginpx
- the percentageno unit
- the width of the containing blockpx
Applies: Any padding or margin written in %, on any side.
Careful: Top and bottom percentages use the container's width too, not its height: padding-top: 10% in a 300 px wide box is 30 px.
Taught in Keeping content in its box