liveBook · Manning

CSS box model

As explained in Web Design Playground, Second Edition

The CSS box model represents an element's dimensions as nested content, padding, border, and margin areas.

The CSS box model describes every element as four nested areas: content, padding, border, and margin. Content holds text or an image; padding adds space around it; the border surrounds that space; and the margin separates the whole box from its neighbors. The model applies to block elements such as div, h1, and p, as well as inline elements such as span, em, and a. It helps control both appearance and positioning by organizing whitespace and where boxes appear on the page. section 9.1

Think of the box from the inside out:

  • Content: the inner rectangle containing text or an image. section 9.1
  • Padding: extra whitespace between the content and border. section 9.1
  • Border: the line surrounding the content and padding. section 9.1
  • Margin: extra whitespace outside the border, separating the element from other boxes. section 9.1

Example: tame a very long line

A containing div with a width of 630px can make text easier to scan when the default line length stretches beyond 150 characters. For sizing behavior, CSS can also use box-sizing: border-box; if a set height causes content to spill outside its box, the overflow property controls that behavior. section 9.2

Figure 9.1 The main parts of an element box

Use the element-box diagram to see how content, padding, border, and margin nest together. section 9.1

How Web Design Playground, Second Edition covers it

How deep it goes
Defines the four box areas and begins applying width, height, box-sizing, and overflow, without developing full page layout.
Examples
Uses a nested-box diagram and a 630px containing div to demonstrate box anatomy and more readable line lengths.
What you can do after
Apply CSS dimensions, spacing, borders, and overflow controls to shape element boxes and improve page organization.
How it connects
Connects content, padding, border, margin, width, height, box-sizing, overflow, whitespace, and positioning.

Also explained in

Compare how 6 books explain CSS box model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.