liveBook · Manning

Margin property

As explained in Web Design Playground, Second Edition

The CSS margin property specifies the space outside an element's border.

The CSS margin property controls the whitespace outside an element’s border. Think of it as breathing room between neighboring elements or between an element and the browser window. Positive values push elements apart; negative values pull them closer and can create overlap. section 9.5

Margin is one part of the CSS box model: content sits inside padding, padding sits inside the border, and margin surrounds the border. Unlike padding, margin changes the space between boxes rather than the space inside one. section 9.1

Apply margin to individual sides or all sides at once:

  • Use margin-top, margin-right, margin-bottom, and margin-left when each side needs its own value. section 9.5
  • Use the shorthand margin property when one declaration can describe the four sides. section 9.5 section 9.7
  • Choose standard CSS units such as px, em, rem, vw, vh, percentages, or auto. section 9.5
  • Set a paragraph’s bottom margin to 1em as a general starting point for text spacing. section 9.5

A pull quote with custom margins

.pullquote { margin: 1.5em 2.5em 2em 3em; } The four values apply clockwise: top, right, bottom, and left. This gives the pull quote different breathing room on every side. section 9.5

Figure 9.1 The main parts of an element box

Use the element-box diagram to see margin as the outermost layer surrounding the border. section 9.1

Browsers apply default margins through their internal style sheet, so a reset can remove those defaults when precise control is needed. Responsive layouts commonly express margins with percentages or rem units rather than fixed pixels. section 9.5 section 14.12

How Web Design Playground, Second Edition covers it

How deep it goes
Explains the box-model role, side properties, units, negative values, shorthand order, defaults, and resets.
Examples
Uses paragraph spacing, a pull quote, navigation lists, surrounding content, and a four-sided shorthand declaration.
What it assumes
Assumes basic familiarity with CSS elements while building understanding from the element box and its four layers.
What you can do after
Apply margin declarations and shorthand to create spacing, separation, intentional overlap, and responsive layouts.
How it connects
Connects margin with the CSS box model, padding, borders, element sizing, CSS resets, and responsive web design.

Also explained in

Compare how 5 books explain margin property →

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.