margin-trim CSS property

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Want more browser support for this feature? Tell us why.

The margin-trim CSS property allows a container to trim the block-axis margins of its children where they adjoin the container's edges.

Try it

margin-trim: none;
margin-trim: block;
margin-trim: block-start;
margin-trim: block-end;
<div class="example-wrapper" id="example-element">
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <p>Paragraph 3</p>
</div>
.example-wrapper {
  border: 2px solid tomato;
  margin: 0;
  padding: 0;
}

p {
  color: white;
  background-color: slategrey;
  padding: 0.5rem;
  margin: 1rem;
}

Syntax

css
margin-trim: none;
margin-trim: block;
margin-trim: block-start;
margin-trim: block-end;

/* Multiple values */
margin-trim: block-start block-end;

/* Global values */
margin-trim: inherit;
margin-trim: initial;
margin-trim: revert;
margin-trim: revert-layer;
margin-trim: unset;

Values

This property is specified as either the keyword none or block, or one or both of the keywords block-start and block-end:

none

Margins are not trimmed by the container.

block

Margins provided to the block children where they adjoin the container's edges are trimmed to zero without affecting the margins provided to the container.

block-start

Margin of the first block child with the container's edge is trimmed to zero.

block-end

Margin of last block child with the container's edge is trimmed to zero.

Description

Margins are often wanted between sibling elements but not between the first or last child and the edges of its container, where the spacing can be set with padding instead. Without margin-trim, removing those margins takes extra rules, such as one that targets the first child with the :first-child pseudo-class. With margin-trim, the container trims them itself, according to whichever value has been used.

Formal definition

Initial valuenone
Applies toBlock containers and multi-column containers. It also applies to ::first-letter.
Inheritedno
Computed valueas specified
Animation typediscrete

Formal syntax

margin-trim = 
none |
block |
[ block-start || block-end ]

Examples

Basic usage

In this example, each paragraph has a margin to space it from its siblings. Setting margin-trim: block-start on the <section> trims the top margin of the first paragraph, while the margins between the paragraphs remain.

This simplifies the syntax, meaning that the first paragraph does not need to be targeted with the :first-child pseudo-class.

html
<h2>Section Heading</h2>
<section>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <p>Paragraph 3</p>
</section>

The border around the <section> and background-color of the <p> are only there to help visualize the removal of the margin.

css
section {
  border: 2px solid tomato;
  margin: 0;
  padding: 0;
  margin-trim: block-start;
}
p {
  background-color: aliceblue;
  padding: 0.5rem;
  margin: 1rem;
}

In browsers that support margin-trim, the first paragraph sits against the top border of the section, while the margin below the last paragraph remains.

Specifications

Specification
CSS Box Model Module Level 4
# margin-trim

Browser compatibility

See also