1

I understand it's possible to position:fixed a child element relative to it's parent using transform. When scrolling the document, is it possible to keep the fixed child's position relative to it's parent, rather than the document?

Demo https://jsfiddle.net/ds0vbtbt/3/

Update: Above is a simiplied version of my problem. position:absolute: is not the solution I'm looking for.

Update: Doesn't seem possible without JS once the initial transform is performed

2
  • have you tried to put parent to position relative? Commented Jul 27, 2017 at 9:33
  • @ÁlvaroTouzón yea the parent is set to relative Commented Jul 27, 2017 at 9:58

2 Answers 2

3

Yeah you can do that with position absolute, provided the containing element is set to relative. You don't need the transform property at all.

.test {
  width: 400px;
  height: 400px;
  border: 1px solid;
  position: relative;
}

.box {
  margin-top: 20px;
  width: 300px;
  height: 100px;
  border: 1px solid red;
  position: absolute;
}

Updated fiddle: https://jsfiddle.net/ds0vbtbt/1/

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed).

However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Update: position: fixed is always going to relative to the view-port - so if you change the window size it will be updated, but when scrolling it wont be. That said, Elements with transforms act as a containing block for fixed position descendants, so position:fixed under something with a transform no longer has fixed behavior. They do work when applied to the same element; the element will be positioned as fixed, and then transformed.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer, updated the question. position fixed with transform will create a local coordinate and always place the fixed element relative to it's parent. However, on scrolling the document this position is not "updated". I wonder if this is possible without using `js´ – Stevanicus 1 min ago edit
position: fixed is always going to relative to the viewport - so if you change the window size it will be updated, but when scrolling it wont be. That said, Elements with transforms act as a containing block for fixed position descendants, so position:fixed under something with a transform no longer has fixed behavior. They do work when applied to the same element; the element will be positioned as fixed, and then transformed.
yea, so the only way to update the position after the initial transform is going to be using js isn't it?
It will be yeah due to the way the fixed position behaves
0

You are using position:fixed which fix the element with viewport. Use position:absolute for child element to fix it with parent element.

2 Comments

thanks for your answer, updated the question. position fixed with transform will create a local coordinate and always place the fixed element relative to it's parent. However, on scrolling the document this position is not "updated". I wonder if this is possible without using `js´
yea sorry, came a bit late :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.