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 级
# 内嵌

浏览器兼容性

另见