The Box Model

Understand how every HTML element is just a box.

Your browser renders every HTML element as a rectangular box according to the standard CSS box model. Each HTML element on your web page consists of a content area, padding, a border, and a margin.

Press + to interact
CSS box model for HTML elements
CSS box model for HTML elements

Learning how to manipulate the CSS properties within the box model will be helpful with laying out content on your web pages.

Relative vs. absolute measurements

We can manipulate the parameters for sizing the “box” that makes up an element with many different CSS properties. Often times, you will want to specify the length of the content area, padding, margins, etc.

But how do we specify these lengths? The most common units used are percentages, %, and the number of pixels, px.

Percentages are a relative measure of length. When specifying length using the % unit, the length is measured as relative to the parent element’s length:

Implementation of relative length measurements

Since we used %, a relative unit of measurement, the length of the h1 elements in the second div are half that of the h1 elements in the first div ...