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 rem. It also assumes you have read the chapters on normal flow, offsets, relative positioning, containing blocks, and absolute positioning. Fixed positioning works like absolute positioning, with a different containing block.
What position: fixed does
An element with position: fixed is taken out of normal flow, like an absolutely positioned element. It leaves no gap behind, and its siblings close up as if it were not there. Its containing block is normally the viewport, so the offsets top, right, bottom, and left measure from the edges of the browser window, not from any ancestor.
The viewport does not move when the page scrolls. A fixed element therefore stays in the same place on screen while the content scrolls underneath it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fixed button</title>
<style>
body { margin: 0; font-family: sans-serif; }
.content { height: 3000px; padding: 1rem; }
.to-top {
position: fixed;
right: 1rem;
bottom: 1rem;
padding: 0.5rem 1rem;
background: #1a5fb4;
color: white;
text-decoration: none;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="content">
<p>Scroll down. The link stays in the bottom-right corner.</p>
</div>
<a class="to-top" href="#">Back to top</a>
</body>
</html>Because the page is 3000px tall, you can scroll it. The link keeps its distance of 1rem from the right and bottom edges of the window the whole time.
How it differs from absolute positioning
Both values remove the element from the flow. The difference is the reference box.
| Feature | `position: absolute` | `position: fixed` |
|---|---|---|
| Removed from normal flow | Yes | Yes |
| Offsets measured from | Nearest positioned ancestor's padding box, or the initial containing block | The viewport (usually) |
| Moves when the page scrolls | Yes, it scrolls with its containing block | No |
| Affected by positioned ancestors | Yes | No (except for the transform case below) |
An absolutely positioned element with no positioned ancestor is placed against the initial containing block, which is the size of the viewport but anchored to the top of the document. It scrolls away with the page. A fixed element does not.
Sizing a fixed element
A fixed element with no width set shrinks to fit its content, the same as an absolutely positioned one. If you give both left and right, and leave width as auto, it stretches between those two edges.
.top-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 3rem;
background: #222;
color: white;
}The inset shorthand sets all four offsets at once. inset: 0 is the same as top: 0; right: 0; bottom: 0; left: 0. On a fixed element it stretches the box over the entire viewport, which is the basis of overlays and backdrops.
Percentages in offsets and sizes refer to the viewport. For example, top: 50% means half the viewport height. Viewport units such as vh, vw, and dvh are often a better fit for the height of a fixed panel.
The space problem
A fixed header is out of flow, so it covers the top of the content that follows it. The page does not make room for it. You have to do that yourself.
.site-header {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 4rem;
background: white;
z-index: 50;
}
body {
padding-top: 4rem;
}The padding on body pushes content down by the header's height. Keep the two values in sync. A custom property avoids drift:
:root {
--header-height: 4rem;
}
.site-header {
position: fixed;
inset: 0 0 auto 0;
height: var(--header-height);
background: white;
z-index: 50;
}
body {
padding-top: var(--header-height);
}Here inset: 0 0 auto 0 sets top, right, bottom, and left in that order, so the bottom offset is auto and the header keeps its own height.
Anchor links have the same problem. When a user follows #section, the browser scrolls that section to the top of the viewport, where the fixed header hides it. The scroll-margin-top property on the target fixes this:
h2[id] {
scroll-margin-top: var(--header-height);
}Centering a modal
The classic recipe moves the top-left corner of the box to the center of the viewport, then pulls the box back by half its own size.
.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;
}top: 50% and left: 50% place the top-left corner at the center. The percentages in translate refer to the modal's own width and height, so -50% shifts it back by half of each. This works without knowing the modal's size.
The backdrop and modal are two separate fixed elements. Giving the modal a higher z-index paints it above the backdrop. Both need to sit above the page content, so their z-index values must be higher than anything else in the same stacking context.
A modern alternative is to make the modal a flex container's child. A fixed backdrop with display: grid; place-items: center centers any child, and the modal then needs no offsets or transform at all:
.backdrop {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: rgba(0, 0, 0, 0.5);
z-index: 100;
}Edge cases and gotchas
An ancestor with a transform captures the fixed element
The specification says that if an ancestor has a transform, perspective, or filter other than none, that ancestor becomes the containing block for fixed descendants. The element is then positioned against the ancestor, not the viewport, and it scrolls with it. The same happens with will-change: transform, contain: paint, and backdrop-filter.
<div class="wrapper">
<div class="pinned">I am not pinned to the viewport.</div>
</div>
<style>
.wrapper {
transform: translateX(0);
height: 2000px;
}
.pinned {
position: fixed;
top: 0;
right: 0;
}
</style>Here .pinned is placed in the top-right corner of .wrapper and scrolls away with it. A common cause is an animated page container or a slide-in menu wrapper. If a fixed element stops being fixed, check every ancestor for these properties. The usual fix is to move the fixed element out of the transformed ancestor in the HTML, for example to be a direct child of body.
Fixed elements do not extend the scrollable area
Because a fixed element is out of flow, it never adds to the page height. Content under it can become unreachable if it is also outside the normal scroll range. A tall fixed panel on a short window cannot be scrolled to see its bottom, because the page scroll moves the page but not the panel. Add overflow-y: auto and a max-height to such panels:
.side-panel {
position: fixed;
top: 0;
right: 0;
bottom: 0;
width: 20rem;
overflow-y: auto;
}With top: 0 and bottom: 0 the panel is exactly as tall as the viewport, and its own content scrolls inside it.
Locking the page behind a modal
A fixed modal does not stop the page behind it from scrolling. A common fix is to hide overflow on the root element while the modal is open:
html.modal-open {
overflow: hidden;
}You add and remove the modal-open class with JavaScript when the modal opens and closes. Hiding the scrollbar also changes the page width on systems with classic scrollbars, so the content can shift sideways. scrollbar-gutter: stable on html reserves the space and avoids the jump.
Mobile browsers and the viewport
On phones, the visible viewport changes as the address bar shows and hides. A fixed element with bottom: 0 generally follows the visible area, but 100vh can be taller than what you can see. Use 100dvh (dynamic viewport height) when you need a panel that matches the visible height. Fixed elements also interact with the on-screen keyboard in browser-specific ways, so test forms inside fixed containers on real devices.
Stacking
A fixed element always creates its own stacking context, even when z-index is auto. That means its children are painted together as a group, and their z-index values only compete with each other. This matters when a fixed element contains something like a dropdown that needs to appear over a sibling of the fixed element: the dropdown cannot escape its fixed parent's group. The next chapters on paint order and stacking contexts cover this in detail.
Fixed versus sticky
Both keep an element visible while scrolling, but they behave differently.
- A fixed element is out of flow. It reserves no space and stays at its offsets for the whole page.
- A sticky element stays in flow and reserves its space. It only sticks while its parent is still in view, and it leaves with the parent.
Use fixed for chrome that belongs to the whole window, such as a top bar, a chat button, a cookie notice, or a modal. Use sticky for items tied to a section of content, such as a table header or a section heading.
Summary of the working model
position: fixedremoves the element from flow.- Offsets are measured from the viewport, unless an ancestor has a transform, filter, or perspective.
- The element does not move on scroll, and it never makes the page taller.
- You must reserve space for it yourself when it overlaps content.
- It forms its own stacking context.
Exercise
You add position: fixed; top: 0; left: 0; right: 0; to a header inside a <div class="app">. The header covers the first paragraph of the page. Later, you add transform: scale(1) to .app for an animation, and the header now scrolls away with the page. Explain both problems and give a fix for each.
Show answer
The first problem is that a fixed element is out of normal flow, so the paragraph moves up underneath it. Fix it by reserving space, for example body { padding-top: <header height>; } (a custom property keeps the two values in sync). The second problem is that a transform on an ancestor makes that ancestor the containing block for fixed descendants. The header is now positioned against .app and scrolls with it. Fix it by removing the transform from .app or by moving the header out of .app, for example making it a direct child of body.
Exercise
Write CSS for a full-screen overlay that dims the page, centers its single child both horizontally and vertically, and sits above content with a z-index of 100. Do not use transform.
Show answer
Use a fixed element with inset: 0 and a grid with place-items: center:
.overlay {
position: fixed;
inset: 0;
display: grid;
place-items: center;
background: rgba(0, 0, 0, 0.5);
z-index: 100;
}
Exercise
A fixed side panel is 20rem wide and runs from top: 0 to bottom: 0. Its content is taller than the window, and the last items cannot be reached. Which property do you add, and why does scrolling the page not help?
Show answer
Add overflow-y: auto to the panel. The panel is fixed to the viewport, so scrolling the page does not move it or reveal more of its content. Only scrolling inside the panel's own box shows the hidden part.