top
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2015å¹´7æâ©.
* Some parts of this feature may have varying levels of support.
topCSS 屿§å®ä¹äºå®ä½å
ç´ çä¸å¤è¾¹è·è¾¹çä¸å
¶å
å«åä¸è¾¹çä¹é´çåç§»ï¼éå®ä½å
ç´ è®¾ç½®æ¤å±æ§æ æã
å°è¯ä¸ä¸
top: 0;
top: 4em;
top: 10%;
top: 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;
}
topçææåå³äºå
ç´ çposition屿§ï¼
- å½
position设置为absoluteæfixedæ¶ï¼top屿§æå®äºå®ä½å ç´ ä¸å¤è¾¹è·è¾¹çä¸å ¶å å«åä¸è¾¹çä¹é´çåç§»ã - å½
position设置为relativeæ¶ï¼top屿§æå®äºå ç´ çä¸è¾¹ç离å¼å ¶æ£å¸¸ä½ç½®çåç§»ã - å½
position设置为stickyæ¶ï¼å¦æå ç´ å¨ viewport éé¢ï¼top屿§çææå position 为relativeçåï¼å¦æå ç´ å¨ viewport å¤é¢ï¼top屿§çææå position 为fixedçåã - å½
position设置为staticæ¶ï¼top屿§æ æã
å½topåbottomåæ¶æå®æ¶ï¼å¹¶ä¸ height没æè¢«æå®æè
æå®ä¸ºautoçæ¶åï¼topåbottom é½ä¼çæï¼å¨å
¶ä»æ
åµä¸ï¼å¦æ height被éå¶ï¼åtop屿§ä¼ä¼å
设置ï¼bottom屿§åä¼è¢«å¿½ç¥ã
| åå§å¼ | auto |
|---|---|
| éç¨å ç´ | positioned elements |
| æ¯å¦æ¯ç»§æ¿å±æ§ | å¦ |
| Percentages | refer to the height of the containing block |
| 计ç®å¼ | if specified as a length, the corresponding absolute length; if specified as a percentage, the specified value; otherwise, auto |
| å¨ç»ç±»å | a length, percentage or calc(); |
è¯æ³
/* <length> å¼ */
top: 3px;
top: 2.4em;
top: anchor(bottom);
top: calc(anchor(--myAnchor 50%) + 10px);
/* ç¸å¯¹äºå
å«åºåé«åº¦ç <percentage> */
top: 10%;
/* å
³é®åå¼ */
top: auto;
/* å
¨å±å¼ */
top: inherit;
top: initial;
top: revert;
top: revert-layer;
top: unset;
åå¼
<length>-
å¯ä»¥æ¯è´çãnull ææ£ç
<length>å¼ï¼ç¨äºè¡¨ç¤ºï¼- 对äºç»å¯¹å®ä½çå ç´ ï¼å ç´ ä¸å¤è¾¹è·è¾¹çä¸å ¶å å«åä¸è¾¹çä¹é´çåç§»ã
- 对äºç¸å¯¹å®ä½å®ä½çå ç´ ï¼å ç´ çä¸è¾¹ç离å¼å ¶æ£å¸¸ä½ç½®çåç§»ã
<percentage>-
代表å ç´ å å«åçé«åº¦çç¾åæ¯
<percentage>ã auto-
表示ï¼
- 对äºç»å¯¹å®ä½å
ç´ ï¼å
ç´ çå®ä½åºäº
bottom屿§ï¼height: auto被è§ä¸ºåºäºå 容çé«åº¦ï¼æè ï¼å¦æbottom乿¯autoï¼åå ç´ å°æå ¶ä½ä¸ºéæå ç´ æ¶åºåç´å®ä½çä½ç½®è¿è¡å®ä½ã - 对äºç¸å¯¹å®ä½å
ç´ ï¼å
ç´ ç¸å¯¹äºå
¶æ£å¸¸ä½ç½®çåç§»éåºäº
bottom屿§ï¼å¦æbottom乿¯autoï¼åå ç´ å°ä¸ä¼å¨åç´æ¹åä¸ç§»å¨ã
- 对äºç»å¯¹å®ä½å
ç´ ï¼å
ç´ çå®ä½åºäº
inherit-
表示该å¼ä¸å ¶ç¶å ç´ ï¼å¯è½ä¸æ¯å®çå å«åï¼ç计ç®å¼ç¸åã对è¿ä¸ªè®¡ç®å¼çå¤çå°åå®åæ¬ä¸º
<length>ã<percentage>æautoå ³é®å䏿 ·ã
示ä¾
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>
è§è
| Specification |
|---|
| CSS Positioned Layout Module Level 3 > # insets > |