CSS margin-inline Property

The CSS margin-inline property is a shorthand that sets the margins at the start and end of an element's inline axis.

Unlike physical margins such as margin-left and margin-right, logical inline margins adapt to the element's writing mode and text direction. The shorthand sets margin-inline-start and margin-inline-end using one or two values.

margin-inline Syntax

The margin-inline shorthand accepts one or two values. Each value may be a length, percentage, or auto.

Basic Syntax

selector {
  margin-inline: 20px;
}

Two Values

selector {
  margin-inline: 10px 30px;
}

Percentage Values

selector {
  margin-inline: 5% 10%;
}

Auto Values

selector {
  margin-inline: auto;
}

Mixed Values

selector {
  margin-inline: 1rem auto;
}

Negative Values

selector {
  margin-inline: -10px 20px;
}

CSS-Wide Keywords

selector {
  margin-inline: initial;
}
selector {
  margin-inline: inherit;
}
selector {
  margin-inline: unset;
}
selector {
  margin-inline: revert;
}
selector {
  margin-inline: revert-layer;
}

Both longhand properties have an initial value of 0. CSS-wide keywords must be used alone, not mixed with length or percentage values.

Property Values

Value Description
<length> Sets an inline margin using a length such as px, em, rem, or vw.
<percentage> Sets an inline margin as a percentage of the containing block's inline size.
auto Allows the browser to calculate the margin according to the formatting context.
initial Resets both inline margins to their initial value of 0.
inherit Inherits both computed inline margin values from the parent element.
unset Resets both inline margins to 0 because margin properties are not inherited.
revert Reverts both inline margins according to cascade origin rules.
revert-layer Reverts both inline margins according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 0 for both longhand properties
Inherited No
Applies to Same as the corresponding physical margin properties
Computed value As specified for each longhand property
Animation type As specified for each longhand property
Negative values Allowed for lengths and percentages
Logical axis Inline axis
Longhand properties margin-inline-start and margin-inline-end

How It Works

The inline axis follows the direction in which text and inline content progress. The margin-inline property controls the outer spacing on both ends of that axis.

HTML Example

<div class="content-box">
  Content Box
</div>

CSS Example

.content-box {
  margin-inline: 20px;
  padding: 15px;
  border: 2px solid #2563eb;
  background-color: #dbeafe;
}

In a left-to-right horizontal writing mode, this creates 20-pixel margins on the left and right sides of the box.

Equivalent Longhand Properties

.content-box {
  margin-inline-start: 20px;
  margin-inline-end: 20px;
}

Both declarations produce the same logical inline margins as margin-inline: 20px.

Important Consideration

Margins are outside the border and do not extend the element's background. They may also affect the available space for the element's content box.

Writing Modes and Direction

The physical sides controlled by margin-inline depend on writing-mode and direction.

Writing Mode Inline-Start Inline-End
horizontal-tb, LTR Left Right
horizontal-tb, RTL Right Left
vertical-rl, LTR Top Bottom
vertical-rl, RTL Bottom Top
vertical-lr, LTR Top Bottom
vertical-lr, RTL Bottom Top

Left-to-Right Layout

.ltr-box {
  direction: ltr;
  margin-inline: 10px 30px;
}

In horizontal writing mode, the left margin is 10 pixels and the right margin is 30 pixels.

Right-to-Left Layout

.rtl-box {
  direction: rtl;
  margin-inline: 10px 30px;
}

In horizontal writing mode, the right margin is 10 pixels and the left margin is 30 pixels.

Vertical Writing Mode

.vertical-box {
  writing-mode: vertical-rl;
  direction: ltr;
  margin-inline: 10px 30px;
}

The top margin is 10 pixels and the bottom margin is 30 pixels.

Physical vs. Logical Margins

Physical margin properties always target fixed sides. Logical margins follow the inline and block axes.

Physical Margins

.box {
  margin-left: 20px;
  margin-right: 20px;
}

Logical Margins

.box {
  margin-inline: 20px;
}

These declarations are equivalent in horizontal writing modes because both inline margins use the same value.

Comparison

Feature Physical Margins margin-inline
Target sides Fixed physical sides Logical inline sides
Horizontal writing mode Left and right Left and right
Vertical writing modes Remain on specified sides Top and bottom
Direction-aware No Yes

Important Consideration

Use logical margins when components must adapt to different writing modes or text directions.

One and Two Values

The number of values determines whether both inline margins are equal or independently specified.

One Value

.box {
  margin-inline: 20px;
}

Both inline-start and inline-end margins are 20 pixels.

Two Values

.box {
  margin-inline: 10px 30px;
}

