The box model — the idea the whole language rests on
CSS lays every element out as four concentric layers, and the box you see is width + padding + border.
- content (
width×height) sits at the centre - padding is space inside the border; border is the edge; margin is the gap outside it
- A
200pxbox with20pxpadding and a2pxborder is really244pxwide box-sizing: border-boxmakeswidthinclude the padding and border — set it everywheremin/max-widthbound the size;overflow: hiddenclips,overflow: scrolladds a scrollbar
Borders, padding, and margins in practice
A border has three sub-properties, and padding and margin take one value per side or a clockwise shorthand.
border-style,border-width,border-color— or thebordershorthand:5px solid redborder-radiusrounds the corners- Shorthand runs clockwise from the top:
padding: 25px 50px 75px 100pxis top, right, bottom, left visibility: hiddenhides the box but keeps its spacedisplay: noneremoves the box and its space entirely
Lists and tables
List markers and table borders reuse tools you know, plus a few of their own.
list-style-type(disc, square, decimal, upper-roman…),list-style-image,list-style-position- The
list-styleshorthand sets all three at once border-collapse: collapsemerges doubled borders into single linesborder-spacingsets the gap when borders stay separateempty-cells: hidesuppresses borders around blank cellstr:hover { background-color: … }highlights the row under the cursor
Forms and images
Form controls are just boxes, and images can be sized and floated.
- Inputs take
font-size,background-color,border,border-radius, and:hover - Add
box-sizing: border-boxso awidth: 100%input doesn't overflow cursorchanges the pointer —pointer,text,move,wait,help- Size images with
width/height;float: left/rightlets text wrap around them background-image+background-repeattile a picture; thebackgroundshorthand collapses colour, image, and repeat