Which property would you use to make an element reside on top of another?

div {
  z-index: 1; /* integer */
}

The z-index property in CSS controls the vertical stacking order of elements that overlap. As in, which one appears as if it is physically closer to you. z-index only affects elements that have a position value other than static (the default).

Elements can overlap for a variety of reasons, for instance, relative positioning has nudged it over something else. Negative margin has pulled the element over another. Absolutely positioned elements overlap each other. All sorts of reasons.

Which property would you use to make an element reside on top of another?

Without any z-index value, elements stack in the order that they appear in the DOM (the lowest one down at the same hierarchy level appears on top). Elements with non-static positioning will always appear on top of elements with default static positioning.

Also note that nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be higher than element B.

Which property would you use to make an element reside on top of another?

Note that older version of IE get this context stuff a bit screwed up. Here’s a jQuery fix for that.

More Information

  • How z-index Works (screencast)
  • Rational z-index Values
  • Understanding z-index (MDN)

Browser Support

ChromeSafariFirefoxOperaIEAndroidiOS
Works Works Works Works 4+ 4+ Works

The position property can help you manipulate the location of an element, for example:

.element {
  position: relative;
  top: 20px;
}

Relative to its original position the element above will now be nudged down from the top by 20px. If we were to animate these properties we can see just how much control this gives us (although this isn’t a good idea for performance reasons):

relative is only one of six values for the position property. Here are the others:

Values

  • static: every element has a static position by default, so the element will stick to the normal page flow. So if there is a left/right/top/bottom/z-index set then there will be no effect on that element.
  • relative: an element’s original position remains in the flow of the document, just like the static value. But now left/right/top/bottom/z-index will work. The positional properties “nudge” the element from the original position in that direction.
  • absolute: the element is removed from the flow of the document and other elements will behave as if it’s not even there whilst all the other positional properties will work on it.
  • fixed: the element is removed from the flow of the document like absolutely positioned elements. In fact they behave almost the same, only fixed positioned elements are always relative to the document, not any particular parent, and are unaffected by scrolling.
  • sticky: the element is treated like a relative value until the scroll location of the viewport reaches a specified threshold, at which point the element takes a fixed position where it is told to stick.
  • inherit: the position value doesn’t cascade, so this can be used to specifically force it to, and inherit the positioning value from its parent.

Absolute

If a child element has an absolute value then the parent element will behave as if the child isn’t there at all:

.element {
  position: absolute;
}

And when we try to set other values such as left, bottom, and right we’ll find that the child element is responding not to the dimensions of its parent, but the document:

.element {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
}

To make the child element positioned absolutely from its parent element we need to set this on the parent element itself:

.parent {
  position: relative;
}

Now properties such as left, right, bottom and top will refer to the parent element, so that if we make the child element transparent we can see it sitting right at the bottom of the parent:

Learn more about position: relative and position: absolute at DigitalOcean.

Fixed

The fixed value is similar to absolute as it can help you position an element anywhere relative to the document, however this value is unaffected by scrolling. See the child element in the demo below and how, once you scroll, it continues to stick to the bottom of the page:

Desktop

ChromeFirefoxIEEdgeSafari
4 2 7 12 3.1

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
106 105 3 8

Sticky

The sticky value is like a compromise between the relative and fixed values. As of this writing, it is currently an experimental value, meaning it is not part of the official spec and only partially adopted by select browsers. In other words, it’s probably not the best idea to use this on a live production website.

What does it do? Well, it allows you to position an element relative to anything on the document and then, once a user has scrolled past a certain point in the viewport, fix the position of the element to that location so it remains persistently displayed like an element with a fixed value.

Take the following example:

.element {
  position: sticky; top: 50px;
}

The element will be relatively positioned until the scroll location of the viewport reaches a point where the element will be 50px from the top of the viewport. At that point, the element becomes sticky and remains at a fixed position 50px top of the screen.

The following demo illustrates that point, where the top navigation is default relative positioning and the second navigation is set to sticky at the very top of the viewport. Please note that the demo will only work in Chrome, Safari and Opera at the time of this writing.

Desktop

ChromeFirefoxIEEdgeSafari
91 59 No 91 7.1*

Mobile / Tablet

Android ChromeAndroid FirefoxAndroidiOS Safari
106 105 106 8*

Learn more about position: sticky at DigitalOcean.

More Information

Video on Feb 25, 2015

#110: Quick Overview of CSS Position Values

▶ Running Time: 13:34
Article on Jul 24, 2020

Absolute Positioning Inside Relative Positioning

Article on Aug 13, 2020

Absolute, Relative, Fixed Positioning: How Do They Differ?

Article on Sep 10, 2018

Creating sliding effects using sticky positioning

Article on Feb 25, 2019

Dealing with overflow and position: sticky;

Article on Feb 6, 2020

Getting Fancy with position: sticky;

Article on Sep 14, 2020

How to Use CSS Grid for Sticky Headers and Footers

Article on Jan 29, 2020
Article on Mar 5, 2020

Position Sticky and Table Headers

Article on Jun 21, 2017

position: sticky;

Article on Feb 4, 2019

More Like position: tricky;

Article on Apr 5, 2011

What if there was no position: static;?

How do you place an element on top of another element?

You can use the CSS position property in combination with the z-index property to overlay an individual div over another div element. The z-index property determines the stacking order for positioned elements (i.e. elements whose position value is one of absolute , fixed , or relative ).

Which property is used to make an element stick to the top of the page?

Introduction. You may have used the CSS position property with the relative and absolute values in the past. Modern web browsers now support the sticky value. It allows you to make elements stick when the scroll reaches a certain point.

What property must be used in order to use the top property?

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.

How do you move an element to the top in CSS?

You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document..
Move Left - Use a negative value for left..
Move Right - Use a positive value for left..
Move Up - Use a negative value for top..
Move Down - Use a positive value for top..