CSS margin-inline-end Property

The CSS margin-inline-end property sets the outer margin at the end of an element's inline axis, with the physical side determined by its writing mode and text direction.

In a left-to-right horizontal writing mode, margin-inline-end corresponds to margin-right. In right-to-left layouts, it corresponds to margin-left. This logical property helps components adapt to different writing directions without requiring separate physical margin declarations.

margin-inline-end Syntax

The margin-inline-end property accepts a CSS length, percentage, or the auto keyword. CSS-wide keywords are also supported.

Basic Syntax

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

Length Value

div {
  margin-inline-end: 2rem;
}

Percentage Value

div {
  margin-inline-end: 5%;
}

Auto Value

div {
  margin-inline-end: auto;
}

Negative Value

div {
  margin-inline-end: -15px;
}

CSS-Wide Keywords

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

The initial value of margin-inline-end is 0.

Property Values

Value Description
<length> Sets the inline-end margin using a length such as px, em, rem, or vw.
<percentage> Sets the 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 the property to its initial value of 0.
inherit Inherits the computed value from the parent element.
unset Resets the property to 0 because margins are not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value 0
Inherited No
Applies to Same as the corresponding physical margin properties
Computed value Absolute length, percentage, or auto
Animation type Length, percentage, or calculated combination
Negative values Allowed for lengths and percentages
Logical axis Inline axis
Physical equivalent Depends on writing-mode and direction

How It Works

The inline axis describes the direction in which text and inline content progress. The margin-inline-end property creates outer spacing at the end of that axis.

HTML Example

<div class="content-row">
  <span class="content-item">First Item</span>
  <span class="content-item">Second Item</span>
</div>

CSS Example

.content-row {
  display: flex;
}
.content-item {
  margin-inline-end: 20px;
  padding: 10px;
  border: 2px solid #2563eb;
  background-color: #dbeafe;
}

In a left-to-right horizontal layout, each item receives a 20-pixel right margin. In a right-to-left layout, the same declaration creates a 20-pixel left margin.

Remove the Last Item's Margin

.content-item:last-child {
  margin-inline-end: 0;
}

This prevents unnecessary spacing after the final item in the row.

Writing Modes and Direction

The physical side controlled by margin-inline-end depends on the element's writing-mode and direction.

Writing Mode Direction Inline-End Side
horizontal-tb ltr Right
horizontal-tb rtl Left
vertical-rl ltr Bottom
vertical-rl rtl Top
vertical-lr ltr Bottom
vertical-lr rtl Top

Left-to-Right Horizontal Layout

.ltr-box {
  writing-mode: horizontal-tb;
  direction: ltr;
  margin-inline-end: 20px;
}

The margin appears on the right side.

Right-to-Left Horizontal Layout

.rtl-box {
  writing-mode: horizontal-tb;
  direction: rtl;
  margin-inline-end: 20px;
}

The margin appears on the left side.

Vertical Writing Mode

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

The margin appears on the bottom side because the inline direction progresses downward.

Important Consideration

Both writing mode and direction must be considered when determining the physical side represented by inline-end.

Physical vs. Logical Margins

Physical margin properties target fixed sides. Logical margins follow the writing mode and direction.

Physical Right Margin

.box {
  margin-right: 20px;
}

Logical Inline-End Margin

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

These declarations are equivalent in a left-to-right horizontal writing mode.

Comparison

Feature margin-right margin-inline-end
Physical side Always right Depends on writing mode and direction
Horizontal LTR Right Right
Horizontal RTL Right Left
Vertical writing modes Right Top or bottom
Direction-aware No Yes

Important Consideration

Logical margins are useful for components that must support both left-to-right and right-to-left languages.

Length Values

The margin-inline-end property supports absolute and relative CSS length units.

Pixels

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

Root Font Size

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

Element Font Size

.box {
  margin-inline-end: 1.5em;
}

Viewport Units

.box {
  margin-inline-end: 3vw;
}

Calculated Value

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

Important Consideration

Relative units can help keep spacing proportional as font sizes or viewport dimensions change.

