top - CSS: Cascading Style Sheets 编辑
The top
CSS property participates in specifying the vertical position of a positioned element. It has no effect on non-positioned elements.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
The effect of top
depends on how the element is positioned (i.e., the value of the position
property):
- When
position
is set toabsolute
orfixed
, thetop
property specifies the distance between the element's outer margin of top edge and the inner boarder of the top edge of its containing block. - When
position
is set torelative
, thetop
property specifies the distance the element's top edge is moved below its normal position. - When
position
is set tosticky
, thetop
property is used to compute the sticky-constraint rectangle. - When
position
is set tostatic
, thetop
property has no effect.
When both top
and bottom
are specified, position
is set to absolute
or fixed
, and height
is unspecified (either auto
or 100%
) both the top
and bottom
distances are respected. In all other situations, if height
is constrained in any way or position
is set to relative
, the top
property takes precedence and the bottom
property is ignored.
Syntax
/* <length> values */
top: 3px;
top: 2.4em;
/* <percentage>s of the height of the containing block */
top: 10%;
/* Keyword value */
top: auto;
/* Global values */
top: inherit;
top: initial;
top: unset;
Values
<length>
- A negative, null, or positive
<length>
that represents:- for absolutely positioned elements, the distance to the top edge of the containing block.
- for relatively positioned elements, the distance that the element is moved below its normal position.
<percentage>
- A
<percentage>
of the containing block's height. auto
- Specifies that:
- for absolutely positioned elements, the position of the element is based on the
bottom
property, whileheight: auto
is treated as a height based on the content; or ifbottom
is alsoauto
, the element is positioned where it should vertically be positioned if it were a static element. - for relatively positioned elements, the distance of the element from its normal position is based on the
bottom
property; or ifbottom
is alsoauto
, the element is not moved vertically at all.
- for absolutely positioned elements, the position of the element is based on the
inherit
- Specifies that the value is the same as the computed value from its parent element (which might not be its containing block). This computed value is then handled as if it were a
<length>
,<percentage>
, or theauto
keyword.
Formal definition
Initial value | auto |
---|---|
Applies to | positioned elements |
Inherited | no |
Percentages | refer to the height of the containing block |
Computed value | if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, auto |
Animation type | a length, percentage or calc(); |
Formal syntax
<length> | <percentage> | auto
Examples
A positioned element set 10% from the top
body {
background: beige;
}
div {
position: absolute;
top: 10%;
right: 40%;
bottom: 20%;
left: 15%;
background: gold;
border: 1px solid blue;
}
<div>The size of this content is determined by the position of its edges.</div>
Specifications
Specification | Status | Comment |
---|---|---|
CSS Positioned Layout Module Level 3 The definition of 'top' in that specification. | Working Draft | Adds behavior for sticky positioning. |
CSS Level 2 (Revision 1) The definition of 'top' in that specification. | Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
inset
, the shorthand for all related properties:top
,bottom
,left
, andright
- The mapped logical properties:
inset-block-start
,inset-block-end
,inset-inline-start
, andinset-inline-end
and the shorthandsinset-block
andinset-inline
position
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论