CSS Reference ยท Positioning and Stacking
The top property
Reference for the CSS top property: how it offsets relative, absolute, fixed and sticky elements, what percentages mean, and how it pairs with bottom.
The top property sets the vertical offset of a positioned element, meaning any element whose position is not static. What the offset is measured from depends on the positioning scheme: for relative it is the element's own normal position, for absolute the padding edge of the nearest positioned ancestor, for fixed the viewport, and for sticky the edge of the scrollport that the element sticks to.
On an element with position: static (the default), top has no effect at all. This is the most common reason an offset "does nothing".
.badge { position: absolute; top: 8px; right: 8px; }
Try it
Syntax
top: auto | <length> | <percentage> | <anchor()> | <anchor-size()>;
- Initial value:
auto - Applies to: positioned elements
- Inherited: no
- Percentages: refer to the height of the containing block
- Animatable: yes, as a length or percentage
- Computed value: for positioned elements, an absolute length or a percentage as specified (
autostaysautowhen it cannot be resolved); forstaticelements,auto
Values
| Value | Description |
|---|---|
auto |
No offset is specified. For an absolutely positioned element with bottom: auto too, it stays where it would sit in normal flow. |
<length> |
A fixed distance, such as 12px or 2rem. Negative values move the element above the reference edge. |
<percentage> |
A percentage of the height of the containing block (for fixed, of the viewport). |
anchor(...) |
A position taken from an anchor element, used with anchor positioning. |
Usage notes
relativeshifts, it does not reflow. The element moves visually but its original space is kept, so neighbors do not move.absoluteneeds a positioned ancestor. If none exists, the offset is measured from the initial containing block (roughly the page). Addposition: relativeto the intended parent.topandbottomtogether. An absolutely positioned element with both set andheight: autostretches between them. If you also set a height,topwins in left-to-right horizontal writing modes andbottomis ignored.- Percentages resolve against the containing block's height, which must be definite for
relativeelements; otherwise the percentage behaves asauto. - For
sticky,topis the threshold at which the element sticks, for exampletop: 0for a header that stays visible while scrolling. - Prefer logical properties such as
inset-block-startwhen your layout must work in vertical writing modes. - Animation: animating
toptriggers layout. For smooth motion prefertransform: translateY().
Browser support
Supported in every browser for many years.