right
Baseline 廣泛可用 *
試一試
right: 0;
right: 4em;
right: 10%;
right: 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 */
right: 3px;
right: 2.4em;
right: anchor(--my-anchor 50%);
right: anchor-size(--my-anchor height, 65px);
/* <percentage>s of the width of the containing block */
right: 10%;
/* Keyword value */
right: auto;
/* Global values */
right: inherit;
right: initial;
right: revert;
right: revert-layer;
right: unset;
值
<length>-
負值、零值或正值
<length>- 對於絕對定位元素,它表示到包含塊右邊緣的距離。
- 對於錨定定位元素,
anchor()函式解析為一個<length>值,該值相對於關聯的錨定元素的左邊緣或右邊緣的位置(參見將嵌入屬性與anchor()函式值一起使用),而anchor-size()函式解析為一個<length>值,該值相對於關聯錨定元素的寬度或高度(參見根據錨定尺寸設定元素位置)。 - 對於相對定位元素,它表示元素向左移動的距離,相對於其正常位置。
<percentage>-
包含塊寬度的
<percentage>。 auto-
指定
描述
right 的效果取決於元素的定位方式(即 position 屬性的值)
- 當
position設定為absolute或fixed時,right屬性指定元素的右邊緣外邊距與其包含塊右邊緣內邊框之間的距離。如果定位元素具有關聯的錨定元素,並且屬性值包含anchor()函式,則right將定位元素的右邊緣相對於指定的<anchor-side>邊緣進行定位。right屬性與left、right、start、end、self-start、self-end、center和<percentage>值相容。 - 當
position設定為relative時,right屬性指定元素的右邊緣向左移動的距離,相對於其正常位置。 - 當
position設定為sticky時,right屬性用於計算粘性約束矩形。 - 當
position設定為static時,right屬性不起作用。
當 left 和 right 都定義時,如果其他屬性未阻止這樣做,元素將拉伸以同時滿足兩者。如果元素無法拉伸以滿足兩者——例如,如果聲明瞭 width——則元素的位置會過度約束。在這種情況下,當容器是從左到右時,left 值具有優先順序;當容器是從右到左時,right 值具有優先順序。
正式定義
正式語法
right =
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
示例
使用 right 進行絕對和相對定位
HTML
html
<div id="relative">Relatively positioned</div>
<div id="absolute">Absolutely positioned</div>
CSS
css
#relative {
width: 100px;
height: 100px;
background-color: #ffc7e4;
position: relative;
top: 20px;
left: 20px;
}
#absolute {
width: 100px;
height: 100px;
background-color: #ffd7c2;
position: absolute;
bottom: 10px;
right: 20px;
}
結果
同時宣告 left 和 right
當同時宣告 left 和 right 時,元素將拉伸以滿足兩者,除非其他約束阻止它這樣做。如果元素不會拉伸或縮小以滿足兩者。當元素的位置過度指定時,優先順序基於容器的方向:如果容器的方向是從左到右,則 left 優先。如果容器的方向是從右到左,則 right 優先。
HTML
html
<div id="parent">
Parent
<div id="noWidth">No width</div>
<div id="width">width: 100px</div>
</div>
CSS
css
div {
outline: 1px solid #cccccc;
}
#parent {
width: 200px;
height: 200px;
background-color: #ffc7e4;
position: relative;
}
/* declare both a left and a right */
#width,
#noWidth {
background-color: #c2ffd7;
position: absolute;
left: 0;
right: 0;
}
/* declare a width */
#width {
width: 100px;
top: 60px;
}
結果
規範
| 規範 |
|---|
| CSS 定位佈局模組第 3 級 # 內嵌 |
瀏覽器相容性
載入中…