DigitalOcean provides cloud products for every stage of your journey. Get started with $200 in free credit!
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 thestatic
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 arelative
value until the scroll location of the viewport reaches a specified threshold, at which point the element takes afixed
position where it is told to stick.inherit
: theposition
value doesn’t cascade, so this can be used to specifically force it to, andinherit
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:
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
4 | 2 | 7 | 12 | 3.1 |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
126 | 127 | 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.
This browser support data is from Caniuse, which has more detail. A number indicates that browser supports the feature at that version and up.
Desktop
Chrome | Firefox | IE | Edge | Safari |
---|---|---|---|---|
91 | 59 | No | 91 | 7.1* |
Mobile / Tablet
Android Chrome | Android Firefox | Android | iOS Safari |
---|---|---|---|
126 | 127 | 126 | 8* |
Learn more about position: sticky
at DigitalOcean.
Very nice!
Lucky I pointed it out.
When I was younger and just starting Web Development, I could have used this guide to clearly lay out the differences of element positioning. This site is Awesome!
Very true. I am starting and this website has answers to all my questions so far and I have been able to understand clearly too. Yay
Hey Chriss your site has always given me the best answer, you are well prepared. Bravo!
Very Nice explanation of CSS positions. Thanks a lot.
Hey,
I used
position:
not the first time, but when i tried it, it didn’t work, so i googled about, found to this site and when i was skimming this site, i readz-index
….… AND this solved my problem!
Thanks a lot!
Here it is, 2015, and people still looking for this content because a lot of folks still on IE8. Unfortunately, what I’m looking for is how to get IE8 to behave with position:absolute. Position:relative doesn’t seem to let me add top/bottom/left/right to influence the position of something. Ah well, the quest continues. But this is awesome information and was very helpful. Thanks!
in chrome.
example Absolute
if set parent display:flex
child behaves unpredictably
so, how set child stable after text in parent, like in example, with flex parent?
Thanks for the articles! I used this information to fix up some positioning issues with a web site. You saved me some time and trouble. :)
One critique: You have said here and in one other article that the position property “does not cascade”. Did you mean that it does not inherit or is not inherited? Cascading and inheritance are not the same. I tripped up a bit over the idea of a property not cascading…
Sticky is awesome! Is there any word on whether this will lead
position
to being animatable? It’d be nice to help the sticky element transition visually once it changes to / from fixed.A fixed element is not always positioned relative to the document!
“When an ancestor has the transform, perspective, or filter property set to something other than none, that ancestor is used as the container instead of the viewport”
See: https://developer.mozilla.org/en-US/docs/Web/CSS/position
Thanks for the reference!
This is interesting! I was trying to achieve something like that: to have an element attached to the bottom of a scrollable container. However other than an element that’s fixed to the browser window the element that’s fixed to a “transformed” container will scroll with the rest of the container-contents.
Is there actually a way to have a real fixed position relative to another HTML element?
hi , what do you mean by performance issue ? about what exactly ? can specifically name this topic ? ( for further search )
Wow… very very help full. I came here to refresh my knowledge on positioning and was not disappointed. Concise, straight to the point and good examples.
I love that idea: “the element is removed from the flow”. That makes sense!
If anyone is interested, I’ve built a CSS position tester tool to visualize position changes immediately in a layout. https://kilianso.com/css_position
Thanks for the tool, Kilian. It’s super helpful to people like me who are just starting out.
The value “sticky” is experimental yet ?
Not really, it’s well supported:
https://caniuse.com/css-sticky
why absolute positioned element loses 100% width?