CSS top Property

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The top property sets the vertical inset of a positioned element. It has no effect when position is static (the default).

With absolute or fixed, top is the distance from the containing block’s top edge. With relative, it moves the element down from its normal place. With sticky, it helps define the sticky constraint.

Standard-track.

Baseline widely available. Not deprecated as a property.

Limited support.

anchor() / anchor-size() values belong to CSS Anchor Positioning and are newer—skip them until you need that feature.

CSS Syntax

syntax.css
top: auto | <length> | <percentage> | initial | inherit;

Percentages refer to the height of the containing block. Negative lengths move the element upward.

Property Values

ValueDescriptionTry it
20px (absolute)Length from the containing block’s topTry it
20px (relative)Nudge down from normal flowTry it
0 (sticky)Stick when the scroll top is reachedTry it
10%Percentage of the containing block’s heightTry it
initialResets to autoTry it
inheritTakes the parent’s topTry it

How to Use top

Position the element first. Then set how far its top edge should move.

  1. Set position to relative, absolute, fixed, or sticky.
  2. For absolute children, give a parent position: relative (or another non-static value) so the containing block is clear.
  3. Add top with a length, percentage, or auto.
  4. Pair with left, right, or bottom when you need the other edges too.
positionWhat top means
staticIgnored
relativeOffset down from the normal position
absolute / fixedDistance to the containing block’s (or viewport’s) top
stickyInset used for the sticky constraint (for example stick 0 from the top)

Live Preview

Absolute box inside a relative parent. Change only top.

Parent is position: relative. The box is position: absolute.

Stage
Box

top: 20px

CSS Examples

Each snippet focuses on top. Use Try It Yourself for a live editor.

Example 1 — Absolute Offset

Pin a box 20px below the top of a relative parent.

CSS
<style>
  .parent {
    position: relative;
  }

  .box {
    position: absolute;
    top: 20px;
  }
</style>

<div class="parent">
  <div class="box">Box</div>
</div>
Try It Yourself

How It Works

  1. Create a containing block. position: relative on the parent anchors the absolute child.
  2. Take the box out of flow. position: absolute removes it from normal layout.
  3. Set the inset. top: 20px is the gap from the parent’s top padding edge.
  4. Without top. An absolute box with top: auto stays near its static place unless bottom is set.

Example 2 — Relative Nudge

Move a box down from its normal spot without removing its space in flow.

CSS
<style>
  .box {
    position: relative;
    top: 20px;
  }
</style>

<p>Above</p>
<div class="box">Box</div>
<p>Below</p>
Try It Yourself

How It Works

  1. Stay in flow. relative keeps the original layout space.
  2. Offset visually. top: 20px shifts the painted box downward.
  3. Neighbors unchanged. “Below” still sits where the unshifted box would leave it.
  4. Overlap tip. Large relative offsets can cover following content—use sparingly.

Example 3 — Sticky at the Top

Stick an element when scrolling reaches top: 0.

CSS
<style>
  .bar {
    position: sticky;
    top: 0;
  }
</style>

<div class="bar">Sticky bar</div>
<p>Scroll content…</p>
Try It Yourself

How It Works

  1. Enable sticky. position: sticky mixes relative flow with a stick point.
  2. Set the stick inset. top: 0 means stick when the top of the scrolling area is reached.
  3. Need a non-auto inset. Sticky on an axis requires a non-auto inset such as top.
  4. Scroll ancestor. Sticky uses the nearest scroll container—overflow on a parent can change the effect.

Edge Cases & Pitfalls

If top seems to do nothing, one of these is usually why.

static

Ignored on position: static

The default position is static. Set a non-static position before expecting any inset.

%

Percentages need a definite height

top: 10% uses the containing block’s height. If that height is auto/indefinite, the percentage may resolve to unexpected results.

bottom

top vs bottom conflict

On relative boxes (or when height is constrained), top wins and bottom is ignored. On absolute/fixed with height auto, both can stretch the element.

margin

Do not confuse with margin-top

margin-top pushes in the box model. top offsets a positioned element. They solve different problems.

sticky

Sticky needs a non-auto inset

Without a length/percentage on top (or the matching inset), sticky will not stick on that axis.

Browser Support

top is a browser CSS property for positioned layout insets. Node.js, Deno, and Bun do not apply it.

✓ Baseline · Widely available since Jul 2015

top

Length, percentage, and auto: all modern browsers and Internet Explorer. Anchor-positioning functions (anchor()) are newer and separate from everyday use.

97% Global support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 6+
Full support
Samsung Internet 1+
Full support
Internet Explorer 5+
Full support
top Widely supported

Bottom line: Safe everywhere for everyday positioning. Learn sticky and absolute containing blocks separately—those behaviors depend on position, not on top alone.

Key Takeaways

  • Role: vertical inset for positioned elements only.
  • Initial value: auto. Not inherited.
  • Prerequisite: non-static position.
  • Pitfall: percentages use containing-block height, not the element’s own height.

One line: set position first, then use top to place the element vertically.

Frequently Asked Questions

It sets the vertical inset of a positioned element. For absolute or fixed, that is the distance from the containing block’s top edge. For relative, it moves the element down from its normal spot.
auto. The browser does not apply a forced vertical inset until you set a length or percentage.
No. On position: static (the default), top has no effect. Use relative, absolute, fixed, or sticky first.
For absolute or fixed with height auto, both can stretch the box. For relative (or when height is fixed), top wins and bottom is ignored.
No. margin-top adds spacing in the box model. top offsets a positioned element relative to its containing block or normal position.
Universally supported in modern browsers and Internet Explorer for length, percentage, and auto. It is a browser CSS property, not a Node.js feature.

Next: transform

Learn how CSS transforms rotate, scale, and move elements visually.

transform tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful