实际上,您可以根据flex项的宽度计算它需要的宽度。
左项的宽度为150px,右项的宽度为100%(解析为400px)。这总计550px,并挤入400px flex容器,因此正确项目的实际使用宽度为400px*(400/550)。
(当然,这是在没有任何扭曲因素的情况下进行的,如flex grow,这将使整个计算更加复杂!)
.container {
display: flex;
width: 400px;
height: 2000px;
background-color: #aaa;
}
.left {
width: 150px;
background-color: lightgreen;
}
.right {
width: 100%;
background-color: lightcoral;
position:relative;
}
.fixed {
position: fixed;
height: 100px;
background-color: skyblue;
width:calc(400px * (400 / (400 + 150)));
}
<div class="container">
<div class="left">
My width can change anytime.
</div>
<div class="right">
<div class="fixed">
I'm fixed. Scroll the page to verify.<br > I want to be the same width as my red parent.<br > Any tips?
</div>
</div>
</div>