right

Baseline 廣泛可用 *

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

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

right CSS 屬性用於指定定位元素的水平位置。此嵌入屬性對非定位元素沒有影響。

試一試

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>

<percentage>

包含塊寬度的<percentage>

auto

指定

  • 對於絕對定位元素,元素的位置基於 left 屬性,而 width: auto 被視為基於內容的寬度;如果 left 也為 auto,則元素水平定位在其應有的位置,就像它是一個靜態元素一樣。
  • 對於相對定位元素,元素與其正常位置的距離基於 left 屬性;如果 left 也為 auto,則元素根本不會水平移動。

描述

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

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

leftright 都定義時,如果其他屬性未阻止這樣做,元素將拉伸以同時滿足兩者。如果元素無法拉伸以滿足兩者——例如,如果聲明瞭 width——則元素的位置會過度約束。在這種情況下,當容器是從左到右時,left 值具有優先順序;當容器是從右到左時,right 值具有優先順序。

正式定義

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

正式語法

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

當同時宣告 leftright 時,元素將拉伸以滿足兩者,除非其他約束阻止它這樣做。如果元素不會拉伸或縮小以滿足兩者。當元素的位置過度指定時,優先順序基於容器的方向:如果容器的方向是從左到右,則 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 級
# 內嵌

瀏覽器相容性

另見