Web Advent Calendar 2024

How to win any z-index war

Sometimes, especially with third party code on your site, you enter a race for the highest z-index. People top each other with z-index: 9999999; and z-index: 99999999 !important;.

You can end and win that game by doing

z-index: calc(infinity);

Infinity in CSS

You can also use infinity to define the size or position of an element. Because it is not actually infinite, but just a verly large number.

Style of this div: {
   width: calc(infinity * 1px);
}

For this div with infinite width, we get in your browser:

  • window.getComputedStyle(elem).width
  • elem.getBoundingClientRect().width