Sub topic 7
Fixed Positioning: Pinning to the Viewport
This chapter assumes you know basic HTML and CSS selectors, the box model, and length units such as px and %. It also assumes you have read the earlier chapters on offsets, containing blocks, and absolute positioning.
What position: fixed does
An element with position: fixed is removed from normal flow, like an absolutely positioned element. Siblings close up the gap as if it were not there. The difference is the containing block. An absolute element is placed against its nearest positioned ancestor. A fixed element is placed against the viewport, which is the visible area of the browser window.
Because of this, a fixed element does not move when the page scrolls. It stays at the same spot on the screen while the content moves underneath it.
Placing a fixed element
You place a fixed element with top, right, bottom, left, or the inset shorthand. The offsets are measured from the edges of the viewport.
.help-button {
position: fixed;
bottom: 1rem;
right: 1rem;
}This pins a help button 1rem from the bottom-right corner of the window. It stays there however far the user scrolls.
If you set no offsets, the element keeps the position it would have had in normal flow, but it is still removed from the flow and does not scroll. Set offsets explicitly so the result is predictable.
Covering the screen and centering
Two common uses are a full-screen backdrop and a centered modal. The inset: 0 shorthand sets all four offsets to 0, so the element stretches across the whole viewport.
.backdrop {
position: fixed;
inset: 0;
background: rgba(0, 0, 0, 0.5);
z-index: 100;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 101;
}Setting top: 50% and left: 50% moves the modal's top-left corner to the center of the viewport. The modal's own center is not there yet. translate(-50%, -50%) shifts the box back by half its own width and height, which centers it. The higher z-index paints the modal above the backdrop.
A fixed element needs space reserved for it
Since a fixed element leaves the flow, content behind it is not pushed down. A fixed header will cover the top of the page content unless you make room.
.top-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 3rem;
background: white;
}
body {
padding-top: 3rem;
}The padding-top on body matches the bar's height, so the first line of content starts below the bar instead of underneath it.
When fixed stops being fixed
The viewport rule has an exception. If an ancestor has a transform, perspective, or filter value other than none, that ancestor becomes the containing block for fixed descendants. The element then scrolls with that ancestor instead of staying on screen.
.page-wrapper {
transform: scale(1);
}A fixed child inside .page-wrapper is now positioned against the wrapper, not the viewport. If a fixed element behaves like an absolute one, check its ancestors for these properties.
Quick reference
| Behavior | Absolute | Fixed |
|---|---|---|
| Removed from normal flow | Yes | Yes |
| Offsets measured from | Nearest positioned ancestor | Viewport |
| Moves when the page scrolls | Yes, with its ancestor | No |
| Affected by ancestor `transform` | Only as a positioned ancestor | Yes, the ancestor becomes the containing block |
Exercise
A fixed footer bar with bottom: 0 scrolls away with the page instead of staying on screen. The CSS for the bar itself is correct. What is the most likely cause, and how do you check it?
Show answer
An ancestor of the bar probably has a transform, perspective, or filter value other than none. That makes the ancestor the containing block for the fixed element. Inspect the ancestors in the browser's developer tools, look for these properties, and remove them or move the bar outside that ancestor.
Exercise
A fixed header with top: 0 and a height of 4rem hides the first heading of the page. How do you fix it?
Show answer
A fixed element is out of normal flow, so the content starts at the top of the page under the header. Add padding-top: 4rem (or a matching margin-top on the first element) to the body or main content so it starts below the header.