CSS: Elements positioning and layering
“Static positioning” refers to the normal flow of the page that we’ve been working with up ’til this point. The other three types of positioning are “relative”, “absolute”, and “fixed”.
The CSS position
property lets you alter the positioning scheme of a particular element. Its default value, as you might imagine, is static
. When an element’s position
property doesn’t have a value of static
, it’s called a “positioned element”. Positioned elements are what this entire chapter is about.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Fpositioned-elements-terminology-861fca.png&width=768&dpr=4&quality=100&sign=2cb85c04&sv=1)
relative positioning
“Relative positioning” moves elements around relative to where they would normally appear in the static flow of the page. This is useful for nudging boxes around when the default flow is just a little bit off.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Fcss-relative-positioning-26842e.png&width=768&dpr=4&quality=100&sign=d84167a&sv=1)
Let’s turn the .item-relative
element in schemes.html
into a relatively positioned element. Add the following rule to styles.css
:
The position: relative;
line makes it a positioned element, and the top
and left
properties let you define how far it’s offset from its static position. This is sort of like setting an (x, y) coordinate for the element.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Frelative-positioning-screenshot-4c23c2.png&width=768&dpr=4&quality=100&sign=dad1059b&sv=1)
Relative positioning works similarly to margins, with one very important difference: neither the surrounding elements or parent element are affected by the top
and left
values. Everything else renders as if .item-relative
was in its original position. Think of the offsets as being applied after the browser finishes laying out the page.
The top
and left
properties measure from the original box’s top and left edges, respectively. We can offset relative to the other edges with the bottom
and right
properties.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Frelative-positioning-offsets-494268.png&width=768&dpr=4&quality=100&sign=ed2d4777&sv=1)
For example, the following will nudge the box in the opposite direction:
absolute positioning
“Absolute positioning” is just like relative positioning, but the offset is relative to the entire browser window instead of the original position of the element. Since there’s no longer any relationship with the static flow of the page, consider this the most manual way to lay out an element.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Fcss-absolute-positioning-228ce0.png&width=768&dpr=4&quality=100&sign=d37a8164&sv=1)
Let’s take a look by adding the following rule to our stylesheet:
Our HTML structure is the exact same as the previous example, but this will stick the purple image in the top-left corner of the browser window. You can also try setting a bottom
or right
value to get a clearer idea of what’s going on.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Fabsolute-positioning-screenshot-641ad7.png&width=768&dpr=4&quality=100&sign=f24cc656&sv=1)
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Fabsolute-positioning-flex-start-screenshot-d4b627.png&width=768&dpr=4&quality=100&sign=9f04e447&sv=1)
This behavior isn’t really all that useful most of the time because it would mean everything on your page needs to be absolutely positioned—otherwise we’d get unpredictable overlaps of static elements with absolute elements. So, why does absolute
even exist?
(relatively) absolute positioning
Absolute positioning becomes much more practical when it’s relative to some other element that is in the static flow of the page. Fortunately, there’s a way to change the coordinate system of an absolutely positioned element.
![](https://dotnetweb30-ke.gitbook.io/~gitbook/image?url=https%3A%2F%2Finternetingishard.com%2Fhtml-and-css%2Fadvanced-positioning%2Fcss-relatively-absolute-positioning-1ba963.png&width=768&dpr=4&quality=100&sign=57cb8352&sv=1)
if we change.item-absolute
’s parent element to be relatively positioned, it should appear in the top-left corner of that element instead of the browser window.
The .absolute
div is laid out with the normal flow of the page, and we can manually move around our .item-absolute
wherever we need to. This is great, because if we want to alter the normal flow of the container, say, for a mobile layout, any absolutely positioned elements will automatically move with it.
Notice how we didn’t specify any offset coordinates for .absolute
. We’re using relative positioning for the sole purpose of letting our absolute element hook back into the normal flow of the page. This is how we safely combine absolute positioning with static positioning.
fixed positioning
“Fixed positioning” has a lot in common with absolute positioning: it’s very manual, the element is removed from the normal flow of the page, and the coordinate system is relative to the entire browser window. The key difference is that fixed elements don’t scroll with the rest of the page.
Go ahead and update our third example to use fixed positioning:
This will place the red image in the bottom-right corner of the screen. Try scrolling the page, and you’ll discover that it doesn’t move with the rest of the elements on the page, while the absolutely positioned purple image does.
This lets you create navigation bars that always stay on the screen, as well as those annoying pop-up banners that never go away.
summary
Relative positioning was for tweaking the position of an element without affecting its surrounding boxes. Absolute positioning took elements out of the static flow of the page and placed them relative to the browser window, while relatively absolute positioning allowed us to hook back into the static flow of the page. Finally, fixed positioning let us make elements that didn't scroll with the rest of the page.
Last updated