CSS margin-left Property

The CSS margin-left property sets the outer margin on the left side of an element, creating space between its left border and surrounding content.

It accepts lengths, percentages, negative values, and the auto keyword. Unlike logical margin properties, margin-left always targets the physical left side, regardless of writing mode or text direction.

margin-left Syntax

The margin-left property accepts a length, percentage, or auto. CSS-wide keywords are also supported.

Basic Syntax

selector {
  margin-left: 20px;
}

Length Value

div {
  margin-left: 2rem;
}

Percentage Value

div {
  margin-left: 10%;
}

Auto Value

div {
  margin-left: auto;
}

Negative Value

div {
  margin-left: -15px;
}

CSS-Wide Keywords

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

The initial value of margin-left is 0.

Property Values

Value Description
<length> Sets the left margin using a length such as px, em, rem, or vw.
<percentage> Sets the left margin as a percentage of the containing block's width.
auto Allows the browser to calculate the left margin according to the formatting context.
initial Resets the property to its initial value of 0.
inherit Inherits the computed left margin 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 All elements except elements with table display types other than table-caption, table, and inline-table
Computed value Absolute length, percentage, or auto
Animation type Length, percentage, or calculated combination
Negative values Allowed for lengths and percentages
Physical side Left
Shorthand property margin

How It Works

The margin-left property adds space outside an element's left border. It affects layout spacing without adding to the element's background or padding.

HTML Example

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

CSS Example

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

The box receives a 30-pixel left margin, separating its left border from the surrounding layout.

Margin vs. Padding

.content-box {
  margin-left: 30px;
  padding-left: 15px;
}

The margin creates space outside the border, while padding creates space between the content and border.

Length Values

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

Pixels

.box {
  margin-left: 24px;
}

Root Font Size

.box {
  margin-left: 2rem;
}

Element Font Size

.box {
  margin-left: 1.5em;
}

Viewport Units

.box {
  margin-left: 3vw;
}

Calculated Value

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

Relative units can help maintain proportional spacing as font sizes and viewport dimensions change.

Percentage Values

Percentage left margins are calculated relative to the width of the containing block, even when the element uses a vertical writing mode.

Percentage Example

.box {
  margin-left: 10%;
}

Fixed Container Width

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

The left margin equals 60 pixels when the containing block's width is 600 pixels.

Important Consideration

Percentage margins can change as the containing block resizes. Large percentages combined with fixed element widths may cause horizontal overflow.

Auto Margins

The auto keyword allows the browser to calculate the left margin based on available space and the formatting context.

Basic Example

.box {
  margin-left: auto;
}

Push a Block to the Right

.box {
  width: 300px;
  max-width: 100%;
  margin-left: auto;
  margin-right: 0;
}

In a normal left-to-right block layout, the automatic left margin absorbs available horizontal space, placing the box against the right side of its containing block.

Center a Block

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

When sufficient horizontal space exists, equal automatic left and right margins center the block.

Important Consideration

Automatic margins do not always create visible spacing. Their behavior depends on the element's dimensions, available space, and layout rules.

Negative Margins

Negative left margins can move an element toward the left or reduce the horizontal space it contributes to the layout.

Negative Length

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

Negative Percentage

.box {
  margin-left: -5%;
}

Overlapping Elements

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

The negative margin can pull an item toward the preceding content, potentially causing overlap.

Important Consideration

Negative margins are valid CSS but can create unexpected overlap or overflow. Test them carefully across different screen sizes.

margin Shorthand

The margin shorthand can set all four physical margins, including margin-left.

Individual Properties

.box {
  margin-top: 10px;
  margin-right: 20px;
  margin-bottom: 30px;
  margin-left: 40px;
}

Equivalent Shorthand

.box {
  margin: 10px 20px 30px 40px;
}

With four values, the order is top, right, bottom, and left.

One Value

.box {
  margin: 20px;
}

All four margins are 20 pixels.

Two Values

.box {
  margin: 10px 20px;
}

The top and bottom margins are 10 pixels, while the left and right margins are 20 pixels.

Three Values

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

The left and right margins are both 20 pixels.

Shorthand Override

.box {
  margin-left: 40px;
  margin: 10px;
}

The final left margin is 10 pixels because the later shorthand resets all four margins.

Override Only the Left Margin

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

The left margin becomes 40 pixels, while the other three margins remain 20 pixels.

Physical vs. Logical Margins

The margin-left property always targets the physical left side. The logical margin-inline-start and margin-inline-end properties adapt to writing modes and text direction.

Physical Left Margin

.box {
  margin-left: 20px;
}

Logical Inline-Start Margin

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

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

Comparison

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

Conflicting 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

Use physical margins when spacing must remain on a fixed side. Use logical margins when components must adapt to different writing directions.

Flexbox and Grid

The margin-left property works with Flexbox and CSS Grid, where it can create spacing or control item alignment.

