Web DesignStyling & layoutLesson 4 / 69

Selectors & the box model

Fill-gap25 min

Selectors & the box model

CSS finds elements with selectors, then paints them:

p { color: firebrick; }        /* every paragraph */
.card { padding: 16px; }      /* class="card" */
#signup { border: 2px solid; } /* id="signup" */

Every element is a box with four layers, from inside out:

content → padding → border → margin

Padding is space inside the border; margin is space outside it. Ninety percent of "why is there a gap" questions are answered by this model — sketch the four layers once and you will never guess again.

Exercise

Add CSS to your page: give body a margin, then a padding, and observe the difference from the outside edge.