CSS top Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Layout

What You’ll Learn

The top property controls vertical offset for positioned elements — essential for layouts using absolute, relative, fixed, and sticky positioning.

01

Vertical

Move down.

02

position

Required pair.

03

px / %

Length units.

04

absolute

Inside parent.

05

fixed

Viewport lock.

06

sticky

Scroll stick.

Introduction

The top property in CSS is used to specify the vertical position of a positioned element. This property works in conjunction with position values such as absolute, relative, fixed, or sticky.

By setting the top property, you can control the distance between the top edge of the element and the top edge of its containing element.

Definition and Usage

Use top when you need precise vertical placement — for example, placing a badge inside a card, pinning a navbar with position: fixed; top: 0;, or nudging an element with position: relative.

💡
Beginner Tip

top does nothing on position: static elements. Always set a non-static position value first.

📝 Syntax

The syntax for the top property is straightforward and can be applied to any positioned element.

syntax.css
element {
  position: position-value;
  top: value;
}

Here, position-value can be absolute, relative, fixed, or sticky, and value can be a length (e.g., px, em), a percentage (%), or other valid CSS units.

Basic Example

top.css
.box {
  position: absolute;
  top: 50px;
}

Syntax Rules

  • Requires position other than static.
  • Positive values move the element down from the top edge of its containing block.
  • Negative values move the element upward.
  • Percentages are relative to the height of the containing block.
  • The property is not inherited.

Related Properties

  • position — enables offset properties like top
  • left, right, bottom — other inset offset properties
  • inset — shorthand for top, right, bottom, and left
  • z-index — controls stacking order of positioned elements

🎯 Default Value

The default value of the top property is auto, which means the browser will calculate the top position of the element.

⚡ Quick Reference

QuestionAnswer
Default valueauto
Offset 50px downposition: absolute; top: 50px;
Pin to viewport topposition: fixed; top: 0;
Sticky header thresholdposition: sticky; top: 0;
Works on static?No
InheritedNo

💎 Property Values

The top property accepts length, percentage, and keyword values.

ValueExampleDescription
lengthtop: 50px;Specifies the top position in length units such as px, em, rem, etc.
percentagetop: 25%;Specifies the top position as a percentage of the containing element’s height.
autotop: auto;Default value. The browser calculates the top position.
initialtop: initial;Sets the property to its default value.
inherittop: inherit;Inherits the value from the parent element.
auto px % em

When to Use top

top is essential whenever you need vertical control in positioned layouts:

  • Overlays and badges — Place elements inside cards with position: absolute; top: ...
  • Fixed headers — Keep navigation at the viewport top with top: 0
  • Sticky sections — Define when sticky elements stick using a top threshold
  • Micro-adjustments — Nudge icons or labels with position: relative; top: -2px;

👀 Live Preview

Three absolutely positioned boxes at different top values inside the same container:

Container

Blue: top: 20px · Purple: top: 50px · Green: top: 80px

Examples Gallery

In this example, we’ll position a box 50 pixels from the top of its containing element using the top property with position: absolute.

📜 Basic Positioning

Place elements vertically inside a positioned container.

Example 1 — Absolute box with top: 50px

In this example, we’ll position a box 50 pixels from the top of its containing element using the top property with position: absolute.

index.html
<style>
  .container {
    position: relative;
    width: 200px;
    height: 200px;
    background-color: lightgray;
  }
  .box {
    position: absolute;
    top: 50px;
    width: 100px;
    height: 100px;
    background-color: blue;
  }
</style>

<div class="container">
  <div class="box"></div>
</div>
Try It Yourself

How It Works

The parent has position: relative as the containing block. The child sits 50px below the parent’s top edge.

Example 2 — Relative nudge with top

Use position: relative with a small top value to shift an element from its normal spot without removing it from flow.

relative.css
.icon {
  position: relative;
  top: -3px;
}
Try It Yourself

How It Works

A negative top moves the icon upward 3px while the element still occupies its original space in the layout.

📄 Layout Patterns

Use top with fixed and sticky positioning for common UI patterns.

