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?
UPDATE:
Ok, I didn't notice that at first but actually it's not only width that changes, position becomes relative too.
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/
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.
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With