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 级 # 内嵌 |
浏览器兼容性
加载中…