CodexBloom - Programming Q&A Platform

CSS Sticky Position optimization guide in Safari with Scrollable Flex Container

๐Ÿ‘€ Views: 0 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-07-27
css flexbox safari CSS

I keep running into I'm working on a personal project and Quick question that's been bugging me - Hey everyone, I'm running into an issue that's driving me crazy. I'm trying to create a sticky header that stays at the top of a flex container when scrolling, but it's not working in Safari. My layout uses a flexbox to arrange some cards and I've set up the header with `position: sticky; top: 0;` but it's not behaving as expected. In Chrome and Firefox, it works perfectly, but in Safari, the header scrolls away with the content instead of sticking at the top. Hereโ€™s the relevant CSS: ```css .container { display: flex; flex-direction: column; height: 300px; overflow-y: auto; } .header { position: sticky; top: 0; background: white; z-index: 10; } .card { height: 100px; border: 1px solid #ccc; margin: 5px; } ``` And hereโ€™s the HTML structure: ```html <div class="container"> <div class="header">Sticky Header</div> <div class="card">Card 1</div> <div class="card">Card 2</div> <div class="card">Card 3</div> <div class="card">Card 4</div> <div class="card">Card 5</div> </div> ``` I've tried adjusting the `z-index` and ensuring the parent container has a defined height, but nothing seems to work in Safari. I also checked for any browser-specific prefixes, but that didnโ€™t help either. Is there a known scenario or workaround for getting sticky positioning to work in Safari under these circumstances? Any insights would be appreciated! What's the best practice here? This is part of a larger service I'm building. I'm using Css 3.11 in this project. How would you solve this? I'm on macOS using the latest version of Css. Is this even possible?