Skip to content
Tutorials Center
My learning

Topics

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".

CSS
.badge {
  position: absolute;
  top: 8px;
  right: 8px;
}

Try it

top with relative and absolute

Syntax

CSS
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 (auto stays auto when it cannot be resolved); for static elements, 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

  • relative shifts, it does not reflow. The element moves visually but its original space is kept, so neighbors do not move.
  • absolute needs a positioned ancestor. If none exists, the offset is measured from the initial containing block (roughly the page). Add position: relative to the intended parent.
  • top and bottom together. An absolutely positioned element with both set and height: auto stretches between them. If you also set a height, top wins in left-to-right horizontal writing modes and bottom is ignored.
  • Percentages resolve against the containing block's height, which must be definite for relative elements; otherwise the percentage behaves as auto.
  • For sticky, top is the threshold at which the element sticks, for example top: 0 for a header that stays visible while scrolling.
  • Prefer logical properties such as inset-block-start when your layout must work in vertical writing modes.
  • Animation: animating top triggers layout. For smooth motion prefer transform: translateY().

Browser support

Supported in every browser for many years.