The Wayback Machine - https://web.archive.org/web/20240927132057/https://www.geeksforgeeks.org/css-top-property/
Open In App

CSS top Property

Last Updated : 28 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Previous Article
Next Article

Similar Reads

Which property specifies the top margin of an element in CSS ?
In this article, we are going to discuss the margin-top property that specifies the top margin of an element. In order to specify the top margin of an element, CSS provides a styling element called margin-top which sets the top margin of an element. This property accepts an integer value. The default value is 0 and negative values are also allowed.
2 min read
CSS scroll-margin-top property
The scroll-margin-top property is used to set all the scroll margins to the top of an element at once. The value specified for the scroll-margin-top determines how much of the page that is primarily outside the support should remain visible. Hence, the scroll-margin-top values represent outsets that define the scroll snap area that is used for snap
2 min read
CSS border-top-color Property
border-color property is used to color all borders of the container with the same color.  border-top-color property provide option to fill different color from parent border-color to top border of the container. Note: Take care of order while writing the statement in CSS, i.e CSS will take last value of the property, So, for expected output always
3 min read
CSS border-top Property
The border-top property in CSS is used to set all top border properties in one line. This property is used to set the width, style, and color of the top border. Syntax: border-top: border-width border-style border-color|initial|inherit;Property Values: border-width: It is used to set the width of the border.border-style: It is used to set the style
2 min read
CSS border-top-left-radius Property
In CSS the border-top-left-radius property is used to specify the radius of the top left corner of an element. Note: The border rounding can be a circle or an ellipse depending on the value of the property. If the value is 0 then there is no change in the border, it remains a square border. Syntax: border-top-left-radius: value;Default Value: It ha
2 min read
CSS border-top-right-radius Property
The border-top-right-radius property in CSS is used to define the radius of the right top corner of the border of a given element. The border-top-right-radius is used to draw a rounded corner at the top-right part of the container. The border-radius is used to set all radius corners of the container at once with the same value of the radius, border
3 min read
CSS border-top-style Property
The border-top style property is used to specify the style of the top border. Syntax: border-top-style: none | dotted | dashed | solid | groove | inset | outset | ridge | double | hidden | initial | inherit;Default Value: The default value is none. Property Values None: It is the default value and it makes the width of the top border zero. Hence, i
5 min read
CSS border-top-width Property
The border-top-width property in CSS is used to set a specific width to the top border of an element. The border-top-style or border-style property is used for the element before using the border-top-width property. Default Value: medium Syntax: border-top-width: length|thin|medium|thick|initial|inherit;Property Values: The border-top-width propert
2 min read
CSS margin-top Property
The margin-top property in CSS is used to set the top margin of an element. It sets the margin area on the top of the element. The default value of the margin-top property is 0. Syntax: margin-top: length|auto|initial|inherit;Property values: length: It is used to specify the length of the margin with a fixed value. This value can be positive, nega
4 min read
CSS padding-top Property
Padding is the space between its content and border. The padding-top property in CSS is used to set the width of the padding area on the top of an element. Syntax: padding-top: length|percentage|initial|inherit;Property Values: length: This mode is used to specify the size of padding as a fixed value. The size can be set in form of px, cm etc. The
3 min read
CSS transition-property Property
The transition effect is used to display the change in the property of an element over a specified duration. The transition-property property is used to specify the name of the CSS property for which the transition effect will occur. Syntax: transition-property: none | all | property | initial | inherit;Property values: none: This value is used to
5 min read
Top 10 Tailwind CSS Templates and Themes
CSS adds visual weight to our websites. It makes them look, smoother, better, and easier to use. But writing CSS can be a headache. Thankfully there are a lot of CSS frameworks out there – like Tailwind, Bootstrap, Foundation, Spectre, Bulma, etc. for creating and rendering beautiful user interfaces. Tailwind CSS is basically a utility-first CSS fr
5 min read
Primer CSS Popover Top-left
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
3 min read
Primer CSS Popover Left-top
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
3 min read
CSS | Scroll Padding top
The Scroll Padding top is an inbuilt property in Scroll Snap module. This property set the scroll padding top longhand. This property acts as a magnet on the top of the sliding element that stick to the top of the view-port and stop the scrolling(forcefully) in that place. The Scroll Padding property is optional, this property is used when the Scro
2 min read
How to remove CSS “top” and “left” attribute with jQuery ?
Method 1: Using the css() method: The css() method is used to get or set CSS properties of a selected element. It takes two arguments, the first argument is the property that has to be set and the second argument is the value that it has to be set to. The 'top' value can be effectively disabled by using the 'initial' value which would set the prope
3 min read
Foundation CSS Top Bar Advanced Layout
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
4 min read
How to set the shape of top-left corner of div using CSS ?
In this article, we will learn How to set the shape of the top-left corner of a div using CSS. The border-top-left-radius property is used to form the shape of the top-left corner. We can use percentages to shape the top-left corner, for that, you can check this article How to set the shape of the top-left corner in percentage? Approach: The border
1 min read
Tailwind CSS Top/Right/Bottom/Left
These classes accept many values in tailwind CSS in which all the properties are covered in class form. These are the alternative to the CSS Top/Right/Bottom/Left properties. These classes are used to control the alignment of a positioned element. Remember we can use these properties only with positioned elements. Top/Right/Bottom/Left classes: .in
3 min read
Foundation CSS Top Bar
Foundation CSS is an open-source and responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to layout stunning responsive websites, apps, and emails that appear amazing and can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is bu
4 min read
Top 10 Tailwind CSS Plugins
Tailwind CSS: This is called utility-first CSS framework which is used for building the custom UI It is a very customizable and low-level CSS framework that gIt provides you with the building blocks that help you to build great designs without any annoying styles. The top 10 Tailwind CSS Plugins are given below: 1. Tailwind CSS Typography: If you w
4 min read
Foundation CSS Kitchen Sink Top Bar
Foundation CSS is the frontend framework of CSS that is used to build responsive websites, apps, and emails that work perfectly on any device. It is written using HTML, CSS, and Javascript and is used by many famous companies like Amazon, Facebook, eBay, etc. It uses packages like Grunt and Libsass for fast coding and controlling and Saas compiler
2 min read
Foundation CSS Top Bar Basics
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
3 min read
Foundation CSS Top Bar Stacking
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
3 min read
Foundation CSS Tooltip Top
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails to look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is b
3 min read
Primer CSS Popover Right-top
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by Object-Oriented CSS principles, functional CSS, an
3 min read
Primer CSS Top-right
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by object-oriented CSS principles, functional CSS, an
3 min read
How can I stack two arrow images (upvote/downvote) on top of eachother using CSS ?
CSS (Cascading Style Sheets) is a stylesheet language used to design a webpage to make it attractive. The reason for using this is to simplify the process of making web pages presentable. It allows you to apply styles on web pages. More importantly, it enables you to do this independent of the HTML that makes up each web page. In this article, we w
2 min read
Onsen UI CSS Component Popover (top)
Popovers are used to notify the user about what notes a user should know before the proceeds further. It can also be used as a notification, like supposing the user has no idea what is going on in any particular section, then this popover can help or guide him/her. Onsen UI CSS Component offers us to create a self-designed popover. There are a lot
3 min read
Onsen UI CSS Component Bordered Top Tabbar
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. Tabbar is used to create the Tabs using Onsen UI classes. Onsen UI CSS Component Bordered Top Tabbar is used to create the bordered tabbar at the top using Onsen UI Tabbar
2 min read