Sticky Position does not Work with Grid/Flex parent

I am a developer from North Carolina. I have always been fascinated by the internet, and scince high school I have been trying to understand the magic behind it all. ECU Computer Science Graduate
Search for a command to run...

I am a developer from North Carolina. I have always been fascinated by the internet, and scince high school I have been trying to understand the magic behind it all. ECU Computer Science Graduate
No comments yet. Be the first to comment.
As a technical person, it’s always been easy for me to focus on hard skills, numbers, and statistics. This worked well through school, personal projects and smaller teams; environments where I could accomplish a ton on my own. But now I’m working in ...

I recently wrote an article detailing how to embed Yotpo’s loyalty widgets on a headless site. As I was writing it, I thought “there is no way that the reviews widget will be this troublesome”. I was wrong, it was just as troublesome. For 2 primary r...

Currently, I am working on turning a Liquid-based Shopify store into a headless Hydrogen-based store. Part of this was transferring Yotpo embedded forms to the new headless site. As for the loyalty and referrals portion of Yotpo, you can embed these ...

Within Shopify, store owners are restricted from having full control of their store’s checkout page. This is a safety feature on Shopify’s end, protectecting store owners and customers from accidentally (or purposely) exposing private information. Th...

HSTS is HTTPS Strict-Transport-Security. It enforces that only HTTPS is used across your web application, preventing unencrypted traffic being monitored and collected by unwanted entities. HSTS in Hydrogen It can be enabled in your Hydrogen app via r...

Frontend developers, do you ever experience strange behavior from sticky elements in your code? I surely did, until I learned that sticky positioning does not work correctly when direct parent elements are flex or grid. To some of you this may already be obvious, somehow I managed to have years of frontend experience before figuring this out.
Imagine a sidebar menu that needs to 'stick' to the left side of the page. So as the user is scrolling through the content of the page, the menu always remains to the side for easy access. This is a common feature for sites with many pages and lots of content (blogs, documentation, ecommerce stores etc.).
Let's use this code as our example:
<div className='grid grid-cols-5'>
<Menu className='sticky top-0 col-span-1'/>
<PageContent className='col-span-4'/>
</div>
Using react and tailwind, but works the same for any js/css config
See we have a sticky element with a direct grid parent. The grid layout will work completely fine, yet the sticky positioning will not!
The simplest way to fix this issue and keep your grid / flex layout, is to wrap the sticky component in an additional div.
<div className='grid grid-cols-5'>
<div className='col-span-1'>
<Menu className='sticky top-0'/>
</div>
<PageContent className='col-span-4'/>
</div>
Super simple! Just make sure you move the flex/grid styles to this new div. Now, you can use both flex/grid with sticky positioning.