Percentage Values

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

Percentage Example

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

Percentage with a Fixed Container Width

.container {
  width: 600px;
}
.container .box {
  margin-inline-end: 10%;
}

In a horizontal writing mode, the 10% margin equals 60 pixels when the containing block's width is 600 pixels.

Important Consideration

Percentage margins are not necessarily based on the dimension of the physical side they affect. Their reference is the containing block's inline size.

Auto Margins

The auto keyword allows the browser to calculate the inline-end margin according to the layout context.

Basic Example

.box {
  margin-inline-end: auto;
}

Flexbox Example

.flex-container {
  display: flex;
  width: 100%;
}
.flex-item:first-child {
  margin-inline-end: auto;
}

In a horizontal flex row with available free space, the first item's automatic inline-end margin can absorb that space, pushing subsequent items toward the inline-end side.

Centered Block

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

Using automatic margins on both inline sides can center a constrained block in normal block layout.

Important Consideration

An automatic inline-end margin does not always create visible spacing. Its behavior depends on the available space and formatting context.

Negative Margins

Negative inline-end margins can reduce spacing or extend an element beyond its normal inline boundary.

Negative Length

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

Negative Percentage

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

Overlapping Items

.item {
  display: inline-block;
  margin-inline-end: -10px;
  padding: 12px;
  border: 1px solid #2563eb;
}

In a horizontal writing mode, the negative margin reduces the space contributed at the inline-end side and can cause adjacent items to overlap.

Important Consideration

Negative margins can cause unintended overlap or overflow. Test these layouts in different writing directions and at smaller viewport sizes.

margin-inline Shorthand

The margin-inline shorthand sets both inline-start and inline-end margins.

Individual Properties

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

Equivalent Shorthand

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

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

Equal Inline Margins

.box {
  margin-inline: 20px;
}

Both inline margins are 20 pixels.

Shorthand Reset Behavior

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

The final inline-end margin is 10 pixels because the later shorthand resets both inline margins.

Override Only Inline-End

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

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

Flexbox and Grid

The margin-inline-end property works with Flexbox and CSS Grid. It can add spacing after individual items or control their alignment.

Flexbox Example

.flex-container {
  display: flex;
  gap: 10px;
}
.flex-item {
  margin-inline-end: 15px;
}

Each item receives an additional inline-end margin. In a left-to-right horizontal row, this appears on the right side.

Grid Example

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

Each grid item receives an inline-end margin within its grid area.

Using gap Instead

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

The gap property is usually preferable for consistent spacing between flex or grid items because it does not add margins around the outside edges.

Important Consideration

Use margin-inline-end when an individual item needs additional spacing after it. Use gap when all items need consistent separation.

Responsive Spacing

Inline-end margins can be adjusted for different viewport sizes using relative units, media queries, and CSS functions.

Media Query Example

.content-item {
  margin-inline-end: 30px;
}
@media (max-width: 768px) {
  .content-item {
    margin-inline-end: 10px;
  }
}

Using rem Units

.content-item {
  margin-inline-end: 1.5rem;
}

Fluid Spacing with clamp()

.content-item {
  margin-inline-end: clamp(0.5rem, 2vw, 2rem);
}

Important Consideration

Responsive inline spacing should account for available width and text direction. Large fixed margins can cause overflow on narrow screens.

margin-inline-end Example

This example demonstrates how different inline-end margin values change the spacing between items in a horizontal flex layout.

HTML Example

<div class="margin-inline-end-demo">
  <div class="demo-section">
    <h3>No Inline-End Margin</h3>
    <div class="margin-area">
      <div class="demo-box no-margin">First</div>
      <div class="demo-box following-box">Second</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>20px Inline-End Margin</h3>
    <div class="margin-area">
      <div class="demo-box medium-margin">First</div>
      <div class="demo-box following-box">Second</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>40px Inline-End Margin</h3>
    <div class="margin-area">
      <div class="demo-box large-margin">First</div>
      <div class="demo-box following-box">Second</div>
    </div>
  </div>
</div>

CSS Example

