Tillitsdone
down Scroll to discover

CSS shape-margin Property Enhance Layouts with Precision

Learn about the CSS shape-margin property.

Discover how to use it to set margins for CSS shapes, explore its various options, and enhance your web layouts.
thumbnail

Introduction

The shape-margin property in CSS is a handy tool for web designers. It lets you set a margin around a shape created with the shape-outside property. This feature has been around since January 2020 and works on many devices and browsers. By adjusting the space between the shape and surrounding content, shape-margin makes your web designs more flexible and visually appealing.

Specification

The shape-margin property is part of the CSS Shapes Module Level 1. This module allows you to define geometric shapes and use them to wrap content around.

Description

The shape-margin property sets a margin for a shape created with the shape-outside property. This margin adjusts the space between the shape and the surrounding content. It’s great for creating dynamic and visually appealing designs.

Syntax

The shape-margin property is easy to use. It accepts lengths, percentages, and global values. Here’s the basic syntax:

shape-margin: <length> | <percentage>;

Length Values

You can set the margin using specific lengths, like pixels (px) or millimeters (mm).

shape-margin: 10px;
shape-margin: 20mm;

Percentage Values

You can also set the margin as a percentage of the width of the element’s containing block.

shape-margin: 60%;

Global Values

The shape-margin property supports global values like inherit, initial, revert, revert-layer, and unset.

shape-margin: inherit;
shape-margin: initial;
shape-margin: revert;
shape-margin: revert-layer;
shape-margin: unset;

Values

The shape-margin property accepts a range of values, including lengths, percentages, and global values.

Length Values

  • <length>: Sets the margin to a specific length, like pixels (px) or millimeters (mm).
    shape-margin: 10px;
    shape-margin: 20mm;

Percentage Values

  • <percentage>: Sets the margin as a percentage of the width of the element’s containing block.
    shape-margin: 60%;

Global Values

  • inherit: Inherits the value from the parent element.
    shape-margin: inherit;
  • initial: Sets the property to its initial value, which is 0.
    shape-margin: initial;
  • revert: Reverts the property to the user agent’s default stylesheet value.
    shape-margin: revert;
  • revert-layer: Reverts the property to the value specified in the previous cascade layer.
    shape-margin: revert-layer;
  • unset: Resets the property to its natural value, which means it acts as initial if the property is not inherited, or inherit if it is.
    shape-margin: unset;

Formal Definition

The shape-margin property is formally defined with specific characteristics:

  • Initial Value: 0
  • Applies To: Floats
  • Inherited: No
  • Percentages: Refer to the width of the containing block
  • Computed Value: As specified, but with relative lengths converted into absolute lengths
  • Animation Type: A length, percentage, or calc()

Formal Syntax

The formal syntax for the shape-margin property is:

shape-margin = <length-percentage [0,]>

Where <length-percentage> can be:

  • <length>: A length value (e.g., px, mm)
  • <percentage>: A percentage value

Browser Compatibility

The shape-margin property is widely supported across various browsers:

  • Google Chrome 37
  • Firefox 62
  • Safari 10.1
  • Opera 24

See Also

  • [CSS Shapes]WebsiteUrl
  • [Overview of CSS Shapes]WebsiteUrl
  • [shape-outside]WebsiteUrl
  • [shape-image-threshold]WebsiteUrl
  • [<basic-shape>]WebsiteUrl

Examples

Adding a Margin to a Polygon

HTML
<section>
<div class="shape"></div>
We are not quite sure of any one thing in biology; our knowledge of geology is
relatively very slight, and the economic laws of society are uncertain to
every one except some individual who attempts to set them forth; but before
the world was fashioned the square on the hypotenuse was equal to the sum of
the squares on the other two sides of a right triangle, and it will be so
after this world is dead; and the inhabitant of Mars, if one exists, probably
knows its truth as we know it.
</section>
CSS
section {
max-width: 400px;
}
.shape {
float: left;
width: 150px;
height: 150px;
background-color: maroon;
clip-path: polygon(0 0, 150px 150px, 0 150px);
shape-outside: polygon(0 0, 150px 150px, 0 150px);
shape-margin: 20px;
}

Adding a Margin to a Circle

HTML
<section>
<div class="circle"></div>
The `shape-margin` property is a powerful tool for web development and design.
It allows you to set a margin for a CSS shape created using the `shape-outside`
property. This feature has been widely available since January 2020 and works
across many devices and browser versions. By adjusting the distance between
the edges of the shape and the surrounding content, `shape-margin` enhances
the flexibility and visual appeal of your web designs.
</section>
CSS
section {
max-width: 400px;
}
.circle {
float: left;
width: 150px;
height: 150px;
background-color: maroon;
border-radius: 50%;
shape-outside: circle(50%);
shape-margin: 20px;
}

Adding a Margin to an Ellipse

HTML
<section>
<div class="ellipse"></div>
The `shape-margin` property provides greater control over the layout and appearance of your web pages. It allows you to fine-tune the spacing around custom shapes, ensuring that your content flows smoothly and looks polished.
</section>
CSS
section {
max-width: 400px;
}
.ellipse {
float: left;
width: 200px;
height: 100px;
background-color: green;
clip-path: ellipse(50% 50% at 50% 50%);
shape-outside: ellipse(50% 50% at 50% 50%);
shape-margin: 15px;
}

Using shape-margin for Spacing Around Shapes

HTML
<section>
<div class="circle"></div>
The `shape-margin` property provides greater control over the layout and appearance of your web pages. It allows you to fine-tune the spacing around custom shapes, ensuring that your content flows smoothly and looks polished.
</section>
CSS
section {
max-width: 400px;
}
.circle {
float: left;
width: 100px;
height: 100px;
background-color: blue;
clip-path: circle(50%);
shape-outside: circle(50%);
shape-margin: 10px;
}

These examples show how to use the shape-margin property to add margins around different shapes, enhancing the layout and readability of your content.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
css_property_cover/css-property-grid-template.png Mastering CSS grid-template for Efficient Layouts Learn about CSS grid-template, a powerful property for creating responsive and dynamic grid layouts. Discover its use cases, available options like rows, columns, and areas, and how to optimize your CSS code. css_property_cover/css-property-padding-inline-start.png Understanding CSS padding-inline-start for Responsive Design Learn how to use the CSS padding-inline-start property to add padding at the start of an element's inline axis. Explore its use cases, available options, and compatibility. css_property_cover/css-property-anchor-name.png CSS Anchor-Name Enhancing Web Layouts with Anchors Discover the CSS anchor-name property, a powerful tool for defining elements as anchors and creating dynamic, responsive web layouts. Learn about its use cases, available options, and how to effectively implement it in your projects. css_property_cover/css-property-empty-cells.png CSS Empty-Cells Enhance Table Design Learn how to use CSS empty-cells to control the appearance of empty table cells. Choose 'show' or 'hide' to create cleaner, more visually appealing tables. css_property_cover/css-property-font-variant-position.png CSS Font-Variant-Position Enhance Typography with Superscript and Subscript Discover the CSS font-variant-position property for controlling superscript and subscript glyphs. Learn about its use cases, available options like 'normal', 'sub', and 'super', and how to incorporate it into your web design projects for improved typography. css_property_cover/css-property-gap.png CSS Gap Simplifying Layout Spacing Learn how to use the CSS gap property to simplify and control spacing in flex, grid, and multi-column layouts. Explore available options and practical examples.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.