<img height="1" width="1" style="display:none;" alt="" src="https://dc.ads.linkedin.com/collect/?pid=12616&amp;fmt=gif">

Latest Posts

Solving Z-Index With Styled-Components

May 15, 2018

CSS is ok-ish now

CSS is known for some quirks that have been driving people mad since they started creating web "apps" (as opposed to just documents with hyperlinks). For example there's z-index, which usually gets a random value that conflicts with another one in your app (you just haven't noticed it yet 😉). There's vertical-align that rarely works as you'd want it to (fortunately we have Flexbox). There's also... wait, is that all?

Lately it seems I don't have too much beef with CSS. Most of the basic layouting problems can be solved with Flexbox. A lot of problems around scoping and modules can be solved by one of the many CSS-in-JS solutions. Speaking of which, as a React user, I had to give them a try.

The first one was styled-jsx, which is quite handy because it retains the CSS syntax while adding scoping and a few extra things on top. Then came styled-components, which had a useful feature styled-jsx only gained in v2 - the ability to use props in styles. I also like how it composes seamlessly with every other component in the app - you only need to pass className prop and it just works!

Anyway, it seems to me that these days the only problems I encounter are the previously mentioned vertical-align and some browser quirks here and there. Even z-index is not scary anymore! In this post I want to show you how I tamed that property thanks to the power of components.

Why z-index is far from perfect

The first problem with z-index you'll encounter is that you don't have an idea what is the "right" value for this property. In my experience, coming up with a value that will work in all situations is never simple, because there are many things to consider:

While z-index is not too much of a problem when the website is simple, as the app scales it will become more and more annoying. Are you thinking of using a 3rd-party component library? It might get even worse, because now you have to deal with values of z-index that were not set by you and they seem even more random.

Now that the image I've painted is black enough, I can lay out my solution to some of those problems.

Step one: get rid of the numbers!

If you're using some form of CSS variables (native, LESS, SASS), then the first thing you can do is to set up some constants. By doing so you'll hide all the magic behind understandable labels. Of course using names like level1, level2, ... and so on is rather redundant, so what you should do instead is think of the types of components that you have in your website and how you want them to be displayed together.

As we started build a library of components at Codility, I added a definition like this:

components at codility

which could then be used like so:


That meant anyone who wanted to use z-index could think of it in terms of named layers rather than some abstract numbers.

This approach proved to be plenty useful for a long time, until we needed to add a tooltip component to our component library. Because having tooltips next to a target element in the actual DOM can be problematic, all of them are instead appended to the document body and are positioned absolutely (thanks to Popper.jsfor computing the right position). And so a decion had to be made: do tooltips go before the modal, or after?

Consider those situations:

  • A tooltip inside a modal should have a higher z-index value, so the order should be: ['modal', 'tooltip'].
  • Given a button with a tooltip on hover and a modal appearing on click, the order should be ['tooltip', 'modal'] so that the hiding tooltip does not appear on top of the appearing modal.

It was impossible to set this with the approach we were using. What it lacked was the notion of a context - every z-index value was defined in a 1-dimensional array, which doesn't work in scenarios such as the one above. It was time to go 2D.

Step two: add some context and go 2D!

Don't worry, it's not about building 2-dimensional arrays. We're not monsters. It's only about slightly shifting the way to think about z-index.

The idea is this: we start with a base level on which the order of components is determined - this is the same as the previous approach. But once we are inside a z-indexed component (a modal, a pop-up, etc.), we go one "level" up. On that level the components have the same order, but all of them appear on top of components on the level below.

That's how we arrived at this piece of code:

increased base value from 10000 from 1000

Notice how in the meantime we increased the base value to 10000 from 1000, courtesy of some 3rd party component.

Now getting the right value works like so: getLevelIndex(1, zIndexMap.modal), getLevelIndex(2, zIndexMap.tooltip), so even if tooltips are hidden behind modals when used on the same level, tooltips from inside the modal will be displayed correctly.

... wait a minute, this seems overly complicated! Now the API is not that friendly, because we have to keep track of the level somehow. But you already know we are going to deal with this, and that's where styled-components come in.

Step three: put everything in a component!

The styled-components library comes with a very powerful tool: themes. Inside every "styled" component you get an additional prop: theme, which carries the context provided by some component above. This is made possible because of React context - a well-hidden gem of this library which recently received an API upgrade.

You can "provide" two types of things in themes: a plain object with some values, or a function that returns such an object. As a bonus, the function is called with the previous theme (set by providers that are higher in the component tree).

Now that we know this, we can add some wrappers for our code:

The setter

First we need to provide the right theme value. We do this by generating the current z-index value for a given type and incrementing the previous level by 1.

incrementing previous level by 1

The component that uses it is quite simple:

array of labels and theme based on type

Both types and getTheme are the values we defined before: the array of labels and the function that gets the theme based on the type.

The getter

After the value is set, we need to use it somehow. Remember, "styled" components get the theme prop:

styled components get the theme prop

... and that's it!

Step four: PROFIT

Now that we have the means to lift our other components onto the next level, let's try using them:

easy setting the value of z-index

Notice how easy setting the value of z-index is: it's always just zIndex 

And providing the value is also simple: just wrap components that use z-index in this one: <LevelUp type={type}>, where type is one of the defined types.

After implementing these ideas z-index is not causing any problems for us. Granted, because we use reusable components, we don't have to set it that often. The only maintenance this needs is adding a new layer type from time to time. But for a few months now this system has worked without problems and it seems pretty solid.

Even if you're not using React or styled-components I hope you'll find something useful here; the pattern shouldn't be hard to adopt in some other tool. In any case, what are your experiences with z-index? How did you handle the problems encountered when using this property? Share your solution in a comment!

Thanks for reading! Be sure to visit the Codility Blog for other thoughts, ideas, and more from our dev team.