Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why adding transform to fixed parent changes child width?

Tags:

html

css

I have a fixed child div inside fixed parent div. When I set the child width to 100% it is set either to 100% width of the window OR 100% of the parent width if I add transform: translate to the parent div.

parent div css:

#outer {
  position: fixed;
  top: 0;
  left: 50%;
  // transform: translate(-50%, 0);

  width: 400px;
  height: 200px;
  z-index: 5000;
  background-color: red;
}

child div:

#inner {
  position: fixed;
  width: 100%;
  left: 30%;
  top: 20%;
  background-color: green;
}

Now #inner is 100% width of the screen. Uncommenting the transform line makes it 100% of parent. What's going on?

  1. http://codepen.io/kokojr/pen/wWvbrz
  2. http://codepen.io/kokojr/pen/KMKLQX

UPDATE:

  1. Ok, I didn't notice that at first but actually it's not only width that changes, position becomes relative too.

  2. For anyone who wonders, that works down the way as well. If you add an #inner-inner div with fixed position it will position itself rel. to #outer unless you add a transform to #inner as well. It's #inner then that becomes the context for fixed children. Reminds me of this very interesting article: https://philipwalton.com/articles/what-no-one-told-you-about-z-index/

like image 989
konrad Avatar asked May 30 '16 22:05

konrad


People also ask

How to set the width of child element while it's fixed?

The truth is, you can't use inherit reliably to set the width of the of the child element while its fixed. This has to do with a misunderstanding, or no understanding, of how fixed actually works. Unlike absolute, fixed doesn't position itself from its closest relative parent. Instead, fixed positions itself relative to the viewport.

What happens when you inherit the width of an element?

That being said, whenever you "inherit" any width it will be respective to the viewport. So it does us no good when we're trying set the width of our target element to the width of it's parent. Learn more about the different behaviors of position. Now that we have a better understanding of fixed, here are two approaches to make this happen

Does the child Div inherit the width of the parent Div?

On chrome, if you set a fixed width on the parent, it seems to work. However if the parent as a % width, it will revert back to the window Following your above instructions, my child div only seems to inherit the parent div's width when I begin resizing the view.

Is it possible to inherit the width of a viewport?

The viewport will always stay fixed, which is why you get the effect that you do. That being said, whenever you "inherit" any width it will be respective to the viewport. So it does us no good when we're trying set the width of our target element to the width of it's parent. Learn more about the different behaviors of position.


1 Answers

From CSS 2.1 - Definition of "containing block" and CSS Position 3 - Definition of containing block,

If the element has position: fixed, the containing block is established by the viewport in the case of continuous media or the page area in the case of paged media.

However, that changes when you add transforms to some ancestor. From CSS Transforms 1 - The Transform Rendering Model,

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.

like image 166
Oriol Avatar answered Nov 15 '22 04:11

Oriol