Top Property

Last updated: 2026-09-13

top Top Property

The top property defines the offset of a positioned element from the top edge of its containing block. It only applies to elements whose position is relative, absolute, fixed, or sticky, and is used to control an element's vertical position precisely.

Category
Initial Valueauto
InheritedNo
Applies Topositioned
Animation Typelength
CSS VersionCSS2
StatusStandard

📝 Syntax

top: auto | <length> | <percentage>;

⚙️ Values

ValueTypeDescription
autoKeywordNo offset.
<length> | <percentage>TypeAn offset value relative to the containing block.

▶ Example

Edit the code below and see the live result in the playground:

🌐 Browser Support

Browser ChromeChrome EdgeEdge FirefoxFirefox SafariSafari OperaOpera
top v1 v12 v1 v1 v1
Supported in all major browsers
Warning: top only affects positioned (non-static) elements; setting it on a position:static element has no effect.
Tip: A percentage is computed against the containing block's height; auto lets the browser resolve the position from other properties.

❓ FAQ

QWhat happens when both top and bottom are set?
AIt depends. For absolute or fixed elements without a height, the element stretches to fill the space between top and bottom, giving an adaptive height. With relative positioning, top wins and bottom is ignored. If a height is also set, top wins and bottom is ignored. This behavior is often used to stretch elements vertically inside a parent, such as full-height background layers.
QWhat is the top percentage relative to?
AThe percentage is computed against the containing block's height. For absolute elements the containing block is the padding box of the nearest positioned ancestor; for fixed it is the viewport; for relative it is the element's own original position. So top:50% places the element's top edge at half the containing block's height, often paired with transform:translateY(-50%) for vertical centering, since the transform shifts it by half of its own height.
QWhy isn't top working?
AThe most common reason is that the element is not positioned: top only works on elements with position relative, absolute, fixed, or sticky, and is ignored entirely on static, the default. Also check that the value is valid, that is, auto, length, or percentage, and that it is not overridden by properties such as height. Confirm position first, then verify the offset calculation.
QHow do I center vertically with top:50%?
ASet the element to position:absolute, with the parent position:relative, use top:50% so its top edge lands at the middle of the container, then apply transform:translateY(-50%) to shift it up by half its own height. That is vertical centering. Note that transform moves the element by 50% of its own height, which is different from how top's percentage works, and the two together are needed for true centering regardless of content size.

📚 Related Tutorials

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