π What Is shape-margin?
The shape-margin property adds space around a CSS shape defined byshape-outside.
It creates a buffer between the wrapping text and the shape, preventing the text from touching the shapeβs edges.
Think of it like the βpaddingβ that surrounds your wrap shape. π§β¨
π§© Basic Syntax
Syntax
shape-margin: <length> | <percentage>;
shape-margin: 10px;
shape-margin: 2rem;
shape-margin: 5%;β Applies only when using shape-outside
β Controls spacing outside the shape
β Accepts px, em, rem, %, etc.
π― Why Use shape-margin?
- Prevents text from hugging the edge of the shape
- Makes editorial layouts more readable
- Improves breathing room in bubble or silhouette designs
- Essential when shape-outside uses images (irregular edges)
π§ͺ Example: Adding Spacing Around a Circle
Circle margin
.circle {
float: left;
width: 200px;
height: 200px;
shape-outside: circle(50%);
shape-margin: 20px;
clip-path: circle(50%);
background: url(profile.jpg) center/cover;
}β Text wraps around the circle with a 20px buffer
β Prevents tight or awkward text flow
π§ͺ Example: shape-margin with Polygon
Polygon margin
.triangle {
float: right;
width: 200px;
height: 200px;
shape-outside: polygon(50% 0%, 0% 100%, 100% 100%);
shape-margin: 15px;
background: coral;
}β Text wraps around the triangle
β margin smoothens sharp edges
π§ͺ Example: Using shape-margin with Image-Based Shapes
Image mask shape
.shape-img {
float: left;
width: 250px;
height: 250px;
shape-outside: url(star.png);
shape-margin: 12px;
background: url(star.png) center/contain no-repeat;
}β Critical for irregular shapes
β Ensures text doesnβt overlap unpredictable contours of the image
π§ͺ Example: Percentage shape-margin
Percentage margin
.ellipse {
float: left;
width: 250px;
height: 200px;
shape-outside: ellipse(50% 40%);
shape-margin: 5%;
}β Percentage margin scales with element size
β Useful for responsive designs
π shape-margin + clip-path for Matching Visuals
shape-outside + clip-path combo
.blob {
float: right;
width: 260px;
height: 260px;
shape-outside: path("M50,15 Q25,0 0,30 T50,60 T100,30 Q75,0 50,15 Z");
shape-margin: 20px;
clip-path: path("M50,15 Q25,0 0,30 T50,60 T100,30 Q75,0 50,15 Z");
}β clip-path controls the actual visible shape
β shape-outside creates wrapping flow around it
β shape-margin keeps text safely away from irregular edges
π οΈ Best Practices
- Always include shape-margin when wrapping text around curves
- Use clip-path to visually match the shape-outside boundary
- Use percentages for responsive layouts
- Increase shape-margin when using jagged or complex image shapes
- Use shape-margin = 0 only when you intentionally want tight wrap (rare)
β οΈ Browser Support
- β Chrome
- β Edge
- β Safari
- β οΈ Firefox: partial support (image shapes not supported)
Note
π Debugging Tips
- If the margin has no effect β ensure float is applied!
- Check if width & height are explicitly set on the floated shape
- Use DevTools to preview the wrapping shape outline
- If using PNG/SVG shapes, ensure they contain transparency
- Increase shape-margin gradually until the text spacing feels natural