left

Baseline 廣泛可用 *

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

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

left CSS 屬性參與指定定位元素的水平位置。此內嵌屬性對非定位元素無效。

試一試

left: 0;
left: 4em;
left: 10%;
left: 20px;
<section id="default-example">
  <div class="example-container">
    <div id="example-element">I am absolutely positioned.</div>
    <p>
      As much mud in the streets as if the waters had but newly retired from the
      face of the earth, and it would not be wonderful to meet a Megalosaurus,
      forty feet long or so, waddling like an elephantine lizard up Holborn
      Hill.
    </p>
  </div>
</section>
.example-container {
  border: 0.75em solid;
  padding: 0.75em;
  text-align: left;
  position: relative;
  width: 100%;
  min-height: 200px;
}

#example-element {
  background-color: #264653;
  border: 4px solid #ffb500;
  color: white;
  position: absolute;
  width: 140px;
  height: 60px;
}

語法

css
/* <length> values */
left: 3px;
left: 2.4em;
left: anchor(--my-anchor 50%);
left: calc(anchor-size(--my-anchor inline, 100px) * 2);

/* <percentage>s of the width of the containing block */
left: 10%;

/* Keyword value */
left: auto;

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

<length>

負值、零值或正值 <length>

<percentage>

包含塊寬度的<percentage>

auto

指定

  • 對於絕對定位元素,元素的位置基於right 屬性,而 width: auto 被視為基於內容的寬度;或者如果 right 也為 auto,則元素水平定位在其作為靜態元素時應有的位置。
  • 對於相對定位元素,元素與其正常位置的距離基於right 屬性;或者如果 right 也為 auto,則元素根本不會水平移動。

描述

left 的效果取決於元素的定位方式(即,position 屬性的值)

  • position 設定為 absolutefixed 時,left 屬性指定了元素的左外邊距邊緣與其包含塊的左內邊框邊緣之間的距離。(包含塊是元素相對定位到的祖先元素。)如果定位元素具有關聯的錨定元素,並且屬性值包含anchor() 函式,則 left 將定位元素的左邊緣相對於指定<anchor-side> 邊緣的位置進行定位。left 屬性與 leftrightstartendself-startself-endcenter<percentage>相容
  • position 設定為 relative 時,left 屬性指定了元素的左邊緣從其正常位置向右移動的距離。
  • position 設定為 sticky 時,left 屬性用於計算粘性約束矩形。
  • position 設定為 static 時,left 屬性無效

當同時定義了 leftright,並且寬度約束不阻止時,元素將拉伸以同時滿足兩者。如果元素無法拉伸以同時滿足兩者,則元素的位置是過度指定的。在這種情況下,當容器是左到右時,left 值具有優先權;當容器是右到左時,right 值具有優先權。

正式定義

初始值auto
應用於定位元素
繼承性
百分比參照包含塊的寬度
計算值如果指定為長度,則為相應的絕對長度;如果指定為百分比,則為指定值;否則為 auto
動畫型別一個長度百分比或 calc();

正式語法

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

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

<anchor()> =
anchor( <anchor-name>? &&
<anchor-side> , <length-percentage>? )

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

<anchor-name> =
<dashed-ident>

<anchor-side> =
inside |
outside |
top |
left |
right |
bottom |
start |
end |
self-start |
self-end |
<percentage> |
center

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

示例

定位元素

HTML

html
<div id="wrap">
  <div id="example_1">
    <pre>
      position: absolute;
      left: 20px;
      top: 20px;
    </pre>
    <p>
      The only containing element for this div is the main window, so it
      positions itself in relation to it.
    </p>
  </div>

  <div id="example_2">
    <pre>
      position: relative;
      top: 0;
      right: 0;
    </pre>
    <p>Relative position in relation to its siblings.</p>
  </div>

  <div id="example_3">
    <pre>
      float: right;
      position: relative;
      top: 20px;
      left: 20px;
    </pre>
    <p>Relative to its sibling div above, but removed from flow of content.</p>

    <div id="example_4">
      <pre>
        position: absolute;
        bottom: 10px;
        right: 20px;
      </pre>
      <p>Absolute position inside of a parent with relative position</p>
    </div>

    <div id="example_5">
      <pre>
        position: absolute;
        right: 0;
        left: 0;
        top: 200px;
      </pre>
      <p>Absolute position with both left and right declared</p>
    </div>
  </div>
</div>

CSS

css
#wrap {
  width: 700px;
  margin: 0 auto;
  background: #5c5c5c;
}

pre {
  white-space: pre-line;
  word-wrap: break-word;
}

#example_1 {
  width: 200px;
  height: 200px;
  position: absolute;
  left: 20px;
  top: 20px;
  background-color: #d8f5ff;
}

#example_2 {
  width: 200px;
  height: 200px;
  position: relative;
  top: 0;
  right: 0;
  background-color: #c1ffdb;
}
#example_3 {
  width: 600px;
  height: 400px;
  position: relative;
  top: 20px;
  left: 20px;
  background-color: #ffd7c2;
}

#example_4 {
  width: 200px;
  height: 200px;
  position: absolute;
  bottom: 10px;
  right: 20px;
  background-color: #ffc7e4;
}
#example_5 {
  position: absolute;
  right: 0;
  left: 0;
  top: 100px;
  background-color: #d7ffc2;
}

結果

規範

規範
CSS 定位佈局模組第 3 級
# 內嵌

瀏覽器相容性

另見