惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
Apple Machine Learning Research
Apple Machine Learning Research
H
Help Net Security
雷峰网
雷峰网
V
Visual Studio Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
IT之家
IT之家
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
月光博客
月光博客
A
About on SuperTechFans
B
Blog RSS Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale

Ben Frain

Well done AI, that’s the end of the honest boxes The Beginnings, by Rudyard Kipling So, you want a React modal that uses the <dialog> element and transitions in AND out? Scroll indicators on tables with background colours using animation-timeline Review: SoundPEATS Clip1 Open ear clip-on headphones VS Code – highlight just the active indent guide Review: MoErgo Go60, a split ergonomic and fully programmable keyboard Review: Kinesis mWave mechanical ergonomic and programmable keyboard iOS26 Safari theme-color/tab-tinting with fixed position elements is a mess New Book: Responsive Web Design with HTML5 and CSS, 5th Edition Use @supports with a proxy feature/value for features you can’t test for (@starting-style) First adventures in View Transitions Review: Benq Screenbar Pro and Halo lightbars. The kit you never knew you needed! Center items in a container, and make then left aligned when they overflow A single element CSS donut timer/countdown timer, that can sit on any background Review: Open Ear Headphones – Bose Open Ultra v Huawei FreeClip In search of the perfect autocomplete for CSS Managing multiple versions of node, without NVM or additional tools Review: Keychron Q14 Max Alice 96 Key mechanical keyboard NEW VIDEO COURSE: Responsive Web Design with HTML5 and CSS Is CSS Grid really slower than Flexbox? Review: Advantage360 Pro Signature Edition 2024 mechanical ergonomic keyboard More Keys or Fewer Keys for mechanical keyboards Neovim – how to do project-wide find and replace? Review: Keyboardio Model 100, split, wooden, mechanical keyboard Struggling to learn SwiftUI How to create rounded gradient borders with any background in CSS How to get equal size icons in the cmp completion menu of Neovim with Kitty terminal Review: Dygma Defy, split, mechanical, programmable ergonomic keyboard What’s the best way to reset WAAPI chained animations?
Yes! You can use position: sticky and overflow together
Ben Frain · 2024-06-20 · via Ben Frain

I have a fix for the perennial problem with position: sticky and overflow.

Well, my perennial problem anyway.

What is the problem, Ben?

Position sticky is defined like this at MDN:

is positioned according to the normal flow of the document, and then offset relative to its nearest scrolling ancestor and containing block (nearest block-level ancestor)

Crucial there is “nearest scrolling ancestor”.

So all works well with position: sticky until you add overflow-x: hidden to an ancestor of the sticky element because a sibling element of the sticky element needs its overflow to be hidden/scrollable (a sideways scroller for example). When you do that, the sticky element does not stick! That’s because using overflow creates a new formatting context which means that our sticky element looks to the item with overflow-x: hidden on as its reference as to whether to stick or not, and now it never will because it will never reach the top of it!

In addition, when you have no overflow-x: hidden on the body, as I often do to prevent side to side ‘rubber band’ scrolling in iOS, that can also kill your sticky element.

overflow: clip to the rescue

However, in both these situations, you can use overflow-x: clip instead and it clips as you would expect allowing the same kind of behaviours, but crucially does NOT stop sticky elements sticking because it does NOT create a formatting context. Again, from MDN:

The difference between clip and hidden is that the clip keyword also forbids all scrolling, including programmatic scrolling. No new formatting context is created.

Amazing, so happy this little piece of differentiation exists and I don’t know why it escaped me until now. I only discovered it when toggling through the overflow options in the dev tools!

Codepen Demo

Not convinced? Open this Codepen and see for yourself, the little button top left is just toggling from hidden to auto on the overflow property. Notice how the black bar sticks and unsticks?

See the Pen Position sticky AND overflow to prevent horizontal rubber-banding by Ben Frain (@benfrain) on CodePen.