Sitemap

Member-only story

I Stopped Using JavaScript for Menus

HTML and CSS now handles what JS used to

For years, I wrote the same 100 lines of JavaScript every time I needed a dropdown.

Read Free for non-members.

Calculate position. Track scroll. Handle resize. Fix mobile bugs. Add escape key logic. Patch the weird stuff that only breaks in production.

Not because I liked it.

Because I thought I had no choice.

Then one day, while playing with modern CSS, I realized something uncomfortable:

The browser was already doing the hard part.

Press enter or click to view image in full size
I Stopped Using JavaScript for Menus | Tushar Kanjariya
I Stopped Using JavaScript for Menus

The real problem with menus

Menus are simple UI elements.

They just need to:

  • Open near the trigger
  • Stay attached while scrolling
  • Never overflow the screen
  • Close correctly

Yet we’ve been solving this with JavaScript math for years.

Mostly because CSS couldn’t express intent.

Now it can.

Native popover changed everything

Written by Tushar Kanjariya

Full-Stack Developer sharing insights on code, design, and AI. 6+ Years of Experience in Tech.