relative
elements are positions with reference to their original position in normal flow, without affecting other elements. The offset is from it’s normal position in the documentabsolute
elements are positioned with reference to their containing elements (or the closest positioned i.e. relative/absolute/fixed parent). The offset is from parent element.fixed
elements are positioned with reference to the viewport (it’ll stay in place in the viewport when you scroll). The offset is from the viewport windowValue | Description |
---|---|
static | A normal box laid out according to normal flow (because normal flow is the default positioning scheme, you will rarely have to specify this value). |
relative
, absolute
, fixed
) allows you the use of offset properties (top
, bottom
, left
, right
). In other words, you need to set a position (other than default static) first in order to adjust it’s position in the viewport using offset property values.left
, right
, bottom
and top
. They let you move the element away from it’s normal position. Offset properties only work on positioned elements (non-static)left: 10px;
, it’ll essentially move the item 10px to the right. Similarly bottom: 25px;
will move the item 25px up.When an element is referred to as a positioned element, it means that its position has been changed from being static, to one of four available values: relative, absolute, fixed, sticky.
Once an element has been positioned (given a non-static value), its position on the page is determined using the offset properties: top, right, bottom, and left.
value | positioning | stays in normal flow | allows offset properties |
---|---|---|---|
static | default | yes | no |
relative | relative to its current position, acts as an anchor point for absolutely positioned elements | yes | yes |
absolute | locks the element in place relative to it’s positioned parent | no | yes |
fixed | locks the element to the browser window (viewport) | no | yes |
relative
, it allows you to specify how CSS should move it relative to its current position in the normal flow of the pageleft
or right
, and top
or bottom
1p {
2 position: relative;
3 bottom: 10px;
4}
position: relative;
to it’s intended parent, the browser will keep looking up the chain and ultimately default to the body tag1<div style="position: relative; padding: 1em; background: plum">
2 RELATIVE
3</div>
4
5<div style="position: absolute; padding: 1em; background: salmon">
6 ABSOLUTE
7</div>
absolute
and fixed
) elements can not be contained. Since absolute elements move out of the normal flow, it’ll look like the relative parent disowned the absolute child (For example, the padding for a parent relative
element will not wrap around a child absolute
element, the absolute element will not be contained inside the relative) TIP: You can add height/width to the parent element to make it look like it is containing the child. 1<div style="position: relative; padding: 1em; background: rgba(0,255,255, .5);">
2 <div style="position: absolute; padding: 1em; background: rgba(221,160,221, .5);">
3 ABSOLUTE inside a relative parent element
4 </div>
5</div>
6
7<div style="position: relative; padding: 1em; background: rgba(70, 130, 180, .5);">
8 <div style="position: fixed; padding: 1em; background: rgba(102, 51, 153, .5);">
9 FIXED inside a relative parent element
10 </div>
11</div>
relative | absolute | fixed |
---|---|---|
acts as anchor point for absolutely positioned (both absolute and fixed) elements | positioned relative to the closest relative positioned parent element | positioned relative to the closest relative or absolute positioned parent element |
sticks to the document | sticks to the viewport | |
moves with the document when you scroll | stays in place when you scroll | |
stays in the normal flow | breaks normal flow | breaks normal flow |
Block elements, take up full width of the containing element | size determined by content, unless explicitly defined | size determined by content, unless explicitly defined |
absolute
and fixed
are both absolutes. (fixed is a type of absolutely positioned elements). Absolutes (both absolute and fixed) are positioned relative to the closest parent relative
element, while fixed
are positioned relative to the closest positioned (absolute or relative) parent elementWhen using offsets to move elements, it only moves the element you targeted, the rest stay in place. So even though elements are positioned relatively, they don’t move relatively, they stay in their places in the document where they occurred.