CSS shape-margin Property
The CSS shape-margin property adds extra space around a shape defined by shape-outside, controlling the distance between the shape and surrounding text.
Unlike the regular margin property, which adds space around an element's rectangular box, shape-margin follows the calculated shape boundary. It is useful for improving text readability around circles, ellipses, polygons, and image-based shapes.
shape-margin Syntax
The shape-margin property accepts a single non-negative length or percentage.
Basic Syntax
selector {
shape-margin: 20px;
}
Length Values
.shape {
shape-margin: 0;
}
.shape {
shape-margin: 10px;
}
.shape {
shape-margin: 1rem;
}
.shape {
shape-margin: 2em;
}
Percentage Values
.shape {
shape-margin: 5%;
}
.shape {
shape-margin: 10%;
}
CSS-Wide Keywords
.shape {
shape-margin: initial;
}
.shape {
shape-margin: inherit;
}
.shape {
shape-margin: unset;
}
.shape {
shape-margin: revert;
}
.shape {
shape-margin: revert-layer;
}
Negative values are not permitted. A value of 0 adds no extra space around the shape.
Property Values
| Value | Description |
|---|---|
0 |
Adds no extra spacing around the shape. This is the initial value. |
<length> |
Specifies a non-negative distance using units such as px, em, or rem. |
<percentage> |
Specifies spacing as a percentage of the width of the floated element's containing block. |
initial |
Resets the property to 0. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to 0 because it is not inherited. |
revert |
Reverts the property according to CSS cascade origin rules. |
revert-layer |
Reverts the property according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | 0 |
| Inherited | No |
| Applies to | Floats |
| Computed value | Absolute length or percentage |
| Animation type | By computed value type (length or percentage) |
| Percentages | Relative to the width of the containing block |
| Related properties | shape-outside, shape-image-threshold |
| Specification | CSS Shapes Module Level 1 |
Valid Values
.shape {
shape-margin: 0;
}
.shape {
shape-margin: 15px;
}
.shape {
shape-margin: 1.5rem;
}
.shape {
shape-margin: 10%;
}
.shape {
shape-margin: calc(10px + 1vw);
}
.shape {
shape-margin: initial;
}
Invalid Values
.shape {
shape-margin: -10px;
}
.shape {
shape-margin: auto;
}
.shape {
shape-margin: 10px 20px;
}
.shape {
shape-margin: normal;
}
.shape {
shape-margin: red;
}
The property accepts one non-negative length or percentage. It does not support negative spacing, multiple values, or color keywords.
How It Works
The shape-margin property expands the exclusion area surrounding a CSS shape. This changes where inline content can flow without changing the floated element's actual dimensions.
Step 1: Create a Floated Element
.shape {
float: left;
width: 180px;
height: 180px;
}
CSS Shapes control text wrapping around floated elements.
Step 2: Define the Shape
.shape {
shape-outside: circle(50%);
}
Text can wrap around the circular boundary instead of only following the rectangular float.
Step 3: Add Shape Margin
.shape {
shape-margin: 20px;
}
The wrapping boundary expands outward by the specified distance, subject to the float's margin box.
Step 4: Observe Text Wrapping
Surrounding text follows the expanded shape boundary, leaving additional space around the circle.
Complete Example
.shape {
float: left;
width: 180px;
height: 180px;
shape-outside: circle(50%);
shape-margin: 20px;
}
Important Consideration
The expanded shape is limited by the floated element's margin box. Increasing shape-margin does not allow the shape to extend beyond that boundary.
Length Values
Length values specify a distance around the calculated shape using CSS length units.
Pixels
.shape {
shape-margin: 15px;
}
Creates a fixed spacing distance around the shape.
rem Units
.shape {
shape-margin: 1rem;
}
Uses a distance based on the root element's font size.
em Units
.shape {
shape-margin: 1.5em;
}
Uses a distance relative to the element's font size.
Viewport Units
.shape {
shape-margin: 2vw;
}
Uses a distance based on the viewport width.
Calculated Values
.shape {
shape-margin: calc(10px + 1vw);
}
Combines compatible length values into a calculated spacing distance.
Important Consideration
Large values may not create visibly larger gaps when the expanded shape reaches the floated element's margin-box boundary.
Percentage Values
Percentage values are calculated relative to the width of the floated element's containing block.
Basic Example
.shape {
shape-margin: 5%;
}
Percentage Calculation
If the containing block is 600 pixels wide, a 5% shape margin represents 30 pixels.
Responsive Spacing
.shape {
shape-margin: 3%;
}
The spacing adjusts as the containing block's width changes.
Percentage vs. Pixel Values
| Value | Behavior |
|---|---|
15px |
Uses a fixed distance of 15 pixels. |
5% |
Uses 5% of the containing block's width. |
1rem |
Uses a distance relative to the root font size. |
Important Consideration
Percentage values are not calculated from the shape's diameter, perimeter, or image dimensions.
Working With shape-outside
The shape-margin property is designed to work with shapes defined by shape-outside.
Circular Shape
.circle-shape {
float: left;
width: 200px;
height: 200px;
shape-outside: circle(50%);
shape-margin: 15px;
}
Text wraps around the circular shape with additional spacing.
Elliptical Shape
.ellipse-shape {
float: left;
width: 220px;
height: 160px;
shape-outside: ellipse(50% 45%);
shape-margin: 12px;
}
The margin follows the elliptical boundary.
Polygon Shape
.polygon-shape {
float: left;
width: 200px;
height: 200px;
shape-outside: polygon(50% 0, 100% 100%, 0 100%);
shape-margin: 15px;
}
The margin expands the exclusion area around the polygon.
Image-Based Shape
.image-shape {
float: left;
width: 200px;
height: 200px;
shape-outside: url("/img/transparent-shape.png");
shape-margin: 15px;
}
The margin follows the shape calculated from the image's alpha channel.
Important Consideration
The shape must be applied to a floated element. The property does not control text wrapping around ordinary non-floated elements.
Working With shape-image-threshold
When shape-outside uses an image, shape-image-threshold determines which alpha values contribute to the shape boundary.
Combining the Properties
.image-shape {
float: left;
width: 200px;
height: 200px;
shape-outside: url("/img/transparent-shape.png");
shape-image-threshold: 0.5;
shape-margin: 15px;
}
The threshold determines the image-based shape, and shape-margin adds spacing around that calculated boundary.
Lower Threshold
.image-shape {
shape-image-threshold: 0.2;
shape-margin: 15px;
}
More partially transparent pixels contribute to the shape, potentially creating a larger exclusion area.
Higher Threshold
.image-shape {
shape-image-threshold: 0.8;
shape-margin: 15px;
}
Fewer partially transparent pixels contribute to the shape, potentially creating a smaller exclusion area.
Related Property Comparison
| Property | Purpose |
|---|---|
shape-outside |
Defines the wrapping shape. |
shape-image-threshold |
Determines which image pixels contribute to the shape. |
shape-margin |
Adds spacing around the resulting shape. |
Important Consideration
The shape-image-threshold property affects image-based shapes. It does not alter the boundaries of geometric basic shapes such as circle() or polygon().
shape-margin vs. margin
Although both properties can affect spacing around floated content, they operate on different boundaries.
Regular margin
.shape {
float: left;
margin-right: 20px;
}
The regular margin adds space around the element's rectangular box.
shape-margin
.shape {
float: left;
shape-outside: circle(40%);
shape-margin: 20px;
}
The shape margin adds space around the calculated circular boundary, within the float's margin box.
Combining Both Properties
.shape {
float: left;
width: 180px;
height: 180px;
margin-right: 20px;
shape-outside: circle(45%);
shape-margin: 15px;
}
The regular margin affects the float's rectangular margin box, while shape-margin expands the wrapping shape inside that box.
Comparison Table
| Feature | margin | shape-margin |
|---|---|---|
| Boundary | Rectangular element box | Calculated CSS shape |
| Multiple sides | Supports individual sides | Uses one value around the shape |
| Negative values | Allowed in many cases | Not allowed |
| Requires shape-outside | No | Needed for a useful shape-based effect |
| Applies to | Most elements | Floated elements |
Important Consideration
Use regular margins to control box spacing and shape-margin to control spacing around a CSS wrapping shape.
Practical Examples
Circle With Extra Spacing
.circle-shape {
float: left;
width: 180px;
height: 180px;
shape-outside: circle(45%);
shape-margin: 15px;
}
Adds spacing around a circular wrapping shape.
Ellipse With Relative Spacing
.ellipse-shape {
float: right;
width: 220px;
height: 160px;
shape-outside: ellipse(50% 40%);
shape-margin: 1rem;
}
Uses root-relative spacing around an elliptical shape.
Polygon With Percentage Spacing
.polygon-shape {
float: left;
width: 200px;
height: 200px;
shape-outside: polygon(50% 0, 100% 100%, 0 100%);
shape-margin: 3%;
}
Uses percentage-based spacing around a triangular shape.
Transparent Image With Shape Margin
.image-shape {
float: left;
width: 200px;
height: 200px;
shape-outside: url("/img/transparent-shape.png");
shape-image-threshold: 0.5;
shape-margin: 12px;
}
Provides extra spacing around an image-based wrapping boundary.
Responsive Shape Margin
.shape {
shape-margin: 10px;
}
@media (min-width: 992px) {
.shape {
shape-margin: 20px;
}
}
Increases shape spacing on wider screens.
Using a CSS Custom Property
.shape {
--shape-spacing: 15px;
shape-margin: var(--shape-spacing);
}
Allows the spacing to be managed through a custom property.
Calculated Shape Margin
.shape {
shape-margin: clamp(10px, 2vw, 25px);
}
Uses responsive spacing constrained by minimum and maximum values.
Resetting Shape Margin
.shape {
shape-margin: 0;
}
Removes the additional spacing around the calculated shape.
shape-margin Example
This example compares three circular shapes with different shape-margin values. The circles have the same dimensions and shape boundaries, but the surrounding text wraps at different distances.
HTML Example
<div class="shape-margin-demo">
<div class="shape-panel">
<h3>shape-margin: 0</h3>
<div class="circle no-margin"></div>
<p>This text wraps around the circular shape without additional shape spacing. The words follow the calculated boundary as closely as the layout permits.</p>
</div>
<div class="shape-panel">
<h3>shape-margin: 15px</h3>
<div class="circle medium-margin"></div>
<p>This text wraps around the circular shape with additional spacing. The shape margin creates more separation between the circle and the surrounding words.</p>
</div>
<div class="shape-panel">
<h3>shape-margin: 30px</h3>
<div class="circle large-margin"></div>
<p>This text wraps around the circular shape with a larger spacing value. The increased exclusion area pushes the text farther away from the circle.</p>
</div>
</div>
CSS Example
.shape-margin-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 260px), 1fr));
gap: 15px;
}
.shape-margin-demo .shape-panel {
min-height: 300px;
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
display: flow-root;
}
.shape-margin-demo .shape-panel h3 {
margin-top: 0;
font-size: 1rem;
}
.shape-margin-demo .circle {
float: left;
width: 150px;
height: 150px;
border-radius: 50%;
background-color: #2563eb;
shape-outside: circle(35%);
}
.shape-margin-demo .no-margin {
shape-margin: 0;
}
.shape-margin-demo .medium-margin {
shape-margin: 15px;
}
.shape-margin-demo .large-margin {
shape-margin: 30px;
}
shape-margin: 0
This text wraps around the circular shape without additional shape spacing. The words follow the calculated boundary as closely as the layout permits.
shape-margin: 15px
This text wraps around the circular shape with additional spacing. The shape margin creates more separation between the circle and the surrounding words.
shape-margin: 30px
This text wraps around the circular shape with a larger spacing value. The increased exclusion area pushes the text farther away from the circle.
The first panel has no additional shape spacing. The second expands the circular wrapping boundary by 15 pixels, while the third requests a 30-pixel expansion.
Comparing the Results
| Value | Wrapping Behavior |
|---|---|
0 |
Text follows the original calculated shape boundary. |
15px |
Text wraps around an expanded shape boundary. |
30px |
Text wraps around a larger exclusion area, limited by the float's margin box. |
Increasing the Available Shape Area
.shape-margin-demo .circle {
shape-outside: circle(30%);
shape-margin: 20px;
}
Using a smaller base circle can leave additional room for shape-margin expansion within the float's margin box.
Changing the Shape
.shape-margin-demo .circle {
shape-outside: ellipse(40% 30%);
}
The shape margin follows the elliptical boundary instead of the original circle.
Resetting the Margin
.shape-margin-demo .circle {
shape-margin: 0;
}
All three panels return to the original shape boundary without additional shape spacing.
Accessibility Considerations
Shape margins can improve the readability of text wrapped around decorative or informative images.
Maintain Comfortable Text Spacing
Provide enough space between text and irregular shape boundaries to avoid a crowded appearance.
Avoid Narrow Text Columns
Large shapes and shape margins can leave insufficient room for readable lines of text.
Preserve Logical Reading Order
CSS Shapes change visual wrapping without changing the document's source order. Keep the HTML structure meaningful.
Test Text Zoom
Verify that content remains readable when users enlarge text or zoom the page.
Responsive Layouts
Disable complex wrapping when narrow screens leave insufficient space for text.
@media (max-width: 600px) {
.shape {
float: none;
shape-outside: none;
shape-margin: 0;
}
}
Provide Alternative Text
Images that convey information should include appropriate alternative text. Decorative images can use empty alternative text.
Avoid Decorative Interference
Use shapes to enhance presentation without making the content difficult to read or navigate.
Common Mistakes
Using Negative Values
.shape {
shape-margin: -10px;
}
Negative values are invalid. Use 0 or a positive length or percentage.
Using Multiple Values
.shape {
shape-margin: 10px 20px;
}
Unlike regular margins, shape-margin accepts only one value.
Using auto
.shape {
shape-margin: auto;
}
The auto keyword is not supported.
Forgetting shape-outside
The property needs a defined wrapping shape to produce its intended effect.
Forgetting float
CSS Shapes control text wrapping around floated elements, not ordinary non-floated elements.
Expecting Shape Margin to Change Element Size
The property changes the text exclusion boundary, not the element's width or height.
Confusing shape-margin With margin
Regular margins affect rectangular box spacing. Shape margins affect the calculated wrapping shape.
Ignoring the Margin-Box Limit
The expanded shape cannot extend beyond the floated element's margin box.
Using Excessively Large Values
Large values may not produce additional visible spacing once the expanded shape reaches its permitted boundary.
Assuming Inheritance
The property is not inherited. Apply it directly to each floated shape that needs additional spacing.
Ignoring Responsive Layouts
Shapes and margins that work on wide screens may leave too little space for text on narrow screens.
Important Considerations
Keep the following points in mind when using shape-margin:
- It adds spacing around a CSS wrapping shape.
- The initial value is
0. - The property is not inherited.
- It applies to floated elements.
- It works with shapes defined by
shape-outside. - It accepts one non-negative length or percentage.
- Negative values are not allowed.
- Percentage values are relative to the containing block's width.
- The property does not change the element's actual dimensions.
- The expanded shape is limited by the float's margin box.
- It works with circles, ellipses, polygons, and image-based shapes.
- It can be combined with
shape-image-threshold. - It differs from the regular
marginproperty. - Large values may reduce the space available for surrounding text.
- CSS custom properties can simplify repeated spacing values.
- Responsive media queries can adjust shape spacing.
- Complex wrapping should be tested with text zoom and narrow screens.
- Shapes should improve visual presentation without reducing readability.
Browser Support
The CSS shape-margin property is widely available in modern browsers, including current versions of Chrome, Edge, Firefox, and Safari.
Its visible effect depends on support for CSS Shapes, the presence of a floated element, and a valid shape-outside declaration.
For current compatibility information, visit Can I Use? and search for shape-margin.
Feature Detection
@supports (shape-margin: 10px) {
.shape {
shape-margin: 10px;
}
}
Feature queries can apply shape spacing when the browser recognizes the property.
Fallback Behavior
When unsupported, the browser ignores the declaration. The remaining float and shape rules continue to determine text wrapping.
Testing Recommendations
Test different shape types, container widths, and text sizes to confirm that the wrapping remains readable.
Specifications
The shape-margin property is defined in the CSS Shapes Module Level 1 specification.
The specification defines how additional spacing expands the calculated shape boundary while remaining constrained by the float's margin box.
For additional documentation and examples, consult MDN Web Docs.
