These four offset properties only have any effect on an element whose position is something other than static, the default, since static elements deliberately ignore them entirely. On a relatively positioned element, they shift it visually from where it would otherwise sit in normal flow, without affecting the space it originally reserved there; on an absolutely, fixed, or sticky positioned element, they instead set the actual distance from the corresponding edge of that element's positioning context, like its nearest positioned ancestor for absolute, or the viewport for fixed.
1Understanding Top, Right, Bottom, Left
These four offset properties only have any effect on an element whose position is something other than static, the default, since static elements deliberately ignore them entirely. On a relatively positioned element, they shift it visually from where it would otherwise sit in normal flow, without affecting the space it originally reserved there; on an absolutely, fixed, or sticky positioned element, they instead set the actual distance from the corresponding edge of that element's positioning context, like its nearest positioned ancestor for absolute, or the viewport for fixed.
top, right, bottom, and left have no effect at all on a statically positioned element, position's default value — if an offset value doesn't seem to be doing anything, double-check that the element actually has an explicit, non-static position value set.
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}2Practical Example
Here is a real-world application of Top, Right, Bottom, Left showing how it is used in production CSS code.
.static-box {
top: 20px;
left: 20px;
}3Best Practices
Follow these guidelines when working with Top, Right, Bottom, Left:
1. Verify an element has an explicit, non-static position value before troubleshooting why top/right/bottom/left don't appear to be having any effect
2. Use top/left, or an equivalent pair, rather than all four properties at once, unless you specifically need to also stretch the element by setting opposing sides simultaneously
3. Combine opposing offsets, like both left: 0 and right: 0 on an absolutely positioned element with no explicit width, to stretch it to fill its positioning context's full width
Tip: top, right, bottom, and left have no effect at all on a statically positioned element, position's default value — if an offset value doesn't seem to be doing anything, double-check that the element actually has an explicit, non-static position value set.
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}