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 級 # 內嵌 |
瀏覽器相容性
載入中…