margin-left

Baseline 廣泛可用 *

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

* 此特性的某些部分可能存在不同級別的支援。

margin-left CSS 屬性設定元素左側的外邊距區域。正值使它離相鄰元素更遠,而負值使它更近。

試一試

margin-left: 1em;
margin-left: 10%;
margin-left: 10px;
margin-left: 0;
<section id="default-example">
  <div id="container">
    <div class="col"></div>
    <div class="col transition-all" id="example-element"></div>
    <div class="col"></div>
  </div>
</section>
#container {
  width: 300px;
  height: 200px;
  display: flex;
  align-content: flex-start;
  justify-content: flex-start;
}

.col {
  width: 33.33%;
  border: solid #5b6dcd 10px;
  background-color: rgb(229 232 252 / 0.6);
  flex-shrink: 0;
}

#example-element {
  border: solid 10px #ffc129;
  background-color: rgb(255 244 219 / 0.6);
}

兩個相鄰盒子的垂直外邊距可能會合並。這被稱為外邊距合併

在極少數情況下,如果寬度被過度約束(即同時定義了 widthmargin-leftborderpadding、內容區域和 margin-right),margin-left 將被忽略,並且其計算值將與指定 auto 值時相同。

語法

css
/* <length> values */
margin-left: 10px; /* An absolute length */
margin-left: 1em; /* relative to the text size */
margin-left: 5%; /* relative to the nearest block container's width */
margin-left: anchor-size(self-inline);
margin-left: calc(anchor-size(--my-anchor width, 20px) / 4);

/* Keyword values */
margin-left: auto;

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

margin-left 屬性被指定為關鍵字 auto,或一個 <length>,或一個 <percentage>。它的值可以是正數、零或負數。

<length>

邊距的大小為固定值。

<percentage>

邊距的大小為百分比,相對於包含塊的內聯尺寸(在水平書寫模式下是寬度,由writing-mode定義)。

auto

左外邊距會獲得一部分未使用的水平空間,這主要由所使用的佈局模式決定。如果 margin-leftmargin-right 的值都為 auto,則計算出的空間將均勻分佈。下表總結了不同的情況:

display 的值 float 的值 position 的值 auto 的計算值 Comment
inline, inline-block, inline-table 任意 staticrelative 0 行內佈局模式
block, inline, inline-block, block, table, inline-table, list-item, table-caption 任意 staticrelative 0,除非 margin-leftmargin-right 都設定為 auto。在這種情況下,它被設定為使元素在其父元素中居中的值。 塊級佈局模式
block, inline, inline-block, block, table, inline-table, list-item, table-caption leftright staticrelative 0 塊級佈局模式(浮動元素)
任意 table-*,除了 table-caption 任意 任意 0 內部 table-* 元素沒有外邊距,請改用border-spacing
任意,除了 flexinline-flextable-* 任意 fixedabsolute 0,除非 margin-leftmargin-right 都設定為 auto。在這種情況下,如果已固定,它被設定為使邊框區域在可用 width 中居中的值。 絕對定位佈局模式
flex, inline-flex 任意 任意 0,除非存在任何正的水平自由空間。在這種情況下,它會均勻地分配給所有水平 auto 外邊距。 Flexbox 佈局模式

正式定義

初始值0
應用於所有元素,除了具有 table-captiontableinline-table 以外的表格display 型別的元素。它也適用於 ::first-letter
繼承性
百分比參照包含塊的寬度
計算值指定的百分比或絕對長度
動畫型別一個長度

正式語法

margin-left = 
<length-percentage> |
auto |
<anchor-size()>

<length-percentage> =
<length> |
<percentage>

<anchor-size()> =
anchor-size( [ <anchor-name> || <anchor-size> ]? , <length-percentage>? )

<anchor-name> =
<dashed-ident>

<anchor-size> =
width |
height |
block |
inline |
self-block |
self-inline

示例

將 margin-left 設定為百分比

margin-left 的百分比值是相對於容器的行內尺寸。

HTML

html
<p>
  A large rose-tree stood near the entrance of the garden: the roses growing on
  it were white, but there were three gardeners at it, busily painting them red.
</p>
<p class="example">
  Alice thought this a very curious thing, and she went nearer to watch them,
  and just as she came up to them she heard one of them say, "Look out now,
  Five! Don't go splashing paint over me like that!"
</p>
<p>
  "I couldn't help it," said Five, in a sulky tone; "Seven jogged my elbow."
</p>

CSS

css
.example {
  margin-left: 50%;
}

結果

規範

規範
CSS Box Model Module Level 3
# margin-physical

瀏覽器相容性

另見