The top property in CSS is used to describe the top position of an element. The top property varies with the position of the element.
- If the position value is fixed or absolute, the element adjusts its top edge with respect to the top edge of its parent element or the block that holds it.
- If the position value is relative then the element is positioned with respect to its own current top edge.
- If the position value is static then the element does not have any effect due to the top property.
Syntax:
top: length| initial| inherit| auto;
Property Values: All the properties are described well with the example below.
length: This property is used to specify the top position of an element. The value of length can be negative, null, or positive. The value of length can be in form of px, cm, etc.
Example: This example illustrates the use of the length property that will use to sets the top edge position in px, cm, etc.
HTML
<!DOCTYPE html>
<html>
<head>
<title> CSS top Property </title>
<!-- CSS property used here -->
<style>
.gfg1 {
position: relative;
top: 00px;
width: 400px;
height: 250px;
border: 2px solid black;
}
.gfg2 {
position: absolute;
top: 50px;
border: 1px solid green;
}
.gfg3 {
position: relative;
top: 150px;
border: 1px solid green;
}
</style>
</head>
<body>
<div class="gfg1">
Main block with position: relative and top: 0px
<div class="gfg2">
Sub block-1 with position: absolute and top: 50px
</div>
<div class="gfg3">
Sub block-2 with position: relative and top: 150px
</div>
</div>
</body>
</html>
Output:

initial: It is used to set an element’s CSS property to its default value. The initial keyword can be used for any CSS property, and on any HTML element.
Example: This example illustrates the use of the initial property in order to set the value to its default value.
HTML
<!DOCTYPE html>
<html>
<head>
<title> CSS top Property </title>
<!-- CSS property used here -->
<style>
.gfg1 {
position: relative;
width: 400px;
height: 150px;
border: 2px solid black;
}
.gfg2 {
position: absolute;
top: 50px;
border: 1px solid green;
}
.gfg3 {
position: relative;
top: initial;
border: 1px solid green;
}
</style>
</head>
<body>
<div class="gfg1">
Main block with position: relative and top: 0px
<div class="gfg2">
Sub block-1 with position: absolute and top: 50px
</div>
<div class="gfg3">
Sub block-2 with position: relative and top: initial
</div>
</div>
</body>
</html>
Output:

inherit: This property is used to inherit a property to an element from its parent element property value. The inherit keyword can be used for inheriting any CSS property, and on any HTML element.
Example: This example illustrates the use of the inherit property, to inherit the properties from its parent element property value.
HTML
<!DOCTYPE html>
<html>
<head>
<title> CSS top Property </title>
<!-- CSS property used here -->
<style>
.gfg1 {
position: relative;
width: 400px;
height: 150px;
border: 2px solid black;
}
.gfg2 {
position: absolute;
top: 50px;
}
.gfg3 {
position: absolute;
top: inherit;
}
</style>
</head>
<body>
<div class="gfg1">
Main block with position: relative and top: 0px.
<div class="gfg2">
Sub block-1 with position: absolute and top: 50px.
<div class="gfg3">
Sub block-2 with position: absolute and top: inherit.
</div>
</div>
</div>
</body>
</html>
Output:

Supported Browsers: The browser supported by the top property are listed below:
- Google Chrome 1.0
- Internet Explorer 5.0
- Microsoft Edge 12.0
- Firefox 1.0
- Opera 6.0
- Safari 1.0
CSS top Property – FAQs
What does the top property do in CSS?
The top property in CSS sets the vertical position of a positioned element (relative, absolute, fixed, or sticky). It defines the distance between the top edge of the element and the top edge of its containing block.
How can I position an element 50px from the top of its container?
To position an element 50px from the top, use: position: absolute; top: 50px;. Ensure the containing block has a defined position other than static.
Does the top property work with position: static?
No, the top property has no effect when the position is set to static, which is the default value for all elements.
Can the top property accept negative values?
Yes, the top property can accept negative values, which will move the element upward beyond the top edge of its containing block.
How does top interact with bottom property in CSS?
The top and bottom properties can be used together, but their combined effect depends on the element’s height and the positioning context. They can be used to stretch or position the element within its container.
Similar Reads
CSS @charset Rule
The @charset rule specifies the character encoding used in the style sheet. The @charset must be the first element in the style sheet and if several @charset rules are defined then the only first one is used. It can not be used in the <style> element where the character set of the HTML page is
3 min read
CSS @keyframes Rule
The CSS @keyframes rule defines animations by specifying keyframes that describe the styles to be applied at various points during the animation duration. It allows for smooth transitions and transformations in web elements, controlled through percentages or from-to values. Note:For optimal browser
3 min read
CSS @media Rule
The @media CSS at-rule is used to apply a different set of styles for different media/devices using the Media Queries. A Media Query is mainly used to check the height, width, resolution, and orientation(Portrait/Landscape) of the device. This CSS rule is a way out for making more out of responsive
5 min read
CSS align-content property
The align-content property changes the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container. This property defines how each flex line is aligned within a flexbox and is only applicable if flex-wrap: wrap is applie
5 min read
CSS align-items Property
The align-items property in CSS is used to align flex items along the cross-axis within a flex container. It accepts values like flex-start, flex-end, center, baseline, and stretch, controlling the vertical alignment of items in a flexbox. Syntaxalign-items: normal | stretch | center | flex-start |
7 min read
CSS align-self Property
The align-self property in CSS is used to align the selected items in the flexible container in many different manners such as flex-end, center, flex-start, etc. Syntax: align-self: auto|normal|self-start|self-end|stretch|center |baseline, first baseline, last baseline|flex-start |flex-end|baseline
5 min read
CSS all Property
The all property in CSS is the shorthand property used to set all the element's values to their initial or inherited values or in some cases used to set the values to another spreadsheet origin. This property is used to reset all the CSS properties in a document. Syntax:Â all: initial|inherit|unset|
3 min read
CSS animation-delay Property
The animation-delay property specifies a delay for the start of an animation. Defined in seconds (s) or milliseconds (ms), this value determines how long to wait before beginning the animation. Negative values start the animation as if it had already been playing for the specified duration. Syntaxan
3 min read
CSS animation-direction Property
The animation-direction CSS property sets whether an animation should play forward, backward, or alternate back and forth between playing the sequence forward and backward. It controls the visual flow and repetition behavior of animations in web pages, enhancing dynamic content presentation. Syntaxa
4 min read
CSS animation-duration Property
The animation-duration property in CSS is essential for controlling the length of time an animation takes to complete one cycle, making it a vital tool for creating dynamic and engaging web designs. Syntax:animation-duration: time | initial | inherit;Property Value:time: This value is used to specif
3 min read