Fixed Positioning
By the end of this lesson you will be able to pin a header or a "View cart" bar to the screen so it stays put while the page scrolls, stop it from hiding your content, and fix the strange case where a fixed element suddenly scrolls away with its parent. You will also know what changes on phones.
You should know Absolute Positioning first, because a fixed element behaves like an absolute one with a different containing block. We continue with TiffinGo, our imaginary food-delivery app from Pune. This time we build the screen frame around its menu.
What fixed does
position: fixed places an element against the viewport, the visible area of the browser window. When the page scrolls, the content moves underneath, but the fixed element stays at the same spot on the screen.
Think of the windscreen sticker on a Pune city bus. The road and the shops move past outside, but the sticker stays in the same corner of the glass. The glass is the viewport.
A fixed element shares most rules with absolute elements:
- It is out of the normal flow, so it takes up no space. Content slides underneath it.
- It is placed with
top,right,bottom,leftorinset. - Without a width it shrinks to fit its content. Use
left: 0; right: 0to make a full-width bar. - Setting both
leftandrightstretches it, just like an absolute box.
Two things are different. Its containing block is the viewport, not a positioned ancestor, so position: relative on a parent has no effect on it. And a fixed element always creates a new stacking context, even without a z-index. Z-Index & Stacking Context explains what that means for layering.
A fixed header and a bottom bar
Step 1: pin the bars
The TiffinGo menu needs a header at the top and a "View cart" bar at the bottom, both always visible. Each gets position: fixed, an edge offset, and left: 0; right: 0 so it spans the full width.
Step 2: give the content room
Because fixed bars take no space, the first dish slides under the header and the last dish hides behind the cart bar. The fix is padding on the page that matches the bar heights. Here both steps are together; the fix is marked so you can switch it off.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fixed header and cart bar</title>
<style>
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; background: #fff8f0; }
.top-bar {
position: fixed;
top: 0;
left: 0;
right: 0;
height: 3.5rem;
padding: 0 1rem;
display: flex;
align-items: center;
background: #9d0208;
color: white;
font-weight: 700;
}
.cart-bar {
position: fixed;
bottom: 0;
left: 0;
right: 0;
height: 3.5rem;
padding: 0 1rem;
display: flex;
align-items: center;
justify-content: space-between;
background: #2d6a4f;
color: white;
}
.menu {
padding: 4.5rem 1rem 4.5rem; /* Step 2: change to "padding: 1rem;" to see the problem */
}
.dish { padding: 0.9rem 1rem; margin-bottom: 0.6rem; border-radius: 10px; background: white; border: 1px solid #f0d9c4; }
</style>
</head>
<body>
<header class="top-bar">TiffinGo · Swad Thali House, Kothrud</header>
<main class="menu">
<div class="dish">1. Maharashtrian thali, ₹250</div>
<div class="dish">2. Varan bhaat, ₹120</div>
<div class="dish">3. Bharli vangi, ₹160</div>
<div class="dish">4. Zunka bhakri, ₹140</div>
<div class="dish">5. Kothimbir vadi, ₹90</div>
<div class="dish">6. Alu vadi, ₹90</div>
<div class="dish">7. Masale bhaat, ₹150</div>
<div class="dish">8. Shrikhand puri, ₹130</div>
<div class="dish">9. Solkadhi, ₹60</div>
<div class="dish">10. Modak (2 pieces), ₹80</div>
</main>
<div class="cart-bar"><span>2 items · ₹370</span><strong>View cart</strong></div>
</body>
</html>What you should see: a red header and a green cart bar that stay on screen while you scroll the dishes between them. Dish 1 starts just below the header and dish 10 can scroll fully above the cart bar. Now change the .menu padding to padding: 1rem; and run again. Dish 1 is hidden under the header, and dish 10 can never be read, because the cart bar always covers it.
Covered content is also an accessibility problem. When a keyboard user tabs to a link, the browser scrolls it into view, but it may still sit under a fixed bar. WCAG 2.2 added the success criterion "Focus Not Obscured", which asks that the focused item is not fully hidden by content such as fixed headers. The same padding and scroll-padding fixes help here.
The transform trap
The rule "fixed means the viewport" has an important exception. If any ancestor has one of these properties, that ancestor becomes the containing block for its fixed descendants:
transform(any value exceptnone), including the individualtranslate,rotateandscaleproperties;perspectiveother thannone;filterother thannone;contain: paintorcontain: layout(andstrictorcontent, which include them);will-changenaming one of those properties, such aswill-change: transform.
The fixed element then behaves like an absolute element inside that ancestor. It scrolls away with the ancestor instead of staying on screen. This catches people because the property is often added for a different reason, such as a slide-in animation, a blur effect or a performance hint, and often in a different file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The transform trap</title>
<style>
body { margin: 0; font-family: system-ui, sans-serif; background: #fff8f0; }
.menu-panel {
transform: translateY(0); /* left over from a slide-in animation: delete this line */
margin: 2rem 1rem 60vh;
padding: 1rem 1rem 4rem;
border-radius: 12px;
background: white;
border: 1px solid #f0d9c4;
}
.dish { padding: 0.7rem 0; border-bottom: 1px solid #f3e3d3; }
.cart-bar {
position: fixed;
left: 0;
right: 0;
bottom: 0;
padding: 0.9rem 1rem;
background: #2d6a4f;
color: white;
font-weight: 600;
}
</style>
</head>
<body>
<div class="menu-panel">
<h2>Desserts</h2>
<div class="dish">Ukadiche modak, ₹80</div>
<div class="dish">Basundi, ₹90</div>
<div class="dish">Amrakhand, ₹110</div>
<div class="dish">Puran poli, ₹90</div>
<div class="dish">Gulab jamun, ₹70</div>
<div class="cart-bar">1 item · ₹80 · View cart</div>
</div>
</body>
</html>What you should see: the green bar sits along the bottom of the white panel, not the bottom of the screen, and it scrolls up with the panel. Delete the transform line and run again: the bar now spans the whole window and stays at the bottom while you scroll.
How do you fix this in a real project? You have three choices:
- Move the fixed element out of the transformed ancestor, for example make it a direct child of
<body>. This is the most reliable fix. - Remove the transform once it is no longer needed, for example after a slide-in animation finishes.
- If the property is a
will-changehint, delete it. It is rarely needed.
When a fixed element misbehaves, open DevTools, select it, and walk up its ancestors in the Computed tab looking for transform, filter, perspective, contain or will-change.
Fixed or sticky?
A header can be fixed or sticky. A sticky header stays in the flow, so it keeps its space and you do not need the padding fix. It only sticks while its parent is on screen, which suits headers inside the page or inside a section. A fixed header stays put no matter what, which suits app-like screens with a bar at the top and bottom. The next lesson, Sticky Positioning, covers sticky in depth.
Fixed elements on phones
Fixed bars cost screen space, and phones have the least of it. Keep these points in mind:
- Keep bars short. Two 56px bars on a phone held sideways can cover a third of the screen. A media query can make the header static when the screen height is small, for example
@media (max-height: 30rem). - Full-screen layers. For a fixed overlay,
inset: 0makes it fit the viewport. Avoidheight: 100vhthere, because on many phones100vhis taller than the visible area when the address bar is showing. Viewport Units: vw, vh, vmin & vmax explainsdvhand friends. - Notches and home bars. On phones with a notch or a gesture bar, a bottom bar can sit under the home indicator. With
viewport-fit=coverin the viewport meta tag,padding-bottom: env(safe-area-inset-bottom)adds the right amount of space. - The on-screen keyboard. When someone types in a search box, the keyboard can cover a bottom bar or push it up awkwardly, and browsers differ here. Test forms with fixed bars on a real phone.
- Zoom. When a user zooms the page to 200%, fixed bars grow too and take an even bigger share of the screen. Short bars help here too.
Common mistakes
- No room for the content. The first or last items hide behind the bars. Add matching padding, or use a sticky header instead.
- Forgetting the width. A fixed bar with only
top: 0shrinks to its text. Addleft: 0; right: 0. - Adding
position: relativeto the parent to "contain" it. This has no effect on fixed elements. Use absolute if you want it inside the parent. - A transform, filter or
will-changeon an ancestor. The fixed element scrolls away with that ancestor. - Too many fixed things. A header, a cart bar, a chat button and a cookie banner together leave no room to read on a phone.
Interview questions
1. What is the containing block of a fixed element?
Normally the viewport, so the element stays in the same place on screen while the page scrolls. If an ancestor has a transform, perspective, filter, contain: paint or contain: layout, or a matching will-change, that ancestor becomes the containing block instead.
2. Why does a fixed header hide the start of the page, and how do you fix it?
A fixed element is removed from the normal flow, so it takes no space and content starts underneath it. Add top padding (or margin) to the page equal to the header height, and scroll-padding-top so in-page links do not land under it. A sticky header avoids the problem because it keeps its space.
3. A modal's fixed close button scrolls away with the modal. What would you check?
I would look for a transformed or filtered ancestor, often from an opening animation, because it turns that ancestor into the containing block. Moving the button out of that ancestor or removing the transform after the animation fixes it.
4. Does a fixed element create a stacking context?
Yes, always, even with z-index: auto. Its children are layered inside it, and the fixed element is ordered as one unit against other elements in its parent stacking context.
5. What should you watch for with fixed elements on mobile?
Screen space, because bars cover more of a small screen, especially sideways or zoomed in. Avoid 100vh for full-screen layers, add safe-area padding with env(safe-area-inset-bottom), and test how the on-screen keyboard interacts with bottom bars.
Key points
position: fixedplaces an element against the viewport, so it does not move when the page scrolls.- Like absolute, it leaves the flow, shrinks to fit, and stretches with
leftandright. - Give content room with padding, and use
scroll-padding-topfor in-page links. transform,perspective,filter,containand matchingwill-changeon an ancestor trap fixed elements inside it.- A fixed element always creates a stacking context.
- On phones, keep bars short, use
inset: 0for overlays, and respect safe areas.
Exercise
Build a TiffinGo order-tracking screen. Add a fixed top bar with the order number, a fixed round "Call rider" button in the bottom-right corner, and at least twelve status updates so the page scrolls. Make sure the first update is not hidden under the top bar. Then wrap the updates in a <div> with filter: blur(0) and put the button inside it. Describe what changes, and fix it without removing the filter.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Order tracking</title>
<style>
body { margin: 0; font-family: system-ui, sans-serif; background: #fff8f0; }
.order-bar { position: fixed; top: 0; left: 0; right: 0; padding: 1rem; background: #9d0208; color: white; }
.updates { filter: blur(0); padding: 4rem 1rem 5rem; }
.update { background: white; border: 1px solid #f0d9c4; border-radius: 8px; padding: 0.8rem; margin-bottom: 0.5rem; }
.call { position: fixed; right: 1rem; bottom: 1rem; padding: 0.9rem 1.2rem; border: none; border-radius: 999px; background: #1d4ed8; color: white; }
</style>
</head>
<body>
<header class="order-bar">Order #TG-4821 · Arriving in 18 min</header>
<div class="updates">
<div class="update">12:01 Order placed at Swad Thali House</div>
<div class="update">12:03 Restaurant accepted your order</div>
<div class="update">12:10 Food is being prepared</div>
<div class="update">12:22 Rider Farhan reached the restaurant</div>
<div class="update">12:25 Order picked up</div>
<div class="update">12:27 Rider crossed Karve Road</div>
<div class="update">12:30 Rider near Nal Stop</div>
<div class="update">12:32 Rider at Paud Phata signal</div>
<div class="update">12:35 Rider entered Kothrud</div>
<div class="update">12:38 Rider is 1 km away</div>
<div class="update">12:41 Rider is 300 m away</div>
<div class="update">12:43 Rider has arrived</div>
</div>
<button class="call">Call rider</button>
</body>
</html>With the button inside .updates, the filter made that <div> its containing block, so the button sat at the bottom of the list and scrolled away. Moving the button out of the filtered <div>, as above, places it against the viewport again while the filter stays. The 4rem top padding keeps the first update clear of the bar.
Quick Quiz
1. A fixed element has top: 0 and no width. What does it look like?
Like absolute boxes, fixed boxes shrink to fit unless you set a width or both left and right.
2. Which ancestor property stops a fixed element from staying on screen?
A transform on an ancestor makes it the containing block for fixed descendants; opacity and relative do not.
3. Why does a fixed header need padding on the content below it?
The header is out of the flow, so the page must reserve the space itself.
4. Which is the safest choice for a full-screen fixed overlay on phones?
inset: 0 fits the viewport, while 100vh can be taller than the visible area on mobile.
Learn CSS with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
