left
Baseline 广泛可用 *
试一试
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>- 对于绝对定位元素,它表示到包含块左边缘的距离。
- 对于锚定定位元素,
anchor()函数解析为一个相对于关联的锚定元素的左边缘或右边缘位置的<length>值(参见将内嵌属性与anchor()函数值一起使用),而anchor-size()函数解析为一个相对于关联的锚定元素的宽度或高度的<length>值(参见根据锚定大小设置元素位置)。 - 对于相对定位元素,它表示元素相对于其正常位置向右移动的距离。
<percentage>-
包含块宽度的
<percentage>。 auto-
指定
描述
left 的效果取决于元素的定位方式(即,position 属性的值)
- 当
position设置为absolute或fixed时,left属性指定了元素的左外边距边缘与其包含块的左内边框边缘之间的距离。(包含块是元素相对定位到的祖先元素。)如果定位元素具有关联的锚定元素,并且属性值包含anchor()函数,则left将定位元素的左边缘相对于指定<anchor-side>边缘的位置进行定位。left属性与left、right、start、end、self-start、self-end、center和<percentage>值兼容。 - 当
position设置为relative时,left属性指定了元素的左边缘从其正常位置向右移动的距离。 - 当
position设置为sticky时,left属性用于计算粘性约束矩形。 - 当
position设置为static时,left属性无效。
当同时定义了 left 和 right,并且宽度约束不阻止时,元素将拉伸以同时满足两者。如果元素无法拉伸以同时满足两者,则元素的位置是过度指定的。在这种情况下,当容器是左到右时,left 值具有优先权;当容器是右到左时,right 值具有优先权。
正式定义
正式语法
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 级 # 内嵌 |
浏览器兼容性
加载中…