liveBook · Manning

Margin

As explained in Build a Website with ChatGPT

Margin is the whitespace outside an element's border that separates the element from surrounding content or the viewport edge.

Margin is the whitespace outside an element’s border. It creates separation between an element and nearby content, or between the element and the page edge. Margin is the outermost part of the CSS box model: content sits inside padding, padding sits inside a border, and margin sits outside that border. section 4.2.3 section 4.5

Figure 4.4 The main parts of an element box

Use the box diagram to distinguish margin from padding: padding is inside the border, while margin is outside it. section 4.2.3

Set a different margin for each side with margin-top, margin-right, margin-bottom, and margin-left. Values are measurements such as pixels (px). This is useful when an element needs more space on one side than another. section 4.2.3

Give a footer uneven outer spacing

This rule adds 24px above the footer, 40px to its right, 32px below it, and 48px to its left: footer { margin-top: 24px; margin-right: 40px; margin-bottom: 32px; margin-left: 48px; } The shorthand form produces the same result, with values ordered top, right, bottom, left: footer { margin: 24px 40px 32px 48px; } section 4.2.3

When customizing a page, adjust the number in an existing padding or margin value to increase or decrease space. Keep the px unit in place. Where accessibility is a concern, px measurements can be converted to rem; by default, 16px equals 1rem. section 6.4 section 9.6

Center a constrained page body

A body rule can combine a maximum width, automatic horizontal margins, and padding: body { max-width: 800px; margin: 0 auto; padding: 25px; line-height: 1.5; } Here, margin is one of several values you can customize while keeping the unit attached to numeric spacing values. section 6.4

Quick check: if space is needed between text and its own border, use padding. If space is needed outside the border to separate the whole element from its surroundings, use margin. section 4.2.3

How Build a Website with ChatGPT covers it

How deep it goes
Defines margin within the box model and demonstrates side-specific properties and shorthand without deeper layout behavior.
Examples
Shows footer margins with four longhand declarations and shorthand, plus a body rule using margin: 0 auto.
What it assumes
Introduces margin from zero through the element box and pixel-based CSS declarations.
What you can do after
Apply it via CSS properties to increase or decrease an element’s outer spacing.
How it connects
Connects margin to the CSS box model, content, padding, border, max-width, and rem measurements.

Also explained in

Compare how 2 books explain margin →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Website with ChatGPT. The section links point to the parts of the book it draws on.