Example 3 — Fixed bar at top: 0

Pin a toolbar to the top of the viewport so it stays visible while scrolling.

fixed.css
.toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 0.75rem 1rem;
  background: #1e293b;
  color: #fff;
}
Try It Yourself

How It Works

position: fixed with top: 0 anchors the bar to the viewport top, independent of page scroll.

Example 4 — Sticky heading with top: 0

Sticky elements stick when scrolled past a threshold defined by top.

sticky.css
.section-title {
  position: sticky;
  top: 0;
  background: #eff6ff;
  padding: 0.5rem;
}
Try It Yourself

How It Works

The heading behaves as relative until scroll reaches the threshold, then sticks at 0px from the top of the scroll area.

♿ Accessibility

  • Fixed headers — Ensure fixed top bars do not hide focused content; add padding to the page body.
  • Keyboard focus — Offsets should not move focusable elements off-screen unexpectedly.
  • Zoom and mobile — Test that fixed top: 0 bars remain usable at larger text sizes.
  • Motion sensitivity — Sticky behavior is generally fine, but avoid layout jumps that disorient users.

top requires position

The offset properties top, right, bottom, and left only apply when position is relative, absolute, fixed, or sticky.

position-top.css
.parent { position: relative; }
.child {
  position: absolute;
  top: 1rem;
  left: 1rem;
}

🧠 How top Works

1

Set position

Choose absolute, relative, fixed, or sticky on the element.

position
2

Define top offset

Set a length, percentage, or leave as auto.

Offset
3

Browser positions element

The top edge moves relative to the containing block or viewport.

Layout
=

Precise vertical placement

Elements land exactly where your layout needs them.

Browser Compatibility

The top property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also supported in older browsers, making it a reliable property to use for positioning elements.

Universal · All browsers

Core layout property

top has been part of CSS positioning since the earliest layout models and works everywhere.

99% Browser support
Google Chrome 1+ · Desktop & Mobile
Full support
Mozilla Firefox 1+ · Desktop & Mobile
Full support
Apple Safari 1+ · macOS & iOS
Full support
Microsoft Edge 12+ · All versions
Full support
Opera 4+ · All versions
Full support

Testing tip

Test sticky and fixed top values on mobile Safari and Chrome — viewport UI can affect visible placement.

top property 99% supported

Bottom line: top is one of the most dependable CSS properties for vertical positioning.

Conclusion

The top property is a fundamental tool for web developers to control the vertical positioning of elements.

Whether you are creating a complex layout or simply adjusting the position of a single element, understanding and using the top property effectively can enhance the design and functionality of your website. Experiment with different values and see how this property can be used to create precise and dynamic layouts.

💡 Best Practices

✅ Do

  • Set a non-static position before using top
  • Use position: relative on parents for absolute children
  • Combine with left, right, or bottom for full placement
  • Add body padding when using fixed top headers
  • Prefer em or rem for scalable sticky thresholds

❌ Don’t

  • Expect top to work on position: static elements
  • Confuse top with margin-top for layout spacing
  • Forget a positioned ancestor for absolute elements
  • Overlap fixed bars with important page content

Key Takeaways

Knowledge Unlocked

Five things to remember about top

Use these points when positioning elements vertically in CSS.

5
Core concepts
02

auto

Default value.

Default
📌 03

position

Required pair.

Rule
📏 04

px / %

Length values.

Values
🔒 05

fixed top: 0

Pin to viewport.

Pattern

❓ Frequently Asked Questions

top sets how far an element's top edge is offset from the top of its containing block. It works with position values other than static.
The default is auto, which lets the browser calculate the element's vertical position.
No. top has no effect on static elements. Use position: relative, absolute, fixed, or sticky first.
top moves positioned elements relative to their containing block. margin-top adds space in normal flow or affects the box model spacing.
No, top is not inherited. Each element sets its own offset when positioned.

Practice in the Live Editor

Open the HTML editor and experiment with top values on an absolutely positioned box.

HTML Editor →

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.

5 people found this page helpful