The first value sets inline-start, and the second sets inline-end.

Equivalent Longhand Declarations

.box {
  margin-inline-start: 10px;
  margin-inline-end: 30px;
}

Invalid Three-Value Declaration

.box {
  margin-inline: 10px 20px 30px;
}

This declaration is invalid because margin-inline accepts only one or two values.

Length and Percentage Values

The property supports absolute and relative lengths, percentages, and CSS calculations.

Pixel Values

.box {
  margin-inline: 16px 24px;
}

Relative Lengths

.box {
  margin-inline: 1rem 2rem;
}

Percentage Values

.box {
  margin-inline: 5% 10%;
}

Percentages are calculated relative to the containing block's inline size, normally its width in horizontal writing modes.

Calculated Values

.box {
  margin-inline: calc(1rem + 5px) 2rem;
}

Important Consideration

Large inline margins can reduce the space available for content or cause overflow when combined with fixed widths.

Auto Margins

Automatic inline margins can absorb available space, making margin-inline: auto useful for centering a block with a constrained width.

Center a Block

.centered-box {
  width: 300px;
  max-width: 100%;
  margin-inline: auto;
}

In a horizontal writing mode, the browser distributes available inline space between the two automatic margins, centering the box when sufficient space exists.

Center a Responsive Container

.page-container {
  max-width: 960px;
  margin-inline: auto;
}

This is a common way to center a page container without using separate left and right margin declarations.

One Automatic Margin

.box {
  width: 300px;
  margin-inline: auto 0;
}

In normal block layout, this can push the box toward the inline-end side when positive free space is available.

Important Consideration

Automatic margins do not guarantee centering in every formatting context. The element's width, available space, and layout rules determine the result.

Negative Margins

Negative inline margins can reduce spacing or move an element beyond its normal inline boundaries.

Negative Inline-Start Margin

.box {
  margin-inline: -20px 0;
}

Negative Inline-End Margin

.box {
  margin-inline: 0 -20px;
}

Negative Margins on Both Sides

.box {
  margin-inline: -15px;
}

Important Consideration

Negative margins can cause content to overlap neighboring elements or extend outside its container. Test these layouts at different viewport sizes and writing directions.

Shorthand and Cascade

The margin-inline shorthand sets both logical inline margin longhands. Later declarations can override one or both values.

Shorthand Followed by a Longhand

.box {
  margin-inline: 20px;
  margin-inline-start: 40px;
}

The final inline-start margin is 40 pixels, while inline-end remains 20 pixels.

Longhand Followed by Shorthand

.box {
  margin-inline-end: 40px;
  margin-inline: 10px;
}

The final inline-start and inline-end margins are both 10 pixels.

Physical and Logical Declarations

.box {
  margin-left: 10px;
  margin-inline-start: 20px;
}

In a left-to-right horizontal writing mode, both declarations target the left margin. With equal specificity and importance, the later declaration wins.

Important Consideration

The CSS cascade determines the winning declaration when physical and logical properties affect the same side.

Flexbox and Grid

Inline margins work with Flexbox and CSS Grid. They can position individual items or create additional space along the inline axis.

Flexbox Example

.flex-container {
  display: flex;
  gap: 10px;
}
.flex-item:last-child {
  margin-inline-start: auto;
}

In a horizontal row layout, the last flex item can be pushed toward the inline-end side when free space is available.

Grid Example

.grid-container {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}
.grid-item {
  margin-inline: 10px;
}

Each grid item receives inline margins inside its grid area.

Using gap Instead

.grid-container {
  display: grid;
  gap: 20px;
}

Use gap for consistent spacing between flex or grid items. Use margins when individual items need additional spacing or alignment.

Responsive Spacing

The margin-inline property can create adaptable horizontal spacing using relative units, media queries, and CSS functions.

Media Query Example

.content-box {
  margin-inline: 40px;
}
@media (max-width: 768px) {
  .content-box {
    margin-inline: 15px;
  }
}

Fluid Spacing

.content-box {
  margin-inline: clamp(1rem, 4vw, 3rem);
}

Centered Responsive Container

.page-container {
  width: min(100% - 2rem, 960px);
  margin-inline: auto;
}

This creates a centered container with a maximum width of 960 pixels and inline space around it on smaller screens.

Important Consideration

For responsive layouts, account for margins, padding, borders, and widths to avoid unintended overflow.

margin-inline Example

This example compares equal inline margins, unequal inline margins, and automatic inline margins in a horizontal writing mode.

HTML Example

<div class="margin-inline-demo">
  <div class="demo-section">
    <h3>Equal Inline Margins</h3>
    <div class="margin-area">
      <div class="demo-box equal-margin">margin-inline: 20px;</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>Different Inline Margins</h3>
    <div class="margin-area">
      <div class="demo-box different-margin">margin-inline: 10px 30px;</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>Automatic Inline Margins</h3>
    <div class="margin-area">
      <div class="demo-box auto-margin">margin-inline: auto;</div>
    </div>
  </div>
</div>

CSS Example

.margin-inline-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
}
.margin-inline-demo .demo-section {
  padding: 15px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.margin-inline-demo h3 {
  margin: 0 0 12px;
  font-size: 1rem;
}
.margin-inline-demo .margin-area {
  padding: 12px;
  background-color: #e2e8f0;
}
.margin-inline-demo .demo-box {
  box-sizing: border-box;
  padding: 12px 6px;
  border: 2px solid #2563eb;
  background-color: #bfdbfe;
  color: #1e3a8a;
  text-align: center;
  font-size: 0.75rem;
  overflow-wrap: anywhere;
}
.margin-inline-demo .equal-margin {
  margin-inline: 20px;
}
.margin-inline-demo .different-margin {
  margin-inline: 10px 30px;
}
.margin-inline-demo .auto-margin {
  width: 75%;
  margin-inline: auto;
}
Browser Result

Equal Inline Margins

margin-inline: 20px;

Different Inline Margins

margin-inline: 10px 30px;

Automatic Inline Margins

margin-inline: auto;

The first box has equal margins, the second has different inline-start and inline-end margins, and the third is centered using automatic margins.

Change to Equal 30px Margins

.margin-inline-demo .equal-margin {
  margin-inline: 30px;
}

Reverse the Unequal Margins

.margin-inline-demo .different-margin {
  margin-inline: 30px 10px;
}

Use Relative Margins

.margin-inline-demo .equal-margin {
  margin-inline: 1.5rem;
}

Common Mistakes

Assuming Inline Always Means Left and Right

In vertical writing modes, inline margins correspond to the top and bottom sides.

Confusing Inline-Start with Left

In right-to-left horizontal writing modes, inline-start corresponds to the right side.

Reversing the Two Values

The first value always sets inline-start, and the second sets inline-end.

Using Three or Four Values

The shorthand accepts only one or two values, unlike the four-sided margin shorthand.

Expecting Auto to Always Center an Element

Automatic margins require a suitable formatting context and available space.

Ignoring Available Width

Large margins combined with fixed widths can cause overflow.

Overwriting Longhand Values

.box {
  margin-inline-start: 40px;
  margin-inline: 10px;
}

The final inline-start margin is 10 pixels.

Mixing Physical and Logical Margins

Physical and logical margin properties can target the same side. The cascade determines which declaration wins.

Important Considerations

Keep the following points in mind when using margin-inline:

  • The property is a shorthand for two logical inline margins.
  • It sets margin-inline-start and margin-inline-end.
  • One value sets both inline margins equally.
  • Two values set inline-start followed by inline-end.
  • Three or four values are invalid.
  • Both longhand properties have an initial value of 0.
  • Margin properties are not inherited.
  • The physical sides depend on writing mode and direction.
  • In horizontal writing modes, the inline axis is horizontal.
  • In vertical writing modes, the inline axis is vertical.
  • CSS length and percentage values are supported.
  • Percentages are based on the containing block's inline size.
  • The auto keyword is supported.
  • Automatic inline margins can center constrained blocks.
  • Negative lengths and percentages are allowed.
  • Negative margins can cause overlap or overflow.
  • Margins are outside the element's border.
  • Margins do not extend the element's background.
  • The shorthand can override earlier inline margin longhands.
  • Later longhand declarations can override one side.
  • The four-sided margin shorthand can affect the same physical sides.
  • Physical and logical margin declarations can conflict.
  • The CSS cascade determines which conflicting declaration wins.
  • The property works with Flexbox and CSS Grid.
  • The gap property is useful for consistent spacing between layout items.
  • Relative units and media queries can create responsive spacing.
  • Logical margins are useful for multilingual and writing-mode-aware layouts.

Browser Support

The CSS margin-inline property is widely supported across modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.

It is part of CSS Logical Properties and Values. Older browsers may require physical margin fallbacks, such as margin-left and margin-right for horizontal writing modes.

For current compatibility information, visit Can I Use? and search for margin-inline.

Specifications

The margin-inline property is defined in the CSS Logical Properties and Values Module Level 1 specification.

CSS Logical Properties and Values Module Level 1: Margin Properties

The specification defines logical margin properties and how they map to physical sides according to writing modes.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: margin-inline