.margin-inline-end-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
}
.margin-inline-end-demo .demo-section {
  padding: 15px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.margin-inline-end-demo h3 {
  margin: 0 0 12px;
  font-size: 1rem;
}
.margin-inline-end-demo .margin-area {
  display: flex;
  direction: ltr;
  padding: 12px;
  background-color: #e2e8f0;
}
.margin-inline-end-demo .demo-box {
  flex: 0 0 auto;
  padding: 12px;
  border: 2px solid #2563eb;
  background-color: #bfdbfe;
  color: #1e3a8a;
  text-align: center;
  font-size: 0.875rem;
}
.margin-inline-end-demo .no-margin {
  margin-inline-end: 0;
}
.margin-inline-end-demo .medium-margin {
  margin-inline-end: 20px;
}
.margin-inline-end-demo .large-margin {
  margin-inline-end: 40px;
}
.margin-inline-end-demo .following-box {
  margin: 0;
  border-color: #64748b;
  background-color: #f1f5f9;
  color: #334155;
}
Browser Result

No Inline-End Margin

First
Second

20px Inline-End Margin

First
Second

40px Inline-End Margin

First
Second

As the inline-end margin increases, the space after the first item increases. In this left-to-right layout, the margin appears on the right side of the first item.

Change the Margin to 30px

.margin-inline-end-demo .medium-margin {
  margin-inline-end: 30px;
}

Use a Relative Unit

.margin-inline-end-demo .large-margin {
  margin-inline-end: 2rem;
}

Remove the Margin

.margin-inline-end-demo .large-margin {
  margin-inline-end: 0;
}

Switch to Right-to-Left Direction

.margin-inline-end-demo .margin-area {
  direction: rtl;
}

The inline-end margin now appears on the left side of the first item.

Common Mistakes

Assuming Inline-End Always Means Right

In right-to-left horizontal layouts, inline-end corresponds to the left side.

Ignoring Vertical Writing Modes

In vertical writing modes, inline-end corresponds to the top or bottom side.

Confusing Inline-End with Block-End

margin-inline-end follows the inline axis, while margin-block-end follows the block axis.

Expecting Percentage Values to Use the Physical Side's Dimension

Percentages are calculated from the containing block's inline size.

Expecting Auto to Always Create Space

Automatic margins depend on the formatting context and available free space.

Using Negative Margins Without Testing

Negative margins can cause overlap or overflow.

Overwriting the Value with a Shorthand

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

The final inline-end margin is 10 pixels.

Mixing Physical and Logical Margins

In left-to-right horizontal layouts, margin-right and margin-inline-end target the same side. The CSS cascade determines which declaration wins.

Using Margins Instead of gap for Every Layout

Use gap when consistent spacing between flex or grid items is needed.

Important Considerations

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

  • The property controls the margin at the end of the inline axis.
  • It is a logical margin property.
  • The initial value is 0.
  • The property is not inherited.
  • The physical side depends on writing mode and direction.
  • In horizontal LTR layouts, inline-end is the right side.
  • In horizontal RTL layouts, inline-end is the left side.
  • In vertical LTR layouts, inline-end is the bottom side.
  • In vertical RTL layouts, inline-end is the top side.
  • CSS length values are supported.
  • Percentage values are based on the containing block's inline size.
  • The auto keyword is supported.
  • 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 property can be set using the margin-inline shorthand.
  • The margin shorthand can also affect the corresponding physical side.
  • Physical and logical margin declarations can conflict.
  • The CSS cascade determines which conflicting declaration wins.
  • The property works with Flexbox and CSS Grid.
  • Automatic margins can absorb free space in flex layouts.
  • The gap property is useful for consistent spacing between layout items.
  • Relative units can provide scalable spacing.
  • Media queries can adjust margins for smaller screens.
  • Logical margins are useful for multilingual and writing-mode-aware layouts.

Browser Support

The CSS margin-inline-end 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-right for left-to-right horizontal layouts.

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

Specifications

The margin-inline-end 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 how logical margin properties map to physical sides according to writing modes and direction.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: margin-inline-end