当元素处于绝对位置或固定位置(即 position:absolute; 和 position:fixed; )时, right 属性指定元素的右边缘与其包含块(元素相对放置的祖先)的右边缘之间的距离。
如果元素是相对放置的(即 position:relative; ),则 right 属性会将元素的右边缘从其正常位置设置为左/右。
如果 position 设置为 sticky,,例如 position:sticky; ,则定位上下文是视口。当元素在视口中时, right 属性的行为就像其位置是相对的。当元素位于外部时, right 属性的行为就像其位置是固定的。
left: auto | length | percentage | initial | inherit;
<!DOCTYPE html>
<html>
<head>
<title>
CSS right Property
</title>
<style>
div{
position: absolute;
width: 200px;
height: 100px;
font-size: 40px;
}
#len {
right: 200px;
border: 5px solid lightgreen;
}
#per {
right: 50%;
border: 5px solid blue;
}
#auto {
right: auto;
border: 10px solid red;
}
#init {
right: initial;
border: 5px solid yellow;
}
</style>
</head>
<body>
<h1> Example of the right Property </h1>
<div id = "len"> right: 200px; </div>
<div id = "per"> right: 50%; </div>
<div id = "auto"> right: auto; </div>
<div id = "init"> right: initial; </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>
CSS right Property
</title>
<style>
div{
position: relative;
width: 250px;
height: 100px;
font-size: 40px;
}
#len {
right: -100px;
border: 5px solid lightgreen;
}
#per {
right: -30%;
border: 5px solid blue;
}
#auto {
right: auto;
border: 5px solid red;
}
#init {
right: initial;
border: 5px solid lightblue;
}
</style>
</head>
<body>
<h1> Example of the right Property </h1>
<div id = "len"> right: -100px; </div>
<div id = "per"> right: -30%; </div>
<div id = "auto"> right: auto; </div>
<div id = "init"> right: initial; </div>
</body>
</html>