Fix Any CSS Z-index Issue With This One Trick

(apologies in advance for the title… 😉)

Z-index controls the stacking order of elements, to determine which elements appear on top of others in the visual layout. It allows you to create interesting UI’s and visual designs by creating depth (literally).

But create web-apps or sites for long enough and soon you come across a z-index issue. Over the years I’ve explored and used many techniques to battle them. From z-index maps (back in the SCSS days) to throwing in the towel and just adding z-index: 99999. (╯°□°)╯︵ ┻━┻

Let’s look at a common example.

<nav class="sticky top-0 z-10">
  Sticky Nav (z-index: 10)
</nav>

<div class="">
  <div class="relative z-20">
    Z-index content (z-index: 20)
  </div>
</div>

The Z-index content will scroll “on top” of the nav element. See this preview:

Z-index content (z-index: 20)

You might think to just change the nav’s z-index to 30 and call it a day. But we both know it’s not going to end well. 😉

The modern (and way more sane) way is to use the isolation property.

<nav class="sticky top-0 z-10">
  Sticky Nav (z-index: 10)
</nav>

<div class="isolate">
  <div class="relative z-20">
    Z-index content (z-index: 20)
  </div>
</div>

This is using Tailwind CSS, but all you need to know is that the isolate class is the same as isolation: isolate;.

Z-index content (z-index: 20)

Once you learn about isolation: isolate; you see many other use cases for it in your apps.

Explore this article for more design tips for developers.

Get UI & Product Engineering Insights for Rails Apps (and product updates!)

Published at . Last updated at . Have suggestions or improvements on this content? Do reach out. Interested in sharing Rails Designer with the Ruby on Rails community? Become an affiliate.

UI components for Ruby on Rails apps

$ 99 one-time
payment

Get Access
  • One-time Payment

  • Access to the Entire Library

  • Built for Ruby on Rails

  • Designed with Tailwind CSS and Enhanced with Hotwire

  • Updates for 12 months