定位position
Setting an element is positioned in a document.
Overview
| Class | Declarations |
|---|---|
position:`type` | position: `type`;
|
rel | position: relative;
|
abs | position: absolute;
|
static | position: static;
|
fixed | position: fixed;
|
sticky | position: sticky;
|
Positioning elements as static
Use staticto position an element according to the normal flow of the document. All HTML elements are positioned static by default.
static parentabs child<div class="static p:20"> <div>static parent</div> <div class="abs left:10 bottom:10">abs child</div></div>Positioning elements as relative
Use relto position an element according to the normal flow of the document, and then offset relative to itself based on the values of top, right, bottom, and left.
relative parent<div class="rel p:20 top:30 left:20"> <div>relative parent</div></div>Positioning elements as absolute
Use absto remove an element from the normal flow of the document, and the element positioned relative to its closest positioned ancestor.
relative parentabs child<div class="rel p:12x"> <div>relative parent</div> <div class="abs left:0 top:0">abs child</div></div>Positioning elements as fixed
Use fixedto remove an element from the normal flow of the document. The element positioned relative to the viewport, and it is always stays in the same place.
aron3:03 a.mFront-end needs a revolution
joy7:38 p.mShilin night market later 🥰 ?
miles5 mins agotask done 🙌🏻
benseage3 hours agoEasy game easy life
lola11 seconds agoLet me write the doc !
<div> <input type="search" class="fixed"> <ul> <li> <div class="rel"> <img src="…"> </div> <div> <div class="flex"> <div>Aron</div> <div>3:03 a.m</div> </div> <p>Front-end needs a revolution</p> </div> </li> … </ul></div>Positioning elements as sticky
Use stickyto position an element base on scroll position. It's like place the element to a relative position until a given offset position is met and then changing the element to a fixed position.
aron3:03 a.mFront-end needs a revolution
joy7:38 p.mShilin night market later 🥰 ?
miles5 mins agotask done 🙌🏻
benseage3 hours agoEasy game easy life
lola11 seconds agoLet me write the doc !
<ul class="overflow:auto"> … <li class="sticky top:0"> <img src="…"> <div>Joy</div> … </li> …</ul>Apply conditionally
Apply styles based on different states using selectors and conditional queries.
<div class="rel:hover rel@sm rel@dark rel@print">…</div>