aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/css/right/index.html
blob: 79559cebac600e6950132d70845ae74afc0ef090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
---
title: right
slug: Web/CSS/right
translation_of: Web/CSS/right
---
<div>{{CSSRef}}</div>

<p><strong><code>right</code></strong><a href="https://developer.mozilla.org/zh-CN/docs/Web/CSS">样式</a>属性定义了定位元素的右外边距边界与其包含块右边界之间的偏移,非定位元素设置此属性无效。</p>

<pre class="brush: css"><code>/* &lt;length&gt; values */
right: 3px;
right: 2.4em;

/* &lt;percentage&gt;s of the width of the containing block */
right: 10%;

/* Keyword value */
right: auto;

/* Global values */
right: inherit;
right: initial;
right: unset;</code>
</pre>

<p><code>right</code>的效果取决于元素的<code>position</code>属性:</p>

<ul>
 <li><code>position</code>设置为<code>absolute</code><code>fixed</code>时,<code>right</code>属性指定了定位元素右外边距边界与其包含块右边界之间的偏移。</li>
 <li><code>position</code>设置为<code>relative</code>时,<code>right</code>属性指定了元素的右边界离开其正常位置的偏移。</li>
 <li><code>position</code>设置为<code>sticky</code>时,如果元素在viewport里面,<code>right</code>属性的效果和position为<code>relative</code>等同;如果元素在viewport外面,<code>right</code>属性的效果和position为<code>fixed</code>等同。</li>
 <li><code>position</code>设置为<code>static</code>时,<code>right</code>属性无效。</li>
</ul>

<p>{{cssxref("left")}}​​​​​​和<font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">right</span></font>同时指定时,元素的位置会被重复指定。当容器是从左到右时,<code>left</code>的值会被优先设定;当容器是从右到左时,<code>right</code>的值会被优先设定。</p>

<p>{{cssinfo}}</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<h3 id="取值">取值</h3>

<dl>
 <dt>{{cssxref("&lt;length&gt;")}}</dt>
 <dd>可以是负的,正的 或者 null {{cssxref("&lt;length&gt;")}} 表示:
 <ul>
  <li>对于<strong>绝对定位的元素</strong>,元素右外边距边界与其包含块右边界之间的偏移。</li>
  <li>对于<strong>相对定位定位的元素,</strong>元素的右边界离开其正常位置的偏移。</li>
 </ul>
 </dd>
 <dt>{{cssxref("&lt;percentage&gt;")}}</dt>
 <dd>代表元素包含块的宽度的百分比 {{cssxref("&lt;percentage&gt;")}}</dd>
 <dt><code>auto</code></dt>
 <dd>这个关键字表示:
 <ul>
  <li>对于绝对定位元素,元素将忽略此属性而以{{ Cssxref("left") }}属性为准,如果此时设置<code>width: auto</code>,将基于内容需要的宽度设置宽度;如果<code>left</code>也为<code>auto</code>的话,元素的水平位置就是它假如作为静态(即static)元素时该在的位置。</li>
  <li>对于相对定位元素,元素相对正常位置的偏移量将基于{{ Cssxref("left") }}属性;如果<code>left</code>也为 <code>auto</code>的话,元素将不会有偏移。</li>
 </ul>
 </dd>
 <dt><code>inherit</code></dt>
 <dd>这个关键字表示该值与其父元素 (可能不是它的包含块) 的计算值相同。对这个计算值的处理将和它原本为 {{cssxref("&lt;length&gt;")}}{{cssxref("&lt;percentage&gt;")}}<code>auto</code> 一样。</dd>
</dl>

<h3 id="Formal_syntax">Formal syntax</h3>

<pre class="syntaxbox">{{csssyntax}}</pre>

<h2 id="Examples" name="Examples"><strong>示例</strong></h2>

<pre class="brush: css; highlight:[16]">#example_3 {
  width: 100px;
  height: 100px;
  background-color: #FFC7E4;
  position: relative;
  top: 20px;
  left: 20px;
}

#example_4 {
  width: 100px;
  height: 100px;
  background-color: #FFD7C2;
  position: absolute;
  bottom: 10px;
  right: 20px;
}</pre>

<pre class="brush: html">&lt;div id="example_3"&gt;Example 3&lt;/div&gt;
&lt;div id="example_4"&gt;Example 4&lt;/div&gt;
</pre>

<p>{{ EmbedLiveSample('Examples', 500, 220) }}</p>

<h2 id="Specifications" name="Specifications">规范</h2>

<table>
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSS3 Transitions', '#animatable-css', 'right')}}</td>
   <td>{{Spec2('CSS3 Transitions')}}</td>
   <td>Defines <code>right</code>as animatable.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS3 Positioning', '#propdef-right', 'right')}}</td>
   <td>{{Spec2('CSS3 Positioning')}}</td>
   <td>Adds behavior for sticky positioning.</td>
  </tr>
  <tr>
   <td>{{SpecName('CSS2.1', 'visuren.html#propdef-right', 'right')}}</td>
   <td>{{Spec2('CSS2.1')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

<div class="hidden">
<p>The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
</div>

<p>{{Compat("css.properties.right")}}</p>

<div id="compat-mobile"> </div>