Sub topic 3
Placing elements with top, right, bottom, left, and inset
This chapter assumes you know basic HTML and CSS selectors, the box model (content, padding, border, margin), and length units such as px, %, and rem. It covers the offset properties that tell a positioned element where to go.
What offsets do
The offset properties are top, right, bottom, and left. They have no effect on an element whose position is static, which is the default. Once you set position to relative, absolute, fixed, or sticky, the offsets become active.
What an offset measures depends on the position value:
- For
relative, it is the distance to move the box away from its normal spot. - For
absolute, it is the distance from an edge of the containing block. - For
fixed, it is the distance from an edge of the viewport. - For
sticky, it is the threshold at which the element stops scrolling and sticks.
Later chapters cover each value in detail. Here the focus is on how the offsets themselves behave.
Direction of movement
Each offset names the edge it is measured from, not the direction the box moves. This surprises many learners.
top: 10pxpushes the box 10px down from the top edge.left: 20pxpushes the box 20px right from the left edge.bottom: 10pxpushes the box 10px up from the bottom edge.right: 20pxpushes the box 20px left from the right edge.
Negative values reverse the direction. top: -10px moves the box up.
.box {
position: relative;
top: 10px;
left: 20px;
}This box is drawn 10px lower and 20px further right than its normal position. The space it occupies in the flow stays where it was, so neighbouring elements do not move.
Opposite offsets on the same element
You can set two opposite offsets together. What happens depends on the element.
- On a
relativeelement, if bothtopandbottomare set,topwins. If bothleftandrightare set,leftwins in a left-to-right language. - On an
absoluteorfixedelement withheight: auto, setting bothtopandbottomstretches the box between those two edges. The same applies toleft,right, andwidth: auto.
The stretching behaviour is useful because it removes the need to calculate sizes.
The inset shorthand
inset sets all four offsets at once. It follows the same value order as margin: top, right, bottom, left.
| Declaration | Equivalent |
|---|---|
| `inset: 0` | `top: 0; right: 0; bottom: 0; left: 0` |
| `inset: 10px 20px` | top and bottom 10px, right and left 20px |
| `inset: 1px 2px 3px` | top 1px, left and right 2px, bottom 3px |
| `inset: 1px 2px 3px 4px` | top 1px, right 2px, bottom 3px, left 4px |
A common use is covering a whole area:
.backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
}Because inset: 0 sets all four edges, the backdrop stretches to fill the viewport with no width or height needed.
Percentages and `auto`
Offsets accept lengths, percentages, and auto.
- A percentage for
toporbottomrefers to the height of the reference box. A percentage forleftorrightrefers to its width. autois the initial value. The browser works out the position itself, or keeps the element at its normal place.
Setting top: 50%; left: 50% moves the box's top-left corner to the centre of the reference box, not the box's own centre. A later chapter shows how transform corrects that.
Common errors
- Offsets seem to do nothing. Check that
positionis notstatic. - The element moved the opposite way from what you expected. Remember that
right: 20pxmoves the box left, because it measures from the right edge. - The element is placed relative to the wrong thing. For
absolute, the reference is the nearest positioned ancestor. That is the subject of the containing block chapter.
Exercise
A box has position: relative; bottom: 15px; right: 5px;. In which direction does it move from its normal position, and by how much?
Show answer
It moves 15px up and 5px to the left. bottom: 15px measures from the bottom edge, which pushes the box upward, and right: 5px measures from the right edge, which pushes the box leftward.
Exercise
Write one declaration that sets top and bottom offsets to 8px and left and right offsets to 16px.
Show answer
inset: 8px 16px;