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 also assumes you have read the previous chapter, which introduced the position property and its five values. Here you learn the properties that say where a positioned element goes: top, right, bottom, left, and the inset shorthand.
Offsets only work on positioned elements
The four offset properties are top, right, bottom, and left. They are often called offsets. They apply only when position is something other than static. On a static element, the browser ignores them without any warning.
.static-box {
position: static; /* the default */
top: 50px; /* has no effect */
left: 50px; /* has no effect */
}If an offset seems to do nothing, check position first. This is the most common offset mistake.
What an offset measures
Each offset is a distance, but the starting point depends on the position value. The same top: 10px means different things in different cases.
| `position` value | Offsets are measured from |
|---|---|
| `relative` | The element's own normal-flow position |
| `absolute` | The padding edge of the containing block |
| `fixed` | The edges of the viewport |
| `sticky` | The edges of the nearest scrolling container, used as thresholds |
Later chapters cover each case in detail. For now, remember the edge names. top: 10px means "put my top edge 10px from the reference edge". It does not mean "move me 10px up". Because the offset is a distance from an edge, left: 20px pushes a relatively positioned box to the right. The box's left edge sits 20px to the right of where it would normally be.
Allowed values
An offset accepts these values:
- A length such as
10px,2rem, or5vh. - A percentage. For
leftandright, the percentage is of the containing block's width. Fortopandbottom, it is of the containing block's height. auto, the initial value. It means "no offset was requested", and the browser works out the position from other properties.- A negative length or percentage, such as
top: -8px. This moves the edge outside the reference edge. - A
calc()expression, such asleft: calc(50% - 100px).
Percentages have one trap. If the containing block's height is auto, meaning it depends on its content, a percentage top or bottom on a relatively positioned element computes to auto. It then does nothing. Give the container an explicit height when you need percentage vertical offsets.
Offsets measure from the padding edge
For absolutely positioned elements, the reference is the padding edge of the containing block. The padding edge is the outer edge of the padding area, just inside the border. So top: 0; left: 0 places the element against the inside of the border, not against the border's outer edge. It also sits outside any padding, so padding does not push it inward.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Padding edge demo</title>
<style>
.panel {
position: relative;
height: 160px;
padding: 24px;
border: 6px solid #333;
}
.corner {
position: absolute;
top: 0;
left: 0;
width: 40px;
height: 40px;
background: tomato;
}
</style>
</head>
<body>
<div class="panel">
<div class="corner"></div>
Panel text
</div>
</body>
</html>The red square touches the inner side of the border. It ignores the 24px of padding. Run this and compare it with the same square placed with margin instead.
Setting opposite offsets together
You can set top and bottom together, or left and right together. What happens depends on position and on whether the size is fixed.
For absolute and fixed elements, setting both opposite offsets with an auto size stretches the element between the two edges. The browser computes the size from the offsets:
.fill {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
/* width and height are auto, so the box fills the containing block's padding box */
}If you also set an explicit height, the constraints disagree (top, height, and bottom all have values). The CSS rules resolve this by ignoring bottom. For horizontal offsets in a left-to-right language, right is ignored. In a right-to-left language, left is ignored.
.over-constrained {
position: absolute;
top: 10px;
bottom: 10px; /* ignored */
height: 100px; /* wins: the box is 100px tall */
}For relatively positioned elements, the element is never stretched. If both top and bottom are set, top wins. If both left and right are set, left wins in left-to-right text and right wins in right-to-left text.
The inset shorthand
inset sets all four offsets in one declaration. It follows the same value order as margin and padding: top, right, bottom, left, which runs clockwise.
| Declaration | Equivalent longhands |
|---|---|
| `inset: 0` | `top`, `right`, `bottom`, `left` all `0` |
| `inset: 10px 20px` | `top` and `bottom` `10px`; `left` and `right` `20px` |
| `inset: 10px 20px 30px` | `top` `10px`; `left` and `right` `20px`; `bottom` `30px` |
| `inset: 0 auto auto 0` | `top` `0`, `right` `auto`, `bottom` `auto`, `left` `0` |
The last row is the useful one for pinning a corner while leaving the opposite sides unconstrained. Use auto for any side you do not want to set. Writing inset: 0 0 auto auto pins the top-right corner:
.badge {
position: absolute;
inset: 0 0 auto auto; /* top: 0; right: 0; bottom: auto; left: auto */
}inset is supported in all current major browsers. Very old browsers, such as Safari before 14.1, do not support it. If you must support them, write the four longhands.
Centering with inset and auto margins
When an absolutely positioned element has a fixed size and all four offsets set, the spare space is split by margin: auto. This centers the element in its containing block without transform:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Centering with inset</title>
<style>
.stage {
position: relative;
height: 300px;
background: #eee;
}
.centered {
position: absolute;
inset: 0;
width: 200px;
height: 100px;
margin: auto;
background: steelblue;
}
</style>
</head>
<body>
<div class="stage">
<div class="centered"></div>
</div>
</body>
</html>The box is centered both ways because the browser divides the leftover width and height equally between the auto margins. This only works with an explicit width and height. If either is auto, the element stretches instead.
Logical offset properties
The physical names top, right, bottom, and left assume a fixed writing direction. CSS also has logical equivalents that follow the writing mode:
inset-block-startandinset-block-endrun along the block axis, which is vertical in English.inset-inline-startandinset-inline-endrun along the inline axis, which is horizontal in English.inset-blockandinset-inlineare shorthands for each pair.
In English, inset-inline-start: 0 behaves like left: 0. In Arabic or Hebrew, which are written right to left, it behaves like right: 0. Use logical properties when your interface must flip for different languages. For a layout that never changes direction, the physical names are fine.
Common mistakes
- Setting an offset without setting
position. The offset is ignored. - Expecting
bottom: 10pxto move an element down. It places the bottom edge 10px above the reference edge. - Using a percentage
topinside a container withheight: auto. - Setting
top,bottom, andheighttogether and wondering whybottomis ignored. - Forgetting that absolute offsets start at the padding edge, so container padding does not push the element inward.
Exercise
A .label element has position: absolute; top: 0; bottom: 0; height: 80px; inside a 300px-tall containing block. How tall is the label, and which declaration is ignored?
Show answer
The label is 80px tall. Because top, height, and bottom all have values, the constraints are over-constrained, and the CSS rules ignore bottom. The label's top edge sits at the top of the containing block's padding box.
Exercise
Write a single inset declaration that pins an absolutely positioned element to the bottom-left corner of its containing block, with top and right left unconstrained.
Show answer
inset: auto auto 0 0;. The values run top, right, bottom, left, so top is auto, right is auto, bottom is 0, and left is 0.
Exercise
You write .tag { top: 12px; left: 12px; } and the tag does not move. What is the most likely cause, and what is the fix?
Show answer
The element has position: static, the default, so the browser ignores offsets. Add a non-static value such as position: relative or position: absolute to .tag.