Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
CSS

CSS: Fix a Sticky Header Jumping When a Scrollbar Appears

- 19.11.26 - ErcanOPAK

🎨 Fix a Sticky Header Jumping When a Scrollbar Appears

`position: sticky` keeps an element pinned relative to its nearest scrolling ancestor, but when page content grows tall enough to introduce a vertical scrollbar for the FIRST time, a browser using a classic non-overlay scrollbar reduces the viewport’s available width by the scrollbar’s own width at that exact moment. A sticky header sized with `width: 100vw` was already laid out against the wider, scrollbar-less viewport, so it visibly jumps the instant the scrollbar appears, because `100vw` always measures the full viewport including the scrollbar gutter, while the rest of the page’s `width: 100%` does not.

🔎 The Problem

.sticky-header {
  position: sticky;
  top: 0;
  width: 100vw; /* includes the scrollbar gutter - jumps when one appears */
}

/* Fix: measure against the containing block, not the raw viewport */
:root {
  scrollbar-gutter: stable; /* reserve the gutter before it's ever needed */
}
.sticky-header {
  position: sticky;
  top: 0;
  width: 100%;
}

✅ Fix: Stop Measuring Against the Raw Viewport

  • Replace any `width: 100vw` on the sticky header (or its full-bleed children) with `width: 100%`, since `100%` resolves against the actual containing block after the scrollbar is already accounted for, while `100vw` always measures the full viewport including the scrollbar gutter.
  • Set `scrollbar-gutter: stable` on the scrolling ancestor so the browser reserves the scrollbar’s width up front, even on pages that don’t yet have enough content to need one — removing the layout shift at its source instead of patching around it.
  • For older browsers without `scrollbar-gutter` support, force a permanent scrollbar with `overflow-y: scroll` instead of the default `auto`, trading a scrollbar that’s always visible for one that never causes a width jump when it appears.

🚨 Why This Is Dangerous

  • A header that jumps the instant a scrollbar appears often does so right as a user is mid-click on a nav link or button in that header, and the sudden shift can cause a mis-click on a different link than the one they aimed for.

A viewport unit and a percentage only agree with each other until a scrollbar shows up to disagree.

— UI Engineer

Related posts:

CSS: Master Grid Layout for Two-Dimensional Layouts

CSS: Master Grid Layout for Two-Dimensional Layouts

Use clamp() for Perfect Responsive Font Sizes

Use clamp() for Perfect Responsive Font Sizes

CSS Animations Drain Battery on Laptops

CSS Animations Drain Battery on Laptops

Views: 5

Post navigation

JavaScript: Fix structuredClone Dropping a Class’s Methods
Photoshop: Fix the Gradient Tool Banding Across a Dark Sky

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com