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

浏览器兼容性

另见