liveBook · Manning

Shape-margin

As explained in CSS in Depth, Second Edition

The CSS shape-margin property adds a margin around a shape used for text wrapping.

The CSS shape-margin property adds space around a shape used by shape-outside to control text wrapping. For example, shape-margin: 15px expands the separation around the shape in all directions. section 14.4.2

Listing 14.17 A shape defined by a gradient
.poem-image {

  float: left;
  --gradient: linear-gradient(     #1
    65deg,                         #1
    black 0 375px,                 #1
    transparent 375px);            #1
  mask: var(--gradient);
  shape-outside: var(--gradient);
  shape-margin: 15px;
}

.poem-image > img {
  display: block;
  height: 350px;
  object-position: -100px 0;      #2
}

A gradient can define both the visible mask and the wrapping shape; shape-margin then adds a 15px gap around that diagonal boundary. section 14.4.2

Use shape-margin carefully when the shape approaches the floated element’s edges:

  • It cannot extend beyond the floated element’s margin box. section 14.4.2
  • With a clip path reaching the element edge, wrapping may follow the circular contour near the top and bottom but the straight margin-box edge in the middle. section 14.4.2
  • Prefer it when adequate space separates the shape from the element edges; otherwise, a regular margin may produce a more consistent gap. section 14.4.2

How CSS in Depth, Second Edition covers it

How deep it goes
Explains syntax, directional effect, margin-box limits, and a clipped-edge failure case.
Examples
Uses a 15px gap around a circle and a gradient-defined diagonal shape.
What it assumes
Assumes familiarity with floated elements and shape-outside, while demonstrating the property through circles, clipping, and gradients.
What you can do after
Apply shape-margin to create spacing around text-wrapping shapes and recognize when the margin-box boundary causes uneven results.
How it connects
Connects shape-outside with floats, clip paths, border-radius, masks, gradients, and margin boxes.

Related concepts

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