CSS and layout

5 concepts · 10 practice questions

The box model

Every element is a box of content, padding, border and margin. With the default box-sizing: content-box, width sets only the content, so padding and border are added on top; with border-box, width includes padding and border, which is why most resets apply it everywhere. Vertical margins between block siblings collapse: two adjacent margins become the larger one, not their sum. Margins do not collapse inside flex or grid containers.

*, *::before, *::after { box-sizing: border-box; }

.card {
  width: 300px;      /* total width stays 300px */
  padding: 16px;
  border: 1px solid;
}
h2 { margin-bottom: 24px; }
p  { margin-top: 16px; } /* gap between them is 24px, not 40px */

Also covered

Specificity and cascade layers
Flexbox and grid
Responsive design
Stacking contexts and z-index

Sign in to read the rest and take the 10-question test.

Sign in to continue