How to contain a fixed positioned element

Article autor
September 9, 2025
How to contain a fixed positioned element
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

It's easy to contain absolute positioned elements. Things get a little trickier when you want to contain a fixed positioned element without changing its stylings.

Containing a fixed positioned element

It's easy to contain an absolutely positioned element without changing its stylings, so the elements top and left properties are relative to the container - all you need to do is assign a position: relative property to the container. Things get a little trickier when you want to contain a fixed-positioned element.

One case scenario when it's suitable is a situation in which you have two layouts, for example, user's and admin's, and both of them contain a fixed positioned header. From the admin panel, you would like to have a preview of the user's layout when you create a new blog post to make sure that it matches visually.

Double wrapper

One solution is to create two wrappers: one that's relatively positioned and a second that's absolute.

.wrapper {
  position: relative;
}
.fixed-wrapper {
  position: absolute;
}
.fixed {
  position: fixed;
  top: 0;
}
<div class="wrapper">
  <div class="fixed-wrapper">
    <div class="fixed">Hello World!</div>
  </div>
</div>

The disadvantage of this method is that it requires you to set width and height in order to maintain responsiveness, and it won't work well with content having variable height.

Trick with transform

According to the spec:

For elements whose layout is governed by the CSS box model, any value other than none for the transform results in the creation of both a stacking context and a containing block. The object acts as a containing block for fixed positioned descendants.

So, if you set a transform property to the container, it becomes a containing block.

.wrapper {
  transform: translateZ(0);
}
.fixed {
  position: fixed;
  top: 0;
}
<div class="wrapper">
  <div class="fixed">Hello World!</div>
</div>

On the other side of the coin, this trick prevents child elements from behaving like fixed, even though such behaviour isn't described in the specification.

Work with a team that keeps learning and building better software every day.

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

How to convert string to camel and snake case in Elixir

Sooner or later you may need to convert a string in Elixir to a camel or snake case. With Macro module (available in Elixir without extra dependency) it's super easy.

How to get the struct type in Elixir

So you don’t know what’s the type of struct you’re passing somewhere? Maybe it can be one of few types and you have to distinguish them? Or any other reason… But it’s about checking the struct type. Just use one of the coolest Elixir features - pattern matching!

Using Logger.info and Logger.debug in ExUnit tests

By default in the test env, Phoenix doesn't show Logger.debug/Logger.info outputs in the console.