0

I have the following use case:

#grandparent {
    width: 100%;
}
    
#parent {
    width: calc(100% - 20px);
    height: 268px;
    background-color: white;
    top: 200px;
    position: absolute;
}
    
#child {
    width: 100%;
    height: 100px;
    background-color: green;
    position: absolute;
}
<div id="grandparent"> 
    <div id="parent"> 
        <div id="child"></div>
    </div>
</div>
    

I want to calculate the css property top of the child with the CSS function calc() that is translated as

top = 184px - width of the parent * (184 / 649)

the fiddle is the following: https://jsfiddle.net/flamant/1sn3e8v4/11/

2
  • 1
    Not possible with CSS since 100% width is not calculable in px until render. You need JS. Commented Apr 28, 2020 at 16:36
  • If you could explain what the actual use case is, i.e. what you are trying to do, we might be able to help/ Commented Apr 28, 2020 at 16:38

1 Answer 1

1

I did a mistake in the formula. The formula is actually

top= 184px - (184px - width of the parent * (184 / 649))

and it is possible with

#child {
  width: 100%;
  height: 100px;
  background-color: green;
  position: absolute;
  top: calc(184px - (184px - (100vw - 20px) * (184 / 649)));
}
Sign up to request clarification or add additional context in comments.

Comments

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.