Flexbox Example

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

In a left-to-right horizontal flex row, the last item's automatic left margin can absorb available space, pushing it toward the right side.

Grid Example

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

Each grid item receives a 20-pixel left margin within its grid area.

Using gap Instead

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

Use gap for consistent spacing between flex or grid items. Use margin-left when an individual item requires additional left-side spacing.

Responsive Spacing

Left margins can be adjusted for different screen sizes using relative units, media queries, and CSS functions.

Media Query Example

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

Relative Spacing

.content-box {
  margin-left: 1.5rem;
}

Fluid Spacing

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

Important Consideration

Large fixed margins combined with fixed widths can cause horizontal overflow on narrow screens. Use responsive units or media queries when appropriate.

margin-left Example

This example demonstrates how different left margin values change the spacing before an element in a horizontal layout.

HTML Example

<div class="margin-left-demo">
  <div class="demo-section">
    <h3>No Left Margin</h3>
    <div class="margin-area">
      <div class="demo-box first-box">First</div>
      <div class="demo-box no-margin">Second</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>20px Left Margin</h3>
    <div class="margin-area">
      <div class="demo-box first-box">First</div>
      <div class="demo-box medium-margin">Second</div>
    </div>
  </div>
  <div class="demo-section">
    <h3>40px Left Margin</h3>
    <div class="margin-area">
      <div class="demo-box first-box">First</div>
      <div class="demo-box large-margin">Second</div>
    </div>
  </div>
</div>

CSS Example

.margin-left-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 20px;
}
.margin-left-demo .demo-section {
  padding: 15px;
  border: 1px solid #cbd5e1;
  background-color: #f8fafc;
}
.margin-left-demo h3 {
  margin: 0 0 12px;
  font-size: 1rem;
}
.margin-left-demo .margin-area {
  display: flex;
  direction: ltr;
  padding: 12px;
  background-color: #e2e8f0;
}
.margin-left-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-left-demo .first-box {
  margin: 0;
  border-color: #64748b;
  background-color: #f1f5f9;
  color: #334155;
}
.margin-left-demo .no-margin {
  margin-left: 0;
}
.margin-left-demo .medium-margin {
  margin-left: 20px;
}
.margin-left-demo .large-margin {
  margin-left: 40px;
}
Browser Result

No Left Margin

First
Second

20px Left Margin

First
Second

40px Left Margin

First
Second

As the left margin increases, the space before the second item increases. The margin remains on the physical left side regardless of text direction.

Change the Margin to 30px

.margin-left-demo .medium-margin {
  margin-left: 30px;
}

Use a Relative Unit

.margin-left-demo .large-margin {
  margin-left: 2rem;
}

Remove the Margin

.margin-left-demo .large-margin {
  margin-left: 0;
}

Use a Negative Margin

.margin-left-demo .large-margin {
  margin-left: -10px;
}

The negative margin can pull the second box toward the first, potentially causing overlap.

Common Mistakes

Confusing Margin with Padding

margin-left creates space outside the border, while padding-left creates space inside it.

Expecting margin-left to Follow Text Direction

The property always targets the physical left side. Use logical margins for direction-aware spacing.

Expecting Percentage Values to Use the Element's Width

Percentage left margins are based on the containing block's width.

Expecting Auto to Always Create Space

Automatic margins depend on the formatting context and available space.

Using Negative Margins Without Testing

Negative margins can cause unintended overlap or overflow.

Overwriting the Value with a Shorthand

.box {
  margin-left: 40px;
  margin: 10px;
}

The final left margin is 10 pixels.

Mixing Physical and Logical Margins

In a left-to-right horizontal layout, margin-left and margin-inline-start target the same side. The 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-left:

  • The property controls the outer margin on the physical left side.
  • The initial value is 0.
  • The property is not inherited.
  • It does not change sides when text direction changes.
  • It does not change sides when the writing mode changes.
  • CSS length values are supported.
  • Percentage values are based on the containing block's width.
  • 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.
  • Margins do not directly increase the element's declared width.
  • The property can be set using the margin shorthand.
  • Later shorthand declarations can override earlier left margins.
  • Physical and logical margin declarations can affect the same side.
  • The CSS cascade determines which conflicting declaration wins.
  • Automatic left margins can absorb available space in certain layouts.
  • Automatic left and right margins can center constrained blocks.
  • The property works with Flexbox and CSS Grid.
  • Relative units can provide scalable spacing.
  • Media queries can adjust margins for smaller screens.
  • Large margins combined with fixed widths can cause overflow.
  • Use logical margins when spacing should follow writing direction.

Browser Support

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

It is a long-established CSS property and does not require vendor prefixes.

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

Specifications

The margin-left property is defined in the CSS Box Model Module Level 3 specification.

CSS Box Model Module Level 3: Physical Margin Properties

The specification defines physical margins, their accepted values, and how margins affect the CSS box model.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: margin-left